Misc fixes:
[sixth.git] / src / main / java / eu / svjatoslav / sixth / core / document / Heading.java
index 23cc92f..8a80d9c 100644 (file)
@@ -1,16 +1,14 @@
 package eu.svjatoslav.sixth.core.document;
 
-import eu.svjatoslav.commons.string.tokenizer.InvalidSyntaxException;
-import eu.svjatoslav.commons.string.tokenizer.Tokenizer;
+import eu.svjatoslav.commons.string.String2;
 import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
+import eu.svjatoslav.sixth.core.document.content.ListElement;
 import eu.svjatoslav.sixth.core.document.text.FormattedText;
 
 import java.util.ArrayList;
 import java.util.List;
 
-import static eu.svjatoslav.sixth.core.document.Helper.TG_DOCUMENT_PROPERTY;
 import static eu.svjatoslav.sixth.core.document.Helper.TG_LIST;
-import static eu.svjatoslav.sixth.core.document.text.FormattedText.fromOrg;
 
 public class Heading {
     public final FormattedText name;
@@ -18,7 +16,7 @@ public class Heading {
     public final Heading parent;
     private final List<Heading> children = new ArrayList<>();
 
-    public final ListElement rootListElement = new ListElement(null, 0, null);
+    public final ListElement rootListElement = new ListElement(null, -2, null, "");
     private ListElement currentListElement = rootListElement;
 
     public Heading(FormattedText name, int level, Heading parent){
@@ -40,8 +38,7 @@ public class Heading {
 
         if (level > 0) sb.append(enlistTitleInMD());
 
-       // sb.append(unparsedContent);
-
+        rootListElement.toMD(sb, -2);
 
         children.stream().map(Heading::toMD).forEach(sb::append);
 
@@ -49,63 +46,66 @@ public class Heading {
     }
 
     private String enlistTitleInMD() {
-        StringBuilder sb = new StringBuilder();
-        for (int i = 0; i < level; i++)
-            sb.append("#");
+        String2 s = new String2();
+        s.append("#", level).append(" ").append(name.toMD(0)).append("\n");
+        return s.toString();
+    }
 
-        sb.append(" ").append(name.compileMd()).append("\n");
-        return sb.toString();
+    public ListElement getCurrentHeading(){
+        return currentListElement;
     }
 
-    public ListElement createListElement(FormattedText name, int targetLevel){
-        if (currentListElement.level == (targetLevel - 1)){
-            ListElement newListElement = new ListElement(name, targetLevel, currentListElement);
-            currentListElement.addChild(newListElement);
-            currentListElement = newListElement;
-            return newListElement;
-        }
+    public void parse(TokenizerMatch tm){
 
-        if (currentListElement.level > (targetLevel - 1)){
-            currentListElement = currentListElement.parent;
-            return createListElement(name, targetLevel);
-        }
+        int indent = Utils.getLineIndent(tm.token);
+        if (indent > -1 && indent <= currentListElement.indent) handleListDepthDecrease(indent);
 
-        try {
-            ListElement missingIntermediate = new ListElement(
-                    fromOrg("<noname>"), currentListElement.level + 1, currentListElement);
-            currentListElement.addChild(missingIntermediate);
-            currentListElement = missingIntermediate;
-            return createListElement(name, targetLevel);
-        } catch (InvalidSyntaxException e) {
-            throw new IllegalStateException("impossible situation");
+        if (tm.isGroup(TG_LIST)){
+            parseList(tm);
+            return;
         }
-    }
 
-    public ListElement getCurrentHeading(){
-        return currentListElement;
+        currentListElement.parse(tm);
     }
 
-    public void parse(Tokenizer tokenizer){
-        while (tokenizer.hasMoreContent()) {
-            final TokenizerMatch tm = tokenizer.getNextToken();
-
-            if (tm.isGroup(TG_LIST)){
-                System.out.println("LIST!: " + tm.token);
-                continue;
+    private void handleListDepthDecrease(int indent) {
+        while (true){
+            if (currentListElement.parent.indent <= indent){
+                currentListElement = currentListElement.parent;
+                return;
             }
+            currentListElement = currentListElement.parent;
+        }
+    }
 
-            if (tm.isGroup(TG_DOCUMENT_PROPERTY)){
-//                System.out.println("DOCUMENT PROPERTY!!!: " + tm.token);
-                continue;
-            }
+    private void parseList(TokenizerMatch tm) {
+        String[] listSections = tm.getRegExpGroups();
+        int indent = listSections[0].length();
+        String type = listSections[1];
 
-            if (tm.isGroup(null)){
-                System.out.println("unhandled \"" + tm.token + "\"");
-                continue;
-            }
+        String title = getPartialTitle(listSections);
 
-            tokenizer.unreadToken();
-            break;
+        if (indent > currentListElement.indent){
+            // list dept increases
+            ListElement newElement = new ListElement(title, indent, currentListElement, type);
+            currentListElement.addContent(newElement);
+            currentListElement = newElement;
+            return;
         }
+
+        if (indent > currentListElement.parent.indent){
+            // list depth is the same
+            ListElement newElement = new ListElement(title, indent, currentListElement.parent, type);
+            currentListElement.parent.addContent(newElement);
+            currentListElement = newElement;
+            return;
+        }
+
+        throw new RuntimeException("Impossible condition reached. Must be a bug!");
     }
+
+    private String getPartialTitle(String[] listSections) {
+        return listSections.length > 2 ? listSections[2] : "";
+    }
+
 }