Skip to content

Commit

Permalink
syntax highlighting for HTML/XML/SVG/MathML fenced code blocks in edi…
Browse files Browse the repository at this point in the history
…tor (#9)
  • Loading branch information
DevCharly committed Dec 13, 2016
1 parent f1079f8 commit a688090
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -211,6 +210,7 @@ private void highlight(Node astRoot, List<ExtraStyledRanges> 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),
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down

0 comments on commit a688090

Please sign in to comment.