From a6880905b9701e78ecf3d467348764ca5b4b0ea0 Mon Sep 17 00:00:00 2001 From: Karl Tauber Date: Tue, 13 Dec 2016 10:06:53 +0100 Subject: [PATCH] syntax highlighting for HTML/XML/SVG/MathML fenced code blocks in editor (#9) --- CHANGES.md | 2 +- README.md | 2 -- .../editor/MarkdownSyntaxHighlighter.java | 22 ++++++++++++++----- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index c5278b13..fd85acce 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -3,7 +3,7 @@ Markdown Writer FX Change Log ## 0.6 -- Syntax highlighting for HTML blocks/inlines in editor. +- Syntax highlighting for HTML/XML/SVG/MathML in editor. - Syntax highlighting for fenced code blocks in Preview that supports [120 languages](http://prismjs.com/#languages-list) (issue #9). - Syntax highlighting in HTML source view. diff --git a/README.md b/README.md index 3f1ebe0a..f620f080 100644 --- a/README.md +++ b/README.md @@ -26,11 +26,9 @@ TODO ---- * auto-complete - * improved syntax highlighting (HTML, ...) * more options * directory tree view * spell checker - * support [Kirby CMS](http://getkirby.com/) content files and tags * TBD Requirements diff --git a/src/main/java/org/markdownwriterfx/editor/MarkdownSyntaxHighlighter.java b/src/main/java/org/markdownwriterfx/editor/MarkdownSyntaxHighlighter.java index 1962404a..f1d0bb94 100644 --- a/src/main/java/org/markdownwriterfx/editor/MarkdownSyntaxHighlighter.java +++ b/src/main/java/org/markdownwriterfx/editor/MarkdownSyntaxHighlighter.java @@ -160,7 +160,6 @@ String cssClass() { // blocks node2lineStyle.put(FencedCodeBlock.class, StyleClass.pre); - node2style.put(FencedCodeBlock.class, StyleClass.pre); node2lineStyle.put(IndentedCodeBlock.class, StyleClass.pre); node2style.put(IndentedCodeBlock.class, StyleClass.pre); node2style.put(BlockQuote.class, StyleClass.blockquote); @@ -211,6 +210,7 @@ private void highlight(Node astRoot, List extraStyledRanges) new VisitHandler<>(OrderedListItem.class, this::visit), new VisitHandler<>(TaskListItem.class, this::visit), new VisitHandler<>(TableCell.class, this::visit), + new VisitHandler<>(FencedCodeBlock.class, this::visit), new VisitHandler<>(HtmlBlock.class, this::visit), new VisitHandler<>(HtmlCommentBlock.class, this::visit), new VisitHandler<>(HtmlInnerBlock.class, this::visit), @@ -371,19 +371,29 @@ private void visit(TableCell node) { setStyleClass(node, node.isHeader() ? StyleClass.th : StyleClass.td); } + private void visit(FencedCodeBlock node) { + String language = node.getInfo().toString(); + if (highlightSequence(node.getContentChars(), language)) { + setStyleClass(node.getOpeningFence(), StyleClass.pre); + setStyleClass(node.getInfo(), StyleClass.pre); + setStyleClass(node.getClosingFence(), StyleClass.pre); + } else + setStyleClass(node, StyleClass.pre); + } + private void visit(HtmlBlockBase node) { setLineStyleClass(node, StyleClass.html); - htmlHighlight(node); + highlightSequence(node.getChars(), "html"); } private void visit(HtmlInlineBase node) { setStyleClass(node, StyleClass.html); - htmlHighlight(node); + highlightSequence(node.getChars(), "html"); } - private void htmlHighlight(Node node) { + private boolean highlightSequence(BasedSequence sequence, String language) { SyntaxHighlighter.HighlightConsumer highlighter = new SyntaxHighlighter.HighlightConsumer() { - private int index = node.getStartOffset(); + private int index = sequence.getStartOffset(); @Override public void accept(int length, String style) { @@ -392,7 +402,7 @@ public void accept(int length, String style) { index += length; } }; - SyntaxHighlighter.highlight(node.getChars().toString(), "html", highlighter); + return SyntaxHighlighter.highlight(sequence.toString(), language, highlighter); } private void setStyleClass(Node node, StyleClass styleClass) {