1 package eu.svjatoslav.sixth.core.document;
3 import eu.svjatoslav.commons.string.String2;
4 import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
5 import eu.svjatoslav.sixth.core.document.content.ListElement;
6 import eu.svjatoslav.sixth.core.document.text.FormattedText;
8 import java.util.ArrayList;
11 import static eu.svjatoslav.sixth.core.document.Helper.TG_LIST;
13 public class Heading {
14 public final FormattedText name;
15 public final int level;
16 public final Heading parent;
17 private final List<Heading> children = new ArrayList<>();
19 public final ListElement rootListElement = new ListElement(null, -2, null, "");
20 private ListElement currentListElement = rootListElement;
22 public Heading(FormattedText name, int level, Heading parent){
28 public void addChild(Heading heading){
29 children.add(heading);
32 public List<Heading> getChildren(){
36 public String toMD () {
37 StringBuilder sb = new StringBuilder();
39 if (level > 0) sb.append(enlistTitleInMD());
41 rootListElement.toMD(sb, -2);
43 children.stream().map(Heading::toMD).forEach(sb::append);
48 private String enlistTitleInMD() {
49 String2 s = new String2();
50 s.append("#", level).append(" ").append(name.toMD(0)).append("\n");
54 public ListElement getCurrentHeading(){
55 return currentListElement;
58 public void parse(TokenizerMatch tm){
60 int indent = Utils.getLineIndent(tm.token);
61 if (indent > -1 && indent <= currentListElement.indent) handleListDepthDecrease(indent);
63 if (tm.isGroup(TG_LIST)){
68 currentListElement.parse(tm);
71 private void handleListDepthDecrease(int indent) {
73 if (currentListElement.parent.indent <= indent){
74 currentListElement = currentListElement.parent;
77 currentListElement = currentListElement.parent;
81 private void parseList(TokenizerMatch tm) {
82 String[] listSections = tm.getRegExpGroups();
83 int indent = listSections[0].length();
84 String type = listSections[1];
86 String title = getPartialTitle(listSections);
88 if (indent > currentListElement.indent){
89 // list dept increases
90 ListElement newElement = new ListElement(title, indent, currentListElement, type);
91 currentListElement.addContent(newElement);
92 currentListElement = newElement;
96 if (indent > currentListElement.parent.indent){
97 // list depth is the same
98 ListElement newElement = new ListElement(title, indent, currentListElement.parent, type);
99 currentListElement.parent.addContent(newElement);
100 currentListElement = newElement;
104 throw new RuntimeException("Impossible condition reached. Must be a bug!");
107 private String getPartialTitle(String[] listSections) {
108 return listSections.length > 2 ? listSections[2] : "";