-package eu.svjatoslav.sixth.core.document;
-
-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_LIST;
-
-public class Heading {
- public final FormattedText name;
- public final int level;
- public final Heading parent;
- private final List<Heading> children = new ArrayList<>();
-
- public final ListElement rootListElement = new ListElement(null, -2, null, "");
- private ListElement currentListElement = rootListElement;
-
- public Heading(FormattedText name, int level, Heading parent){
- this.level = level;
- this.name = name;
- this.parent = parent;
- }
-
- public void addChild(Heading heading){
- children.add(heading);
- }
-
- public List<Heading> getChildren(){
- return children;
- }
-
- public String toMD () {
- StringBuilder sb = new StringBuilder();
-
- if (level > 0) sb.append(enlistTitleInMD());
-
- rootListElement.toMD(sb, -2);
-
- children.stream().map(Heading::toMD).forEach(sb::append);
-
- return sb.toString();
- }
-
- private String enlistTitleInMD() {
- String2 s = new String2();
- s.append("#", level).append(" ").append(name.toMD(0)).append("\n");
- return s.toString();
- }
-
- public ListElement getCurrentHeading(){
- return currentListElement;
- }
-
- 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;
- }
-
- 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];
-
- String title = getPartialTitle(listSections);
-
- 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] : "";
- }
-
-}