Refactoring in JetMacro
This commit is contained in:
@@ -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("<");
|
||||
break;
|
||||
case '>':
|
||||
builder.append(">");
|
||||
break;
|
||||
case '&':
|
||||
builder.append("&");
|
||||
break;
|
||||
case ' ':
|
||||
builder.append(" ");
|
||||
break;
|
||||
case '"':
|
||||
builder.append(""");
|
||||
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.atlassian.renderer.v2.macro.MacroException;
|
||||
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 class JetMacro extends BaseMacro {
|
||||
|
||||
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 class JetMacro extends BaseMacro {
|
||||
@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 class JetMacro extends BaseMacro {
|
||||
@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 class JetMacro extends BaseMacro {
|
||||
@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 class JetMacro extends BaseMacro {
|
||||
@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 class JetMacro extends BaseMacro {
|
||||
@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 class JetMacro extends BaseMacro {
|
||||
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 class JetMacro extends BaseMacro {
|
||||
}
|
||||
|
||||
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 class JetMacro extends BaseMacro {
|
||||
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 class JetMacro extends BaseMacro {
|
||||
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;
|
||||
case '"':
|
||||
builder.append(""");
|
||||
break;
|
||||
default:
|
||||
builder.append(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String addNewLineOpenTag() {
|
||||
return "<div class=\"line\">";
|
||||
}
|
||||
@@ -246,7 +170,7 @@ public class JetMacro extends BaseMacro {
|
||||
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(" ");
|
||||
@@ -259,14 +183,23 @@ public class JetMacro extends BaseMacro {
|
||||
@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 class JetMacro extends BaseMacro {
|
||||
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: <").append(tag.type).append("></div>");
|
||||
while (tag != null && tag.getEnd() < tokenEnd) {
|
||||
result.append("<div class=\"jet hwarning\">Skipping a tag in the middle of a token: <").append(tag.getType()).append("></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,14 +243,15 @@ public class JetMacro extends BaseMacro {
|
||||
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";
|
||||
@@ -325,13 +259,15 @@ public class JetMacro extends BaseMacro {
|
||||
}
|
||||
}
|
||||
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>");
|
||||
}
|
||||
|
||||
@@ -339,19 +275,10 @@ public class JetMacro extends BaseMacro {
|
||||
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 @@ public class JetMacro extends BaseMacro {
|
||||
}
|
||||
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 @@ public class JetMacro extends BaseMacro {
|
||||
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 @@ public class JetMacro extends BaseMacro {
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.jetbrains.jet.tags;
|
||||
|
||||
import org.jetbrains.jet.ConfluenceUtils;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
|
||||
public class StyledDivTagType extends TagType {
|
||||
|
||||
public StyledDivTagType(String tagName) {
|
||||
super(tagName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendOpenTag(StringBuilder builder, TagData tagData) {
|
||||
assert tagData.getType() == this;
|
||||
builder.append("<div class=\"jet ").append(getTagName()).append("\"");
|
||||
if (tagData.getMessage() != null) {
|
||||
builder.append(" title=\"");
|
||||
ConfluenceUtils.escapeHTML(builder, tagData.getMessage());
|
||||
builder.append("\"");
|
||||
}
|
||||
builder.append(">");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void appendCloseTag(StringBuilder builder, TagData tagData) {
|
||||
builder.append("</div>");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package org.jetbrains.jet.tags;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
|
||||
public class TagData {
|
||||
private final TagType type;
|
||||
private final String message;
|
||||
private final int start;
|
||||
private final boolean nextToken;
|
||||
private int end;
|
||||
|
||||
public TagData(TagType type, String message, int start, boolean nextToken) {
|
||||
this.type = type;
|
||||
this.message = message;
|
||||
this.start = start;
|
||||
this.nextToken = nextToken;
|
||||
}
|
||||
|
||||
public TagType getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public int getStart() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public boolean isNextToken() {
|
||||
return nextToken;
|
||||
}
|
||||
|
||||
public int getEnd() {
|
||||
return end;
|
||||
}
|
||||
|
||||
public void setEnd(int end) {
|
||||
this.end = end;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package org.jetbrains.jet.tags;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
|
||||
public abstract class TagType {
|
||||
private final String tagName;
|
||||
|
||||
protected 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;
|
||||
}
|
||||
|
||||
public String getTagName() {
|
||||
return tagName;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user