Skip to content

Commit

Permalink
Fixed the HTML escaping and refactored the snippet widget.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Jul 28, 2015
1 parent 0dfe7e2 commit 7011cb2
Showing 1 changed file with 10 additions and 3 deletions.
Expand Up @@ -52,16 +52,19 @@ protected Tag render() {
return hardcoded("<pre class=\"example-code\">" + prettify() + "</pre>");
}

public String prettify() {

public static String prettify(String sourceCode, boolean escape) {
// ignoring "\"" => "&quot
String snippet = code.replace("<", "&lt;").replace(">", "&gt;").replace("&", "&amp;");
String snippet = escape ? sourceCode.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
: sourceCode;

snippet = UTILS.replace(snippet, regex, new Mapper<String[], String>() {
@Override
public String map(String[] src) throws Exception {
String s = src[0];
char ch = s.charAt(0);

// FIXME @Annotations

if (Character.isUpperCase(ch)) {
return "<span class=\"_code_cls\">" + s + "</span>";
} else if (ch == '"' || ch == "'".charAt(0)) {
Expand All @@ -81,4 +84,8 @@ public String map(String[] src) throws Exception {
return snippet.trim();
}

public String prettify() {
return prettify(code, true);
}

}

0 comments on commit 7011cb2

Please sign in to comment.