Ability to generate headings in markdown
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 1 Aug 2020 06:39:16 +0000 (09:39 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Sat, 1 Aug 2020 06:39:16 +0000 (09:39 +0300)
src/main/java/eu/svjatoslav/sixth/core/document/Document.java
src/main/java/eu/svjatoslav/sixth/core/document/Heading.java
src/main/java/eu/svjatoslav/sixth/core/document/MdGenerator.java
src/main/java/eu/svjatoslav/sixth/core/document/OrgParser.java
src/main/java/eu/svjatoslav/sixth/core/document/text/FormattedText.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/sixth/core/document/text/FormattedTextElement.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/sixth/core/document/text/Hyperlink.java [new file with mode: 0644]
src/main/java/eu/svjatoslav/sixth/core/document/text/PlainText.java [new file with mode: 0644]

index 362265c..2385284 100644 (file)
@@ -1,13 +1,18 @@
 package eu.svjatoslav.sixth.core.document;
 
+import eu.svjatoslav.sixth.core.document.text.FormattedText;
+
+import static eu.svjatoslav.sixth.core.document.text.FormattedText.fromOrg;
+
 public class Document {
-    public final Heading rootHeading = new Heading("<root>", 0, null);
+    public final Heading rootHeading = new Heading( fromOrg("<root>") , 0, null);
 
     private Heading currentHeading = rootHeading;
 
-    public Heading createHeading(String name, int targetLevel){
+    public Heading createHeading(FormattedText name, int targetLevel){
         if (currentHeading.level == (targetLevel - 1)){
             Heading newHeading = new Heading(name, targetLevel, currentHeading);
+            currentHeading.addChild(newHeading);
             currentHeading = newHeading;
             return newHeading;
         }
@@ -17,7 +22,8 @@ public class Document {
             return createHeading(name, targetLevel);
         }
 
-        Heading missingIntermediate = new Heading("<noname>", currentHeading.level + 1, currentHeading);
+        Heading missingIntermediate = new Heading(fromOrg("<noname>"), currentHeading.level + 1, currentHeading);
+        currentHeading.addChild(missingIntermediate);
         currentHeading = missingIntermediate;
         return createHeading(name, targetLevel);
     }
index f9c3e6d..6159e1d 100644 (file)
@@ -1,14 +1,28 @@
 package eu.svjatoslav.sixth.core.document;
 
+import eu.svjatoslav.sixth.core.document.text.FormattedText;
+
+import java.util.ArrayList;
+import java.util.List;
+
 public class Heading {
-    public final String name;
+    public final FormattedText name;
     public final int level;
     public final Heading parent;
+    private final List<Heading> children = new ArrayList<>();
 
-    public Heading(String name, int level, Heading parent){
+    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;
+    }
+
 }
index edc08f4..90f1b1a 100644 (file)
@@ -1,4 +1,34 @@
 package eu.svjatoslav.sixth.core.document;
 
+import java.io.File;
+import java.io.IOException;
+
+import static eu.svjatoslav.commons.file.IOHelper.saveToFile;
+
 public class MdGenerator {
+
+    private StringBuilder sb;
+
+    public void generate(Document document, File file) throws IOException {
+        sb = new StringBuilder();
+
+        enlistHeading(document.rootHeading);
+
+        saveToFile(file, sb.toString());
+    }
+
+    private void enlistHeading(Heading heading) {
+        if (heading.level > 0) enlistHeadingTitle(heading);
+
+        heading.getChildren().forEach(this::enlistHeading);
+    }
+
+    private void enlistHeadingTitle(Heading heading) {
+        for (int i = 0; i < heading.level; i++)
+            sb.append("#");
+
+        sb.append(" ").append(heading.name.compileMd()).append("\n");
+    }
+
+
 }
index 15bd880..fdbd41b 100644 (file)
@@ -11,6 +11,7 @@ import java.io.IOException;
 import static eu.svjatoslav.commons.file.IOHelper.getFileContentsAsString;
 import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP;
 import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.PRESERVE;
+import static eu.svjatoslav.sixth.core.document.text.FormattedText.fromOrg;
 
 public class OrgParser {
 
@@ -42,14 +43,14 @@ public class OrgParser {
             return;
 
         if (token.token.startsWith("*")){
-            parseHeading(line, token);
+            parseHeading(token);
             return;
         }
     }
 
-    private void parseHeading(String line, TokenizerMatch token) {
+    private void parseHeading(TokenizerMatch token) {
         int level = token.token.length()-1;
-        document.createHeading(token.reminder, level);
+        document.createHeading(fromOrg(token.reminder), level);
    }
 
     private Tokenizer getLineTokenizer(String contents) {
diff --git a/src/main/java/eu/svjatoslav/sixth/core/document/text/FormattedText.java b/src/main/java/eu/svjatoslav/sixth/core/document/text/FormattedText.java
new file mode 100644 (file)
index 0000000..b9d42a2
--- /dev/null
@@ -0,0 +1,28 @@
+package eu.svjatoslav.sixth.core.document.text;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class FormattedText {
+    List<FormattedTextElement> elements = new ArrayList<>();
+
+    public void parseOrgSyntax(String orgText){
+        PlainText plainText = new PlainText(orgText);
+        elements.add(plainText);
+    }
+
+    public static FormattedText fromOrg(String orgText){
+        FormattedText formattedText = new FormattedText();
+        formattedText.parseOrgSyntax(orgText);
+        return formattedText;
+    }
+
+    public String compileMd(){
+        StringBuilder sb = new StringBuilder();
+
+        for (FormattedTextElement element : elements)
+            sb.append(element.compileMd());
+
+        return sb.toString();
+    }
+}
diff --git a/src/main/java/eu/svjatoslav/sixth/core/document/text/FormattedTextElement.java b/src/main/java/eu/svjatoslav/sixth/core/document/text/FormattedTextElement.java
new file mode 100644 (file)
index 0000000..d769ce4
--- /dev/null
@@ -0,0 +1,6 @@
+package eu.svjatoslav.sixth.core.document.text;
+
+public interface FormattedTextElement {
+    String compileMd();
+}
+
diff --git a/src/main/java/eu/svjatoslav/sixth/core/document/text/Hyperlink.java b/src/main/java/eu/svjatoslav/sixth/core/document/text/Hyperlink.java
new file mode 100644 (file)
index 0000000..9f64b09
--- /dev/null
@@ -0,0 +1,9 @@
+package eu.svjatoslav.sixth.core.document.text;
+
+public class Hyperlink implements FormattedTextElement {
+
+    @Override
+    public String compileMd() {
+        return "-TODO-";
+    }
+}
diff --git a/src/main/java/eu/svjatoslav/sixth/core/document/text/PlainText.java b/src/main/java/eu/svjatoslav/sixth/core/document/text/PlainText.java
new file mode 100644 (file)
index 0000000..6f46732
--- /dev/null
@@ -0,0 +1,17 @@
+package eu.svjatoslav.sixth.core.document.text;
+
+public class PlainText implements FormattedTextElement {
+
+    private String content;
+
+    public PlainText(String content){
+        this.content = content;
+    }
+
+    @Override
+    public String compileMd() {
+        return content;
+    }
+}
+
+