]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/markdown/nodes/Node.java
Markdown to HTML generator has now genration options
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / markdown / nodes / Node.java
1 package org.simantics.scl.compiler.markdown.nodes;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.simantics.scl.compiler.markdown.html.HtmlGenerationContext;
7 import org.simantics.scl.compiler.markdown.internal.ExtensionNodeHandler;
8 import org.simantics.scl.compiler.markdown.internal.MarkdownParser;
9
10
11 public abstract class Node {
12     public Node next;
13     public Node prev;
14     public Node parent;
15     public Node firstChild;
16     public Node lastChild;
17     public boolean open = true;
18     public boolean lastLineBlank = false;
19     public StringBuilder stringContent;
20     public int lineNumber;
21     
22     public void setLastLineBlank(boolean value) {
23         if(MarkdownParser.DEBUG)
24             System.out.println("    " + getClass().getSimpleName() + "@" + hashCode() + " lastLineBlank: " +
25                     lastLineBlank + " -> " + value);
26         this.lastLineBlank = value;
27     }
28     
29     public boolean canContain(Node node) {
30         return false;
31     }
32
33     public boolean acceptLines() {
34         return false;
35     }
36     
37     public void toHtml(HtmlGenerationContext context, StringBuilder b) {
38         for(Node child = firstChild; child != null; child = child.next)
39             child.toHtml(context, b);
40     }
41     
42     public List<HeaderNode> extractHeaders() {
43         ArrayList<HeaderNode> result = new ArrayList<HeaderNode>();
44         for(Node child = firstChild; child != null; child = child.next)
45             if(child instanceof HeaderNode)
46                 result.add((HeaderNode)child);
47         return result;
48     }
49     
50     public String toHtml(HtmlGenerationContext context) {
51         StringBuilder b = new StringBuilder();
52         toHtml(context, b);
53         int len = b.length();
54         if(len > 0 && b.charAt(len-1) == '\n')
55             b.delete(len-1, len);
56         return b.toString();
57     }
58     
59     public void toPlainText(StringBuilder b) {
60         for(Node child = firstChild; child != null; child = child.next)
61             child.toPlainText(b);
62     }
63     
64     public void remove() {
65         if(prev == null)
66             parent.firstChild = next;
67         else
68             prev.next = next;
69         if(next == null)
70             parent.lastChild = prev;
71         else
72             next.prev = prev;
73     }
74     
75     public void processExtensionNodes(ExtensionNodeHandler handler) {
76         for(Node child = firstChild; child != null; child = child.next)
77             child.processExtensionNodes(handler);
78     }
79
80     public void addChild(Node child) {
81         child.parent = this;
82         if(lastChild == null)
83            firstChild = child; 
84         else {
85             lastChild.next = child;
86             child.prev = lastChild;
87         }
88         lastChild = child;
89     }
90 }