Permalink
Browse files

Refactoring in JetMacro

  • Loading branch information...
1 parent bbc8e37 commit 8074629b7762fd685ab249aea3c1fe9f4895b76e @NataliaUkhorskaya NataliaUkhorskaya committed Apr 19, 2012
@@ -1,6 +1,6 @@
<project name="ConfluenceLexer" default="confluenceLexer">
<property name="home" value="${basedir}"/>
- <property file="${home}/../compiler/frontend/idea.properties"/>
+ <property file="${home}/../ideaSDK/bin/idea.properties"/>
<property name="flex.base" value="${idea.home}/tools/lexer/jflex-1.4"/>
<property name="out.dir" value="${basedir}/tmpout"/>
@@ -0,0 +1,47 @@
+package org.jetbrains.jet;
+
+/**
+ * @author Natalia.Ukhorskaya
+ */
+
+public class ConfluenceUtils {
+
+ public 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("&lt;");
+ break;
+ case '>':
+ builder.append("&gt;");
+ break;
+ case '&':
+ builder.append("&amp;");
+ break;
+ case ' ':
+ builder.append("&nbsp;");
+ break;
+ case '"':
+ builder.append("&quot;");
+ break;
+ default:
+ builder.append(c);
+ }
+ }
+ }
+
+ public static String getErrorInHtml(Throwable e, String info) {
+ StringBuilder stringBuilder = new StringBuilder();
+ stringBuilder.append("<div class=\"jet herror\">Jet highlighter error [").append(e.getClass().getSimpleName()).append("]: ");
+ ConfluenceUtils.escapeHTML(stringBuilder, e.getMessage());
+ stringBuilder.append("<br/>");
+ stringBuilder.append("Original text:");
+ stringBuilder.append("<pre>");
+ ConfluenceUtils.escapeHTML(stringBuilder, info);
+ stringBuilder.append("</pre>");
+ stringBuilder.append("</div>");
+ return stringBuilder.toString();
+ }
+}
@@ -25,6 +25,10 @@
import com.intellij.psi.TokenType;
import com.intellij.psi.tree.IElementType;
import org.apache.velocity.VelocityContext;
+import org.jetbrains.jet.ConfluenceUtils;
+import org.jetbrains.jet.tags.StyledDivTagType;
+import org.jetbrains.jet.tags.TagData;
+import org.jetbrains.jet.tags.TagType;
import java.io.StringReader;
import java.util.*;
@@ -38,61 +42,6 @@
public static final StringReader DUMMY_READER = new StringReader("");
- private static class TagData {
- final TagType type;
- final String message;
- final int start;
- final boolean nextToken;
- int end;
-
- TagData(TagType type, String message, int start, boolean nextToken) {
- this.type = type;
- this.message = message;
- this.start = start;
- this.nextToken = nextToken;
- }
- }
-
- private static abstract class TagType {
- public final String tagName;
-
- private TagType(String tagName) {
- this.tagName = tagName;
- }
-
- public abstract void appendOpenTag(StringBuilder builder, TagData tagData);
- public abstract void appendCloseTag(StringBuilder builder, TagData tagData);
-
- @Override
- public String toString() {
- return tagName;
- }
- }
-
- private static class StyledDivTagType extends TagType {
-
- private StyledDivTagType(String tagName) {
- super(tagName);
- }
-
- @Override
- public void appendOpenTag(StringBuilder builder, TagData tagData) {
- assert tagData.type == this;
- builder.append("<div class=\"jet ").append(tagName).append("\"");
- if (tagData.message != null) {
- builder.append(" title=\"");
- escapeHTML(builder, tagData.message);
- builder.append("\"");
- }
- builder.append(">");
- }
-
- @Override
- public void appendCloseTag(StringBuilder builder, TagData tagData) {
- builder.append("</div>");
- }
- }
-
private static final TagType[] knownExtraTagTypes = {
new StyledDivTagType("error"),
new StyledDivTagType("warning"),
@@ -101,7 +50,7 @@ public void appendCloseTag(StringBuilder builder, TagData tagData) {
@Override
public void appendOpenTag(StringBuilder builder, TagData tagData) {
builder.append("<a class=\"jet ref\" href=\"#");
- builder.append(tagData.message);
+ builder.append(tagData.getMessage());
builder.append("\">");
}
@@ -114,7 +63,7 @@ public void appendCloseTag(StringBuilder builder, TagData tagData) {
@Override
public void appendOpenTag(StringBuilder builder, TagData tagData) {
builder.append("<a name=\"");
- builder.append(tagData.message);
+ builder.append(tagData.getMessage());
builder.append("\">");
}
@@ -127,7 +76,7 @@ public void appendCloseTag(StringBuilder builder, TagData tagData) {
@Override
public void appendOpenTag(StringBuilder builder, TagData tagData) {
builder.append("<a class=\"jet anchor\" href=\"");
- builder.append(tagData.message);
+ builder.append(tagData.getMessage());
builder.append("\">");
}
@@ -140,7 +89,7 @@ public void appendCloseTag(StringBuilder builder, TagData tagData) {
@Override
public void appendOpenTag(StringBuilder builder, TagData tagData) {
builder.append("<div style=\"");
- builder.append(tagData.message);
+ builder.append(tagData.getMessage());
builder.append("\">");
}
@@ -153,7 +102,7 @@ public void appendCloseTag(StringBuilder builder, TagData tagData) {
@Override
public void appendOpenTag(StringBuilder builder, TagData tagData) {
builder.append("<div class=\"");
- builder.append(tagData.message);
+ builder.append(tagData.getMessage());
builder.append("\">");
}
@@ -167,9 +116,10 @@ public void appendCloseTag(StringBuilder builder, TagData tagData) {
private static final Map<TagType, Pattern> nextTokenTags = new HashMap<TagType, Pattern>();
private static final Map<TagType, Pattern> closedTags = new HashMap<TagType, Pattern>();
private static final Map<TagType, Pattern> closeTags = new HashMap<TagType, Pattern>();
+
static {
for (TagType type : knownExtraTagTypes) {
- String tagName = type.tagName;
+ String tagName = type.getTagName();
openTags.put(type, Pattern.compile("<" + tagName + "\\s*((desc)?=\\\"([^\n\"]*?)\\\")?>", Pattern.MULTILINE));
closeTags.put(type, Pattern.compile("</" + tagName + ">"));
@@ -179,6 +129,7 @@ public void appendCloseTag(StringBuilder builder, TagData tagData) {
}
private static final Map<IElementType, String> styleMap = new HashMap<IElementType, String>();
+
static {
styleMap.put(JetTokens.BLOCK_COMMENT, "jet-comment");
styleMap.put(JetTokens.DOC_COMMENT, "jet-comment");
@@ -198,7 +149,6 @@ public void appendCloseTag(StringBuilder builder, TagData tagData) {
styleMap.put(JetTokens.LABEL_IDENTIFIER, "label");
styleMap.put(JetTokens.ATAT, "label");
styleMap.put(JetTokens.FIELD_IDENTIFIER, "field");
- styleMap.put(JetTokens.RAW_STRING_LITERAL, "string");
styleMap.put(TokenType.BAD_CHARACTER, "bad");
}
@@ -212,32 +162,6 @@ 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("&lt;");
- break;
- case '>':
- builder.append("&gt;");
- break;
- case '&':
- builder.append("&amp;");
- break;
- case ' ':
- builder.append("&nbsp;");
- break;
- case '"':
- builder.append("&quot;");
- break;
- default:
- builder.append(c);
- }
- }
- }
-
private String addNewLineOpenTag() {
return "<div class=\"line\">";
}
@@ -246,7 +170,7 @@ private String addNewLineCloseTag() {
return "</div>";
}
- private void addNewLines(StringBuilder result, String yytext) {
+ private void convertNewLines(StringBuilder result, String yytext) {
for (int i = 0; i < yytext.length(); i++) {
if (yytext.charAt(i) == '\n') {
result.append("&nbsp;");
@@ -259,14 +183,23 @@ private void addNewLines(StringBuilder result, String yytext) {
@Override
public String execute(Map map, String code, RenderContext renderContext) throws MacroException {
try {
- List<TagData> tags = new ArrayList<TagData>();
-
- StringBuilder afterPreprocessing = preprocess(code.trim(), tags);
-
VelocityContext context = new VelocityContext(MacroUtils.defaultVelocityContext());
String renderedTemplate = VelocityUtils.getRenderedTemplate("template.velocity", context);
-
StringBuilder result = new StringBuilder(renderedTemplate);
+
+ generateHtmlFromCode(code, result);
+
+ return result.toString();
+ } catch (Throwable e) {
+ return ConfluenceUtils.getErrorInHtml(e, code);
+ }
+ }
+
+ public void generateHtmlFromCode(String code, StringBuilder result) throws java.io.IOException {
+ try {
+ List<TagData> tags = new ArrayList<TagData>();
+ StringBuilder afterPreprocessing = preprocess(code.trim(), tags);
+
result.append(
"<div class=\"code panel\" style=\"border-width: 1px;\">" +
"<div class=\"codeContent panelContent\">" +
@@ -282,24 +215,24 @@ public String execute(Map map, String code, RenderContext renderContext) throws
TagData tag = iterator.hasNext() ? iterator.next() : null;
while (true) {
int tokenEnd = jetLexer.getTokenEnd();
- while (tag != null && tag.end < tokenEnd) {
- result.append("<div class=\"jet hwarning\">Skipping a tag in the middle of a token: &lt;").append(tag.type).append("&gt;</div>");
+ while (tag != null && tag.getEnd() < tokenEnd) {
+ result.append("<div class=\"jet hwarning\">Skipping a tag in the middle of a token: &lt;").append(tag.getType()).append("&gt;</div>");
tag = iterator.hasNext() ? iterator.next() : null;
}
if (tag != null) {
- if (tag.start == tokenEnd) {
+ if (tag.getStart() == tokenEnd) {
// result.append("<div class=\"jet ").append(tag.type).append("\"");
// if (tag.message != null) {
// result.append(" title=\"").append(tag.message).append("\"");
// }
// result.append(">");
- tag.type.appendOpenTag(result, tag);
+ tag.getType().appendOpenTag(result, tag);
}
}
if (tag != null) {
- if (tag.end == tokenEnd || (tag.nextToken && tag.start < tokenEnd)) {
- tag.type.appendCloseTag(result, tag);
+ if (tag.getEnd() == tokenEnd || (tag.isNextToken() && tag.getStart() < tokenEnd)) {
+ tag.getType().appendCloseTag(result, tag);
tag = iterator.hasNext() ? iterator.next() : null;
}
}
@@ -310,48 +243,42 @@ public String execute(Map map, String code, RenderContext renderContext) throws
String yytext = jetLexer.yytext().toString();
if (yytext.contains("\n")) {
- addNewLines(result, yytext);
+ convertNewLines(result, yytext);
yytext = yytext.replaceAll("\n", "");
}
String style = null;
if (token instanceof JetKeywordToken) {
style = "keyword";
- } else if (token == JetTokens.IDENTIFIER) {
+ }
+ else if (token == JetTokens.IDENTIFIER) {
for (IElementType softKeyword : JetTokens.SOFT_KEYWORDS.asSet()) {
if (((JetKeywordToken) softKeyword).getValue().equals(yytext.toString())) {
style = "softkeyword";
break;
}
}
style = style == null ? "plain" : style;
- } else if (styleMap.containsKey(token)) {
+ }
+ else if (styleMap.containsKey(token)) {
style = styleMap.get(token);
- } else {
+ }
+ else {
style = "plain";
}
result.append("<code class=\"jet ").append(style).append("\">");
- escapeHTML(result, yytext);
+ ConfluenceUtils.escapeHTML(result, yytext);
result.append("</code>");
}
result.append(addNewLineCloseTag());
result.append("</div>");
result.append("</div>");
result.append("</div>");
- return result.toString();
} catch (Throwable e) {
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append("<div class=\"jet herror\">Jet highlighter error [").append(e.getClass().getSimpleName()).append("]: ");
- escapeHTML(stringBuilder, e.getMessage());
- stringBuilder.append("<br/>");
- stringBuilder.append("Original text:");
- stringBuilder.append("<pre>");
- escapeHTML(stringBuilder, code);
- stringBuilder.append("</pre>");
- stringBuilder.append("</div>");
- return stringBuilder.toString();
+ result = new StringBuilder(ConfluenceUtils.getErrorInHtml(e, code));
}
+
}
private StringBuilder preprocess(CharSequence code, Collection<TagData> tags) {
@@ -382,11 +309,11 @@ private StringBuilder preprocess(CharSequence code, Collection<TagData> tags) {
}
else {
TagData tag = tagStack.pop();
- if (type != tag.type) {
+ if (type != tag.getType()) {
throw new IllegalArgumentException("Unmatched closing tag: " + closeMatcher.group());
}
- tag.end = position;
+ tag.setEnd(position);
tags.add(tag);
i += closeMatcher.end() - 1;
continue charLoop;
@@ -397,7 +324,7 @@ private StringBuilder preprocess(CharSequence code, Collection<TagData> tags) {
Matcher closedMatcher = matchFrom(code, i, closed);
if (closedMatcher != null) {
TagData tag = new TagData(type, closedMatcher.group(3), position, false);
- tag.end = position;
+ tag.setEnd(position);
tags.add(tag);
i += closedMatcher.end() - 1;
continue charLoop;
@@ -408,7 +335,7 @@ private StringBuilder preprocess(CharSequence code, Collection<TagData> tags) {
if (nextMatcher != null) {
TagData tag = new TagData(type, nextMatcher.group(3), position, true);
tags.add(tag);
- tag.end = code.length();
+ tag.setEnd(code.length());
i += nextMatcher.end() - 1;
continue charLoop;
}
Oops, something went wrong.

0 comments on commit 8074629

Please sign in to comment.