From: Hannu Niemistö Date: Tue, 11 Jul 2017 12:20:12 +0000 (+0300) Subject: Markdown to HTML generator has now genration options X-Git-Tag: v1.31.0~264^2~55 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F10%2F710%2F1;p=simantics%2Fplatform.git Markdown to HTML generator has now genration options Change-Id: Ifcb03519f497f32f2cbd870743ba3e349887cdab --- diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/html/HtmlDocumentationGeneration.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/html/HtmlDocumentationGeneration.java index e92f4d3b4..77d192f00 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/html/HtmlDocumentationGeneration.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/html/HtmlDocumentationGeneration.java @@ -1,5 +1,5 @@ -package org.simantics.scl.compiler.markdown.html; +package org.simantics.scl.compiler.markdown.html; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; @@ -62,7 +62,7 @@ public class HtmlDocumentationGeneration { b.append("\n"); } addContentsTree(b, result); - result.toHtml(b); + result.toHtml(HtmlGenerationContext.DEFAULT, b); if(navigation != null) b.append("\n"); diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/html/HtmlGenerationContext.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/html/HtmlGenerationContext.java new file mode 100644 index 000000000..2de5a7066 --- /dev/null +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/html/HtmlGenerationContext.java @@ -0,0 +1,12 @@ +package org.simantics.scl.compiler.markdown.html; + +public class HtmlGenerationContext { + public final boolean generateAnchors; + + public HtmlGenerationContext(boolean generateAnchors) { + this.generateAnchors = generateAnchors; + } + + public static final HtmlGenerationContext DEFAULT = new HtmlGenerationContext(true); + public static final HtmlGenerationContext TEST_DEFAULT = new HtmlGenerationContext(false); +} diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/internal/MarkdownParser.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/internal/MarkdownParser.java index eb4626f66..f49e4781c 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/internal/MarkdownParser.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/internal/MarkdownParser.java @@ -218,7 +218,7 @@ public class MarkdownParser { else if((c == '=' || c == '-') && container instanceof ParagraphNode && Scanner.isSetextHeaderLine(line, firstNonspace, c) - && container.stringContent.indexOf("\n") == -1 + /*&& container.stringContent.indexOf("\n") == -1*/ ) { HeaderNode header = new HeaderNode(c == '=' ? 1 : 2, true); header.lineNumber = container.lineNumber; diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/AutolinkNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/AutolinkNode.java index 34323b402..f7b854007 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/AutolinkNode.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/AutolinkNode.java @@ -1,5 +1,6 @@ package org.simantics.scl.compiler.markdown.nodes; +import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext; import org.simantics.scl.compiler.markdown.internal.HtmlEscape; public class AutolinkNode extends Node { @@ -11,7 +12,7 @@ public class AutolinkNode extends Node { } @Override - public void toHtml(StringBuilder b) { + public void toHtml(HtmlGenerationContext context, StringBuilder b) { b.append("\n"); - super.toHtml(b); + super.toHtml(context, b); b.append("\n"); } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/CodeBlockNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/CodeBlockNode.java index 797a21bf0..fed7a73a8 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/CodeBlockNode.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/CodeBlockNode.java @@ -1,5 +1,6 @@ package org.simantics.scl.compiler.markdown.nodes; +import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext; import org.simantics.scl.compiler.markdown.internal.HtmlEscape; public class CodeBlockNode extends Node { @@ -30,7 +31,7 @@ public class CodeBlockNode extends Node { } @Override - public void toHtml(StringBuilder b) { + public void toHtml(HtmlGenerationContext context, StringBuilder b) { if(infoString == null || infoString.isEmpty()) b.append("
");
         else {
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/CodeNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/CodeNode.java
index 673a3e82d..8a421d293 100644
--- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/CodeNode.java
+++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/CodeNode.java
@@ -1,5 +1,6 @@
 package org.simantics.scl.compiler.markdown.nodes;
 
+import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext;
 import org.simantics.scl.compiler.markdown.internal.HtmlEscape;
 
 public class CodeNode extends Node {
@@ -8,7 +9,7 @@ public class CodeNode extends Node {
     }
     
     @Override
-    public void toHtml(StringBuilder b) {
+    public void toHtml(HtmlGenerationContext context, StringBuilder b) {
         b.append("");
         b.append(HtmlEscape.escape(stringContent));
         b.append("");
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/EmphNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/EmphNode.java
index 82d7d2eb4..b438ab4e0 100644
--- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/EmphNode.java
+++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/EmphNode.java
@@ -1,5 +1,7 @@
 package org.simantics.scl.compiler.markdown.nodes;
 
+import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext;
+
 public class EmphNode extends Node {
     boolean strong;
     
@@ -8,12 +10,12 @@ public class EmphNode extends Node {
     }
 
     @Override
-    public void toHtml(StringBuilder b) {
+    public void toHtml(HtmlGenerationContext context, StringBuilder b) {
         if(strong)
             b.append("");
         else
             b.append("");
-        super.toHtml(b);
+        super.toHtml(context, b);
         if(strong)
             b.append("");
         else
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ExtensionBlockNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ExtensionBlockNode.java
index 62d95e1a3..525a4187c 100644
--- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ExtensionBlockNode.java
+++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ExtensionBlockNode.java
@@ -1,5 +1,6 @@
 package org.simantics.scl.compiler.markdown.nodes;
 
+import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext;
 import org.simantics.scl.compiler.markdown.internal.ExtensionNodeHandler;
 
 public class ExtensionBlockNode extends Node {
@@ -37,7 +38,7 @@ public class ExtensionBlockNode extends Node {
     }
     
     @Override
-    public void toHtml(StringBuilder b) {
+    public void toHtml(HtmlGenerationContext context, StringBuilder b) {
         b.append("::").append(extension).append('[').append(content).append(']');
     }
 }
diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HardLineBreakNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HardLineBreakNode.java
index 71933f0eb..816d595fb 100644
--- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HardLineBreakNode.java
+++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HardLineBreakNode.java
@@ -1,8 +1,10 @@
 package org.simantics.scl.compiler.markdown.nodes;
 
+import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext;
+
 public class HardLineBreakNode extends Node {
     @Override
-    public void toHtml(StringBuilder b) {
+    public void toHtml(HtmlGenerationContext context, StringBuilder b) {
         b.append("
\n"); } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HeaderNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HeaderNode.java index 05de801ad..39a2b47f7 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HeaderNode.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HeaderNode.java @@ -1,5 +1,6 @@ package org.simantics.scl.compiler.markdown.nodes; +import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext; public class HeaderNode extends Node { public int level; @@ -16,11 +17,16 @@ public class HeaderNode extends Node { } @Override - public void toHtml(StringBuilder b) { - b.append(""); - super.toHtml(b); + public void toHtml(HtmlGenerationContext context, StringBuilder b) { + b.append(""); + } + else + b.append('>'); + super.toHtml(context, b); b.append("\n"); } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HorizontalRuleNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HorizontalRuleNode.java index 5416cb0c3..22b393cc2 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HorizontalRuleNode.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HorizontalRuleNode.java @@ -1,8 +1,10 @@ package org.simantics.scl.compiler.markdown.nodes; +import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext; + public class HorizontalRuleNode extends Node { @Override - public void toHtml(StringBuilder b) { + public void toHtml(HtmlGenerationContext context, StringBuilder b) { b.append("
\n"); } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HtmlNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HtmlNode.java index fc0752b64..897c962d0 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HtmlNode.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HtmlNode.java @@ -1,5 +1,7 @@ package org.simantics.scl.compiler.markdown.nodes; +import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext; + public class HtmlNode extends Node { public HtmlNode() { } @@ -12,7 +14,7 @@ public class HtmlNode extends Node { } @Override - public void toHtml(StringBuilder b) { + public void toHtml(HtmlGenerationContext context, StringBuilder b) { b.append(stringContent); b.append('\n'); } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HtmlTagNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HtmlTagNode.java index 25122bc73..9ef771ab9 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HtmlTagNode.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/HtmlTagNode.java @@ -1,11 +1,13 @@ package org.simantics.scl.compiler.markdown.nodes; +import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext; + public class HtmlTagNode extends Node { public HtmlTagNode(StringBuilder stringContent) { this.stringContent = stringContent; } @Override - public void toHtml(StringBuilder b) { + public void toHtml(HtmlGenerationContext context, StringBuilder b) { b.append(stringContent); } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ImageNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ImageNode.java index ebe5d5654..e238c4351 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ImageNode.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ImageNode.java @@ -1,5 +1,6 @@ package org.simantics.scl.compiler.markdown.nodes; +import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext; import org.simantics.scl.compiler.markdown.internal.HtmlEscape; public class ImageNode extends Node { @@ -14,7 +15,7 @@ public class ImageNode extends Node { } @Override - public void toHtml(StringBuilder b) { + public void toHtml(HtmlGenerationContext context, StringBuilder b) { b.append("\"");\n"); return; @@ -29,7 +30,7 @@ public class ItemNode extends Node { for(Node child = firstChild; child != null; child = child.next) { if(child instanceof ParagraphNode) { for(Node n=child.firstChild;n!=null;n=n.next) - n.toHtml(b); + n.toHtml(context, b); noNewline = true; } else { @@ -37,14 +38,14 @@ public class ItemNode extends Node { b.append('\n'); noNewline = false; } - child.toHtml(b); + child.toHtml(context, b); } } b.append("\n"); } else { b.append("
  • \n"); - super.toHtml(b); + super.toHtml(context, b); b.append("
  • \n"); } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/LinkNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/LinkNode.java index 829dd4233..bde60f2b7 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/LinkNode.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/LinkNode.java @@ -1,5 +1,6 @@ package org.simantics.scl.compiler.markdown.nodes; +import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext; import org.simantics.scl.compiler.markdown.internal.HtmlEscape; public class LinkNode extends Node { @@ -14,7 +15,7 @@ public class LinkNode extends Node { } @Override - public void toHtml(StringBuilder b) { + public void toHtml(HtmlGenerationContext context, StringBuilder b) { b.append("
    "); @@ -23,7 +24,7 @@ public class LinkNode extends Node { b.append(HtmlEscape.escape(title)); b.append("\">"); } - super.toHtml(b); + super.toHtml(context, b); b.append(""); } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ListNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ListNode.java index 1bf339738..82e123f26 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ListNode.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ListNode.java @@ -1,5 +1,6 @@ package org.simantics.scl.compiler.markdown.nodes; +import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext; public class ListNode extends Node { public char bulletChar; @@ -25,11 +26,11 @@ public class ListNode extends Node { } @Override - public void toHtml(StringBuilder b) { + public void toHtml(HtmlGenerationContext context, StringBuilder b) { if(bulletChar == '+' || bulletChar == '-' || bulletChar == '*') { b.append("
      \n"); for(Node child = firstChild; child != null; child = child.next) - ((ItemNode)child).toHtml(b, tight); + ((ItemNode)child).toHtml(context, b, tight); b.append("
    \n"); } else { @@ -38,7 +39,7 @@ public class ListNode extends Node { else b.append("
      \n"); for(Node child = firstChild; child != null; child = child.next) - ((ItemNode)child).toHtml(b, tight); + ((ItemNode)child).toHtml(context, b, tight); b.append("
    \n"); } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/Node.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/Node.java index 7c91ceaa0..9e576beed 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/Node.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/Node.java @@ -3,6 +3,7 @@ package org.simantics.scl.compiler.markdown.nodes; import java.util.ArrayList; import java.util.List; +import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext; import org.simantics.scl.compiler.markdown.internal.ExtensionNodeHandler; import org.simantics.scl.compiler.markdown.internal.MarkdownParser; @@ -33,9 +34,9 @@ public abstract class Node { return false; } - public void toHtml(StringBuilder b) { + public void toHtml(HtmlGenerationContext context, StringBuilder b) { for(Node child = firstChild; child != null; child = child.next) - child.toHtml(b); + child.toHtml(context, b); } public List extractHeaders() { @@ -46,9 +47,9 @@ public abstract class Node { return result; } - public String toHtml() { + public String toHtml(HtmlGenerationContext context) { StringBuilder b = new StringBuilder(); - toHtml(b); + toHtml(context, b); int len = b.length(); if(len > 0 && b.charAt(len-1) == '\n') b.delete(len-1, len); diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ParagraphNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ParagraphNode.java index 8e37c1e5c..3b3611ac7 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ParagraphNode.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/ParagraphNode.java @@ -1,5 +1,6 @@ package org.simantics.scl.compiler.markdown.nodes; +import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext; public class ParagraphNode extends Node { @Override @@ -7,9 +8,9 @@ public class ParagraphNode extends Node { return true; } @Override - public void toHtml(StringBuilder b) { + public void toHtml(HtmlGenerationContext context, StringBuilder b) { b.append("

    "); - super.toHtml(b); + super.toHtml(context, b); b.append("

    \n"); } } diff --git a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/TextNode.java b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/TextNode.java index 710b3ce51..8922a1e52 100644 --- a/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/TextNode.java +++ b/bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/TextNode.java @@ -1,5 +1,6 @@ package org.simantics.scl.compiler.markdown.nodes; +import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext; import org.simantics.scl.compiler.markdown.internal.HtmlEscape; public class TextNode extends Node { @@ -7,7 +8,7 @@ public class TextNode extends Node { this.stringContent = text; } @Override - public void toHtml(StringBuilder b) { + public void toHtml(HtmlGenerationContext context, StringBuilder b) { b.append(HtmlEscape.escape(stringContent)); } @Override diff --git a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/markdown/MarkdownTests.java b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/markdown/MarkdownTests.java index ff0c799ed..7644afe05 100644 --- a/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/markdown/MarkdownTests.java +++ b/tests/org.simantics.scl.compiler.tests/src/org/simantics/scl/compiler/tests/markdown/MarkdownTests.java @@ -7,6 +7,7 @@ import java.io.StringReader; import java.nio.charset.Charset; import org.junit.Test; +import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext; import org.simantics.scl.compiler.markdown.internal.MarkdownParser; import org.simantics.scl.compiler.markdown.nodes.Node; @@ -77,10 +78,12 @@ public class MarkdownTests { } public static int test(int id, String in, String out) throws IOException { + if(in.contains("\u2192")) + return SKIPPED; MarkdownParser parser = new MarkdownParser(); Node node = parser.parseDocument(new StringReader(in.replace('\u2192', '\t'))); - String result = node.toHtml().replace('\t', '\u2192'); + String result = node.toHtml(HtmlGenerationContext.TEST_DEFAULT).replace('\t', '\u2192'); boolean passed = result.equals(out);