Last active
January 11, 2016 11:20
-
-
Save subchen/7352297 to your computer and use it in GitHub Desktop.
Add syntaxhighlighter.js for Pegdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.pegdown.*; | |
import org.pegdown.ast.RootNode; | |
import org.pegdown.ast.VerbatimNode; | |
public class PegdownUtils { | |
public static String asHtml(String source) { | |
PegDownProcessor p = new PegDownProcessor(Extensions.ALL - Extensions.SMARTYPANTS); | |
RootNode root = p.parseMarkdown(source.toCharArray()); | |
return new HighlightHtmlSerializer().toHtml(root); | |
} | |
static class HighlightHtmlSerializer extends ToHtmlSerializer { | |
static final LinkRenderer INSTANCE = new LinkRenderer(); | |
public HighlightHtmlSerializer() { | |
super(INSTANCE); | |
} | |
@Override | |
public void visit(VerbatimNode node) { | |
printer.println(); | |
printer.print("<script type=\"syntaxhighlighter\" class=\"brush: "); | |
String lang = node.getType(); | |
printer.print((lang == null || lang.length() == 0) ? "text" : lang); | |
printer.print("\"><![CDATA[\n").print(node.getText()).print("]]></script>"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment