Misc fixes:
[sixth.git] / src / main / java / eu / svjatoslav / sixth / core / document / Heading.java
index f4a9513..8a80d9c 100644 (file)
@@ -1,7 +1,6 @@
 package eu.svjatoslav.sixth.core.document;
 
 import eu.svjatoslav.commons.string.String2;
-import eu.svjatoslav.commons.string.tokenizer.Tokenizer;
 import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
 import eu.svjatoslav.sixth.core.document.content.ListElement;
 import eu.svjatoslav.sixth.core.document.text.FormattedText;
@@ -58,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;
@@ -66,11 +68,22 @@ 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(getPartialTitle(listSections), tm.getTokenizer(), indent));
+
+        String title = getPartialTitle(listSections);
 
         if (indent > currentListElement.indent){
             // list dept increases
@@ -80,7 +93,7 @@ public class Heading {
             return;
         }
 
-        if (indent == currentListElement.indent){
+        if (indent > currentListElement.parent.indent){
             // list depth is the same
             ListElement newElement = new ListElement(title, indent, currentListElement.parent, type);
             currentListElement.parent.addContent(newElement);
@@ -88,57 +101,11 @@ public class Heading {
             return;
         }
 
-        // list dept decreases
-        while (true){
-            if (currentListElement.parent.indent <= indent){
-                if (currentListElement.parent.indent < 0){
-                    // reached first depth level, cannot go any deeper.
-                    // This special situation arisesbb only when lint indents are not properly aligned.
-                    // That is, document structure is incorrect.
-                    ListElement newElement = new ListElement(title, indent, currentListElement.parent, type);
-                    currentListElement.parent.addContent(newElement);
-                    currentListElement = newElement;
-                    return;
-                }
-
-                ListElement newElement = new ListElement(title, indent, currentListElement.parent.parent, type);
-                currentListElement.parent.parent.addContent(newElement);
-                currentListElement = newElement;
-                return;
-            }
-            currentListElement = currentListElement.parent;
-        }
-
+        throw new RuntimeException("Impossible condition reached. Must be a bug!");
     }
 
     private String getPartialTitle(String[] listSections) {
         return listSections.length > 2 ? listSections[2] : "";
     }
 
-    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, Helper.TG_NORMAL_TEXT)){
-                String titleContinuation = tm.token.substring(listIndent).trim();
-                sb.append("\n").append(titleContinuation);
-                continue;
-            }
-
-            tokenizer.unreadToken();
-            break;
-        }
-
-        return sb.toString();
-    }
-
-    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;
-    }
-
 }