From f9ba1a34f75fc8bfe9e6507762947566c8e22bce Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sat, 1 Aug 2020 09:39:16 +0300 Subject: [PATCH] Ability to generate headings in markdown --- .../sixth/core/document/Document.java | 12 ++++++-- .../sixth/core/document/Heading.java | 18 +++++++++-- .../sixth/core/document/MdGenerator.java | 30 +++++++++++++++++++ .../sixth/core/document/OrgParser.java | 7 +++-- .../core/document/text/FormattedText.java | 28 +++++++++++++++++ .../document/text/FormattedTextElement.java | 6 ++++ .../sixth/core/document/text/Hyperlink.java | 9 ++++++ .../sixth/core/document/text/PlainText.java | 17 +++++++++++ 8 files changed, 119 insertions(+), 8 deletions(-) create mode 100644 src/main/java/eu/svjatoslav/sixth/core/document/text/FormattedText.java create mode 100644 src/main/java/eu/svjatoslav/sixth/core/document/text/FormattedTextElement.java create mode 100644 src/main/java/eu/svjatoslav/sixth/core/document/text/Hyperlink.java create mode 100644 src/main/java/eu/svjatoslav/sixth/core/document/text/PlainText.java diff --git a/src/main/java/eu/svjatoslav/sixth/core/document/Document.java b/src/main/java/eu/svjatoslav/sixth/core/document/Document.java index 362265c..2385284 100644 --- a/src/main/java/eu/svjatoslav/sixth/core/document/Document.java +++ b/src/main/java/eu/svjatoslav/sixth/core/document/Document.java @@ -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("", 0, null); + public final Heading rootHeading = new Heading( fromOrg("") , 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("", currentHeading.level + 1, currentHeading); + Heading missingIntermediate = new Heading(fromOrg(""), currentHeading.level + 1, currentHeading); + currentHeading.addChild(missingIntermediate); currentHeading = missingIntermediate; return createHeading(name, targetLevel); } 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 f9c3e6d..6159e1d 100644 --- a/src/main/java/eu/svjatoslav/sixth/core/document/Heading.java +++ b/src/main/java/eu/svjatoslav/sixth/core/document/Heading.java @@ -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 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 getChildren(){ + return children; + } + } diff --git a/src/main/java/eu/svjatoslav/sixth/core/document/MdGenerator.java b/src/main/java/eu/svjatoslav/sixth/core/document/MdGenerator.java index edc08f4..90f1b1a 100644 --- a/src/main/java/eu/svjatoslav/sixth/core/document/MdGenerator.java +++ b/src/main/java/eu/svjatoslav/sixth/core/document/MdGenerator.java @@ -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"); + } + + } diff --git a/src/main/java/eu/svjatoslav/sixth/core/document/OrgParser.java b/src/main/java/eu/svjatoslav/sixth/core/document/OrgParser.java index 15bd880..fdbd41b 100644 --- a/src/main/java/eu/svjatoslav/sixth/core/document/OrgParser.java +++ b/src/main/java/eu/svjatoslav/sixth/core/document/OrgParser.java @@ -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 index 0000000..b9d42a2 --- /dev/null +++ b/src/main/java/eu/svjatoslav/sixth/core/document/text/FormattedText.java @@ -0,0 +1,28 @@ +package eu.svjatoslav.sixth.core.document.text; + +import java.util.ArrayList; +import java.util.List; + +public class FormattedText { + List 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 index 0000000..d769ce4 --- /dev/null +++ b/src/main/java/eu/svjatoslav/sixth/core/document/text/FormattedTextElement.java @@ -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 index 0000000..9f64b09 --- /dev/null +++ b/src/main/java/eu/svjatoslav/sixth/core/document/text/Hyperlink.java @@ -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 index 0000000..6f46732 --- /dev/null +++ b/src/main/java/eu/svjatoslav/sixth/core/document/text/PlainText.java @@ -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; + } +} + + -- 2.20.1