" +
- "
"
- );
-
- _JetLexer jetLexer = new _JetLexer(new StringReader(""));
- jetLexer.reset(normalizedNewlines, 0, normalizedNewlines.length(), _JetLexer.YYINITIAL);
-
- Map
styleMap = new HashMap();
+ private static final Map styleMap = new HashMap();
+ static {
styleMap.put(JetTokens.BLOCK_COMMENT, "comment");
styleMap.put(JetTokens.DOC_COMMENT, "comment");
styleMap.put(JetTokens.EOL_COMMENT, "comment");
@@ -131,24 +158,87 @@ public class JetMacro extends BaseMacro {
styleMap.put(JetTokens.FIELD_IDENTIFIER, "field");
styleMap.put(JetTokens.RAW_STRING_LITERAL, "string");
styleMap.put(TokenType.BAD_CHARACTER, "bad");
+ }
+
+ @Override
+ public boolean hasBody() {
+ return true;
+ }
+
+ @Override
+ public RenderMode getBodyRenderMode() {
+ return RenderMode.allow(0);
+ }
+
+ private static void escapeHTML(StringBuilder builder, CharSequence seq) {
+ if (seq == null) return;
+ for (int i = 0; i < seq.length(); i++) {
+ char c = seq.charAt(i);
+ switch (c) {
+ case '<':
+ builder.append("<");
+ break;
+ case '>':
+ builder.append(">");
+ break;
+ case '&':
+ builder.append("&");
+ break;
+ case '"':
+ builder.append(""");
+ break;
+ default:
+ builder.append(c);
+ }
+ }
+ }
+
+ @Override
+ public String execute(Map map, String code, RenderContext renderContext) throws MacroException {
+ try {
+ List tags = new ArrayList();
+
+ StringBuilder afterPreprocessing = preprocess(code.trim(), tags);
+
+ VelocityContext context = new VelocityContext(MacroUtils.defaultVelocityContext());
+ String renderedTemplate = VelocityUtils.getRenderedTemplate("template.velocity", context);
+
+ StringBuilder result = new StringBuilder(renderedTemplate);
+ result.append(
+ "" +
+ "
" +
+ "
"
+ );
+
+ _JetLexer jetLexer = new _JetLexer(DUMMY_READER);
+ jetLexer.reset(afterPreprocessing, 0, afterPreprocessing.length(), _JetLexer.YYINITIAL);
+
+ Iterator
iterator = tags.iterator();
+ TagData tag = iterator.hasNext() ? iterator.next() : null;
+ while (true) {
+ int tokenEnd = jetLexer.getTokenEnd();
+ while (tag != null && tag.end < tokenEnd) {
+ result.append("Skipping a tag in the middle of a token: <").append(tag.type).append(">
");
+ tag = iterator.hasNext() ? iterator.next() : null;
+ }
- Iterator iterator = tags.iterator();
- ExtraTag tag = iterator.hasNext() ? iterator.next() : null;
- while (true) {
- try {
if (tag != null) {
- if (tag.start == jetLexer.getTokenEnd()) {
- result.append("");
+ if (tag.start == tokenEnd) {
+// result.append("
");
+ tag.type.appendOpenTag(result, tag);
}
- else if (tag.end == jetLexer.getTokenEnd()) {
- result.append("
");
+ }
+ if (tag != null) {
+ if (tag.end == tokenEnd || (tag.nextToken && tag.start < tokenEnd)) {
+ tag.type.appendCloseTag(result, tag);
tag = iterator.hasNext() ? iterator.next() : null;
}
}
+
IElementType token = jetLexer.advance();
if (token == null) break;
CharSequence yytext = jetLexer.yytext();
@@ -168,16 +258,100 @@ public class JetMacro extends BaseMacro {
} else {
style = "plain";
}
- result.append("
").append(yytext).append("");
-
- } catch (Throwable e) {
- result.append("ERROR IN HIGHLIGHTER: ").append(e.getClass().getSimpleName()).append(" : ").append(e.getMessage());
+ result.append("
");
+ escapeHTML(result, yytext);
+ result.append("");
}
- }
- result.append("
");
- result.append("");
- result.append("
");
- return result.toString();
+ result.append("
");
+ result.append("");
+ result.append("
");
+ return result.toString();
+ } catch (Throwable e) {
+ StringBuilder stringBuilder = new StringBuilder();
+ stringBuilder.append("
Jet highlighter error [").append(e.getClass().getSimpleName()).append("]: ");
+ escapeHTML(stringBuilder, e.getMessage());
+ stringBuilder.append("
");
+ stringBuilder.append("Original text:");
+ stringBuilder.append("
");
+ escapeHTML(stringBuilder, code);
+ stringBuilder.append("");
+ stringBuilder.append("
");
+ return stringBuilder.toString();
+ }
+ }
+
+ private StringBuilder preprocess(CharSequence code, Collection