From 9522b4873da0b7872cf033c0762a58a73c1ba517 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Tue, 1 Sep 2020 20:07:29 +0300 Subject: [PATCH] Properly handle special case, when list depth decreases and depth is misaligned --- .../svjatoslav/sixth/core/document/Heading.java | 17 +++++++++++++++-- .../sixth/core/document/text/FormattedText.java | 4 ++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/main/java/eu/svjatoslav/sixth/core/document/Heading.java b/src/main/java/eu/svjatoslav/sixth/core/document/Heading.java index db9f9df..f4a9513 100644 --- a/src/main/java/eu/svjatoslav/sixth/core/document/Heading.java +++ b/src/main/java/eu/svjatoslav/sixth/core/document/Heading.java @@ -73,6 +73,7 @@ public class Heading { FormattedText title = FormattedText.fromOrg(parseFullListTitle(getPartialTitle(listSections), tm.getTokenizer(), indent)); if (indent > currentListElement.indent){ + // list dept increases ListElement newElement = new ListElement(title, indent, currentListElement, type); currentListElement.addContent(newElement); currentListElement = newElement; @@ -80,16 +81,28 @@ public class Heading { } if (indent == currentListElement.indent){ + // list depth is the same ListElement newElement = new ListElement(title, indent, currentListElement.parent, type); currentListElement.parent.addContent(newElement); currentListElement = newElement; return; } + // list dept decreases while (true){ if (currentListElement.parent.indent <= indent){ - ListElement newElement = new ListElement(title, indent, currentListElement.parent, type); - currentListElement.parent.addContent(newElement); + 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; } diff --git a/src/main/java/eu/svjatoslav/sixth/core/document/text/FormattedText.java b/src/main/java/eu/svjatoslav/sixth/core/document/text/FormattedText.java index ad7d8ca..44af05c 100644 --- a/src/main/java/eu/svjatoslav/sixth/core/document/text/FormattedText.java +++ b/src/main/java/eu/svjatoslav/sixth/core/document/text/FormattedText.java @@ -67,4 +67,8 @@ public class FormattedText { return tokenizer; } + + public String toString(){ + return toMD(0); + } } -- 2.20.1