Separate multiline code block language from remaining parameters
[sixth.git] / src / main / java / eu / svjatoslav / sixth / core / document / content / ListElement.java
index c4e4425..0db636f 100644 (file)
@@ -7,52 +7,148 @@ 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.*;
+import static eu.svjatoslav.sixth.core.document.Utils.isBlank;
 
 public class ListElement implements Content {
     public final FormattedText name;
     public final int indent;
     public final ListElement parent;
-    private String type;
+    private final String type;
     private final List<Content> content = new ArrayList<>();
 
     public ListElement(FormattedText name, int indent, ListElement parent, String type) {
         this.indent = indent;
         this.name = name;
-        this.parent = parent;
         this.type = type;
+        this.parent = parent;
+    }
+
+    private Content findCurrentContentElement(){
+        if (content.isEmpty()) return null;
+        return content.get(content.size()-1);
+    }
+
+    private boolean isLastContentElement(Class aClass){
+        Content contentElement = findCurrentContentElement();
+        if (contentElement == null) return false;
+
+        return contentElement.getClass().isInstance(aClass);
     }
 
-    public void addContent(ListElement content) {
-        this.content.add(content);
+    public void addContent(Content contentElement){
+        content.add(contentElement);
     }
 
     public void parse(TokenizerMatch tm) {
 
         if (tm.isGroup(TG_DOCUMENT_PROPERTY)) {
-//            System.out.println("DOCUMENT PROPERT!!!: " + tm.token);
+            parseDocumentProperty(tm);
             return;
         }
 
-        if (tm.isGroup(null)) {
-//            System.out.println("  Plain text content: " + tm.token);
+        if (tm.isGroup(TG_DRAWER_PROPERTY)) {
+            // TODO
+            // System.out.println("DOCUMENT PROPERTY!!!: " + tm.token);
+            return;
+        }
+
+        if (tm.isGroup(TG_NORMAL_TEXT)) {
+            if (isBlank(tm.token)){
+                parseSeparator();
+                return;
+            }
+
+            parseTextBlock(tm);
+            return;
+        }
+
+        if (tm.isGroup(TG_MULTILINE_CODE)){
+           // System.out.println(tm.toString());
+            String[] groups = tm.getRegExpGroups();
+            content.add(new MultilineCode(
+                    groups[3], // language
+                    groups[5]  // code
+            ));
+            return;
+        }
+
+        if (tm.isGroup(TG_VERSE)){
+            String[] groups = tm.getRegExpGroups();
+            content.add(new Verse(groups[5]));
             return;
         }
 
         System.out.println("ERROR!!!! Unable to handle: " + tm);
+    }
+
+    private void parseTextBlock(TokenizerMatch tm) {
+        TextBlock textBlock;
+        if (isLastContentElement(TextBlock.class)) {
+            textBlock = ((TextBlock) findCurrentContentElement());
+        } else {
+            textBlock = new TextBlock();
+            content.add(textBlock);
+        }
+
+        textBlock.addContent(tm.token + "\n");
+    }
+
+    private void parseDocumentProperty(TokenizerMatch tm) {
+        DocumentPropertyCollection documentPropertyCollection;
+
+        if (isLastContentElement(DocumentPropertyCollection.class)){
+            documentPropertyCollection = (DocumentPropertyCollection)findCurrentContentElement();
+        } else {
+            documentPropertyCollection = new DocumentPropertyCollection();
+            content.add(documentPropertyCollection);
+        }
 
+        documentPropertyCollection.addProperty(
+                tm.getRegExpGroups()[0],
+                tm.getRegExpGroups()[1]);
     }
 
+    private void parseSeparator() {
+        if (isLastContentElement(Separator.class)){
+            ((Separator)findCurrentContentElement()).addLine();
+        } else {
+            content.add(new Separator()); 
+        }
+    }
+
+
+    public void toMD(StringBuilder sb, int indent) {
+        disablePlantUmlExport();
 
-    public void toMD(StringBuilder sb, int i) {
-        if (indent >= 0){
+        if (this.indent >= 0) {
             String2 s = new String2();
-            s.addSuffix(" ", indent).addSuffix(type).addSuffix(" ").addSuffix(name.toMD()).addSuffix("\n");
+            s.append(" ", indent).append(type).append(" ").append(name.toMD(indent + 2)).append("\n");
             sb.append(s.toString());
         }
 
         for (Content c : content) {
-            c.toMD(sb, indent + 2);
+            c.toMD(sb, this.indent + 2);
+        }
+    }
+
+    private void disablePlantUmlExport() {
+
+        for (int i = 0; i< (content.size()-2); i++){
+            if (!(content.get(i) instanceof MultilineCode)) continue;
+
+            MultilineCode code = (MultilineCode) content.get(i);
+            if (!"plantuml".equalsIgnoreCase(code.language)) continue;;
+
+            if (!(content.get(i+1) instanceof DocumentPropertyCollection)) continue;
+            DocumentPropertyCollection property = (DocumentPropertyCollection) content.get(i+1);
+
+            if (!property.hasProperty("results")) continue;;
+
+            if (!(content.get(i+2) instanceof TextBlock)) continue;
+            TextBlock textBlock = (TextBlock) content.get(i+2);
+
+            textBlock.disableForExport();
         }
     }
 }