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;fp=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fsixth%2Fcore%2Fdocument%2FHeading.java;h=0ae9ad5433ab6e9a7063c364bab508d6bdfc29f2;hp=23cc92f4e78d3c980c69c302cbd2372b35077bd0;hb=e8052e0b1948260c3701bfe80dfaa8fdf5c38053;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..0ae9ad5 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.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, -1, 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); @@ -53,59 +50,70 @@ public class Heading { for (int i = 0; i < level; i++) sb.append("#"); - sb.append(" ").append(name.compileMd()).append("\n"); + sb.append(" ").append(name.toMD()).append("\n"); return sb.toString(); } - 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 ListElement getCurrentHeading(){ + return currentListElement; + } - if (currentListElement.level > (targetLevel - 1)){ - currentListElement = currentListElement.parent; - return createListElement(name, targetLevel); - } + public void parse(TokenizerMatch tm){ - 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; } + + currentListElement.parse(tm); } - public ListElement getCurrentHeading(){ - return currentListElement; + 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; + + if (indent > currentListElement.indent){ + ListElement newElement = new ListElement(title, indent, parent, type); + currentListElement.addContent(newElement); + currentListElement = newElement; + } + } - 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 String parseFullListTitle(String partialTitle, Tokenizer tokenizer, int listIndent){ + StringBuilder sb = new StringBuilder(); + sb.append(partialTitle); - if (tm.isGroup(TG_DOCUMENT_PROPERTY)){ -// System.out.println("DOCUMENT PROPERTY!!!: " + tm.token); - continue; - } + while (tokenizer.hasMoreContent()){ + final TokenizerMatch tm = tokenizer.getNextToken(); - if (tm.isGroup(null)){ - System.out.println("unhandled \"" + tm.token + "\""); + if (isContentContinuation(tm, listIndent, null)){ + 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; + } + }