e1cf33d36e595756d003e52b72969a2cb56f9d41
[sixth.git] / src / main / java / eu / svjatoslav / sixth / core / document / content / ListElement.java
1 package eu.svjatoslav.sixth.core.document.content;
2
3 import eu.svjatoslav.commons.string.String2;
4 import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
5 import eu.svjatoslav.sixth.core.document.text.FormattedText;
6
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import static eu.svjatoslav.sixth.core.document.Helper.*;
11
12 public class ListElement implements Content {
13     public final FormattedText name;
14     public final int indent;
15     public final ListElement parent;
16     private final String type;
17     private final List<Content> content = new ArrayList<>();
18     StringBuilder normalTextAccumulator = new StringBuilder();
19
20     public ListElement(FormattedText name, int indent, ListElement parent, String type) {
21         this.indent = indent;
22         this.name = name;
23         this.type = type;
24         this.parent = parent;
25     }
26
27     public void addContent(Content content) {
28         applyTextAccumulator();
29         this.content.add(content);
30     }
31
32     private void applyTextAccumulator(){
33         if (normalTextAccumulator.length() == 0)
34             return;
35
36         content.add(new TextBlock(normalTextAccumulator.toString()));
37
38         normalTextAccumulator.setLength(0);
39     }
40
41     public void parse(TokenizerMatch tm) {
42
43         if (tm.isGroup(TG_DOCUMENT_PROPERTY)) {
44             // TODO
45             // System.out.println("DOCUMENT PROPERTY!!!: " + tm.token);
46             return;
47         }
48
49         if (tm.isGroup(TG_DRAWER_PROPERTY)) {
50             // TODO
51             // System.out.println("DOCUMENT PROPERTY!!!: " + tm.token);
52             return;
53         }
54
55         if (tm.isGroup(TG_NORMAL_TEXT)) {
56             normalTextAccumulator.append(tm.token);
57             return;
58         }
59
60         if (tm.isGroup(TG_MULTILINE_CODE)){
61            // System.out.println(tm.toString());
62             String[] groups = tm.getRegExpGroups();
63             addContent(new MultilineCode(
64                     groups[3], // language
65                     groups[5]  // code
66             ));
67             return;
68         }
69
70         if (tm.isGroup(TG_VERSE)){
71             String[] groups = tm.getRegExpGroups();
72             addContent(new Verse(groups[5]));
73             return;
74         }
75
76         System.out.println("ERROR!!!! Unable to handle: " + tm);
77     }
78
79
80     public void toMD(StringBuilder sb, int indent) {
81         applyTextAccumulator();
82
83         if (this.indent >= 0) {
84             String2 s = new String2();
85             s.append(" ", indent).append(type).append(" ").append(name.toMD(indent + 2)).append("\n");
86             sb.append(s.toString());
87         }
88
89         for (Content c : content) {
90             c.toMD(sb, this.indent + 2);
91         }
92     }
93 }