X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=sixth.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fcore%2Fdocument%2FHeading.java;h=8a80d9c825668ae2a3fed94274184938106ca7d9;hp=23cc92f4e78d3c980c69c302cbd2372b35077bd0;hb=168cf05300cec2bb4c0607711b752609976b2901;hpb=e6e265a23ae9e32b65ba6a70d302ae680e73138f 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 23cc92f..8a80d9c 100644 --- a/src/main/java/eu/svjatoslav/sixth/core/document/Heading.java +++ b/src/main/java/eu/svjatoslav/sixth/core/document/Heading.java @@ -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 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(""), 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] : ""; + } + }