1 package org.simantics.scl.compiler.internal.parsing.documentation;
3 import java.io.IOException;
4 import java.io.StringReader;
5 import java.util.regex.Matcher;
6 import java.util.regex.Pattern;
8 import org.simantics.scl.compiler.elaboration.modules.Documentation;
9 import org.simantics.scl.compiler.types.Type;
10 import org.simantics.scl.compiler.types.util.TypeUnparsingContext;
12 public abstract class HtmlUnparsingContext {
13 StringBuilder stringBuilder;
14 String listLevel = "";
16 int tableOfContentsLevel = 0;
17 StringBuilder tableOfContents;
21 public HtmlUnparsingContext(StringBuilder stringBuilder) {
22 this.stringBuilder = stringBuilder;
23 this.tableOfContents = null;
26 public HtmlUnparsingContext() {
27 this(new StringBuilder());
28 tableOfContents = new StringBuilder();
31 public StringBuilder getStringBuilder() {
35 public StringBuilder getTableOfContents() {
36 return tableOfContents;
39 public void finishTableOfContents() {
40 while(tableOfContentsLevel > 0) {
41 tableOfContents.append("</ul>\n");
42 --tableOfContentsLevel;
46 public void header(int level, String text) {
49 stringBuilder.append("<h").append(level)
50 .append(" id=\"sec").append(headerLinkId).append("\">")
52 .append("</h").append(level).append(">\n");
54 while(tableOfContentsLevel > level) {
55 tableOfContents.append("</ul>\n");
56 --tableOfContentsLevel;
58 while(tableOfContentsLevel < level) {
59 tableOfContents.append("<ul>\n");
60 ++tableOfContentsLevel;
62 tableOfContents.append("<li><a href=\"#sec")
63 .append(headerLinkId).append("\">")
64 .append(text).append("</a></li>\n");
71 public void setListLevel(String newLevel) {
72 String oldLevel = this.listLevel;
74 while(commonLevel < oldLevel.length() && commonLevel < newLevel.length() &&
75 newLevel.charAt(commonLevel) == oldLevel.charAt(commonLevel))
78 for(int i=oldLevel.length()-1;i>=commonLevel;--i) {
79 char c = oldLevel.charAt(i);
81 stringBuilder.append("</ul>\n");
83 stringBuilder.append("</ol>\n");
85 for(int i=commonLevel;i<newLevel.length();++i) {
86 char c = newLevel.charAt(i);
88 stringBuilder.append("<ul>\n");
90 stringBuilder.append("<ol>\n");
93 this.listLevel = newLevel;
96 public void appendDocumentation(String doc) {
97 DocumentationLexer lexer = new DocumentationLexer(new StringReader(doc));
101 DocumentationElement element = null;
103 element = lexer.nextToken();
104 } catch (IOException e) {
106 } catch (DocumentationParsingException e) {
111 element.toHtml(this);
116 public static String escape(String text) {
117 return text.replace("&", "&").replace("<", "<").replace(">", ">");
120 public abstract void documentEntity(String ref);
122 public void appendType(TypeUnparsingContext tuc, Type type, int precedence) {
123 StringBuilder b = new StringBuilder();
124 type.toString(tuc, b, precedence);
125 stringBuilder.append(escape(b.toString()));
128 public void appendType(TypeUnparsingContext tuc, Type type) {
129 appendType(tuc, type, 3);
132 public void appendRef(String ref) {
133 stringBuilder.append("<a name=\"").append(ref).append("\" class=\"ref\">")
134 .append(escape(ref)).append("</a>");
137 public Documentation toDocumentation() {
138 finishTableOfContents();
139 return new Documentation(stringBuilder.toString(), tableOfContents.toString());
142 private static final Pattern PARAGRAPH_PATTERN = Pattern.compile("'[^ ']+'|@[^@]+@");
144 public void appendParagraph(String text) {
146 stringBuilder.append("<p>");
147 Matcher m = PARAGRAPH_PATTERN.matcher(text);
150 int start = m.start();
153 stringBuilder.append(text, lastAppend, start);
154 char c = text.charAt(start);
156 String ref = text.substring(start+1, end-1);
158 stringBuilder.append("<code><a href=\"#").append(ref).append("\">")
160 .append("</a></code>");
163 stringBuilder.append("<code>").append(text, start+1, end-1).append("</code>");
167 stringBuilder.append(text, lastAppend, text.length());
168 stringBuilder.append("</p>\n");