Misc fixes:
[sixth.git] / src / main / java / eu / svjatoslav / sixth / core / document / Heading.java
index 0ae9ad5..8a80d9c 100644 (file)
@@ -1,6 +1,6 @@
 package eu.svjatoslav.sixth.core.document;
 
-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;
@@ -16,7 +16,7 @@ public class Heading {
     public final Heading parent;
     private final List<Heading> children = new ArrayList<>();
 
-    public final ListElement rootListElement = new ListElement(null, -1, null, "");
+    public final ListElement rootListElement = new ListElement(null, -2, null, "");
     private ListElement currentListElement = rootListElement;
 
     public Heading(FormattedText name, int level, Heading parent){
@@ -46,12 +46,9 @@ public class Heading {
     }
 
     private String enlistTitleInMD() {
-        StringBuilder sb = new StringBuilder();
-        for (int i = 0; i < level; i++)
-            sb.append("#");
-
-        sb.append(" ").append(name.toMD()).append("\n");
-        return sb.toString();
+        String2 s = new String2();
+        s.append("#", level).append(" ").append(name.toMD(0)).append("\n");
+        return s.toString();
     }
 
     public ListElement getCurrentHeading(){
@@ -60,6 +57,9 @@ public class Heading {
 
     public void parse(TokenizerMatch tm){
 
+        int indent = Utils.getLineIndent(tm.token);
+        if (indent > -1 && indent <= currentListElement.indent) handleListDepthDecrease(indent);
+
         if (tm.isGroup(TG_LIST)){
             parseList(tm);
             return;
@@ -68,52 +68,44 @@ public class Heading {
         currentListElement.parse(tm);
     }
 
+    private void handleListDepthDecrease(int indent) {
+        while (true){
+            if (currentListElement.parent.indent <= indent){
+                currentListElement = currentListElement.parent;
+                return;
+            }
+            currentListElement = currentListElement.parent;
+        }
+    }
+
     private void parseList(TokenizerMatch tm) {
         String[] listSections = tm.getRegExpGroups();
         int indent = listSections[0].length();
         String type = listSections[1];
 
-        FormattedText title = FormattedText.fromOrg(parseFullListTitle(listSections.length > 2 ? listSections[2] : "", tm.getTokenizer(), indent));
-
-//        System.out.println("  indent: " + indent);
-//        System.out.println("  type: " + type);
-//        System.out.println("  title: " + title);
-
-        ListElement parent = null;
+        String title = getPartialTitle(listSections);
 
         if (indent > currentListElement.indent){
-            ListElement newElement = new ListElement(title, indent, parent, type);
+            // list dept increases
+            ListElement newElement = new ListElement(title, indent, currentListElement, type);
             currentListElement.addContent(newElement);
             currentListElement = newElement;
+            return;
         }
 
-    }
-
-
-    private String parseFullListTitle(String partialTitle, Tokenizer tokenizer, int listIndent){
-        StringBuilder sb = new StringBuilder();
-        sb.append(partialTitle);
-
-        while (tokenizer.hasMoreContent()){
-            final TokenizerMatch tm = tokenizer.getNextToken();
-
-            if (isContentContinuation(tm, listIndent, null)){
-                String titleContinuation = tm.token.substring(listIndent).trim();
-                sb.append("\n").append(titleContinuation);
-                continue;
-            }
-
-            tokenizer.unreadToken();
-            break;
+        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;
         }
 
-        return sb.toString();
+        throw new RuntimeException("Impossible condition reached. Must be a bug!");
     }
 
-    public static boolean isContentContinuation(TokenizerMatch tm, int requiredIndent, String requiredGroup) {
-        if (tm.token.length() <= requiredIndent) return false;
-
-        return tm.isGroup(requiredGroup) && tm.token.substring(0, requiredIndent +1).trim().length() == 0;
+    private String getPartialTitle(String[] listSections) {
+        return listSections.length > 2 ? listSections[2] : "";
     }
 
 }