package org.simantics.scl.compiler.internal.parsing.documentation; import java.io.IOException; import java.io.StringReader; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.simantics.scl.compiler.elaboration.modules.Documentation; import org.simantics.scl.compiler.types.Type; import org.simantics.scl.compiler.types.util.TypeUnparsingContext; public abstract class HtmlUnparsingContext { StringBuilder stringBuilder; String listLevel = ""; int tableOfContentsLevel = 0; StringBuilder tableOfContents; int headerLinkId = 0; public HtmlUnparsingContext(StringBuilder stringBuilder) { this.stringBuilder = stringBuilder; this.tableOfContents = null; } public HtmlUnparsingContext() { this(new StringBuilder()); tableOfContents = new StringBuilder(); } public StringBuilder getStringBuilder() { return stringBuilder; } public StringBuilder getTableOfContents() { return tableOfContents; } public void finishTableOfContents() { while(tableOfContentsLevel > 0) { tableOfContents.append("\n"); --tableOfContentsLevel; } } public void header(int level, String text) { clear(); ++headerLinkId; stringBuilder.append("") .append(text) .append("\n"); while(tableOfContentsLevel > level) { tableOfContents.append("\n"); --tableOfContentsLevel; } while(tableOfContentsLevel < level) { tableOfContents.append("\n"); else if(c == '#') stringBuilder.append("\n"); } for(int i=commonLevel;i\n"); else if(c == '#') stringBuilder.append("
    \n"); } this.listLevel = newLevel; } public void appendDocumentation(String doc) { DocumentationLexer lexer = new DocumentationLexer(new StringReader(doc)); clear(); while(true) { DocumentationElement element = null; try { element = lexer.nextToken(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentationParsingException e) { e.printStackTrace(); } if(element == null) break; element.toHtml(this); } clear(); } public static String escape(String text) { return text.replace("&", "&").replace("<", "<").replace(">", ">"); } public abstract void documentEntity(String ref); public void appendType(TypeUnparsingContext tuc, Type type, int precedence) { StringBuilder b = new StringBuilder(); type.toString(tuc, b, precedence); stringBuilder.append(escape(b.toString())); } public void appendType(TypeUnparsingContext tuc, Type type) { appendType(tuc, type, 3); } public void appendRef(String ref) { stringBuilder.append("") .append(escape(ref)).append(""); } public Documentation toDocumentation() { finishTableOfContents(); return new Documentation(stringBuilder.toString(), tableOfContents.toString()); } private static final Pattern PARAGRAPH_PATTERN = Pattern.compile("'[^ ']+'|@[^@]+@"); public void appendParagraph(String text) { clear(); stringBuilder.append("

    "); Matcher m = PARAGRAPH_PATTERN.matcher(text); int lastAppend = 0; while(m.find()) { int start = m.start(); int end = m.end(); stringBuilder.append(text, lastAppend, start); char c = text.charAt(start); if(c == '\'') { String ref = text.substring(start+1, end-1); stringBuilder.append("") .append(ref) .append(""); } else if(c == '@') { stringBuilder.append("").append(text, start+1, end-1).append(""); } lastAppend = end; } stringBuilder.append(text, lastAppend, text.length()); stringBuilder.append("

    \n"); } }