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;
}
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);
}
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;
+ }
+
}
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");
+ }
+
+
}
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 {
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) {
--- /dev/null
+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();
+ }
+}
--- /dev/null
+package eu.svjatoslav.sixth.core.document.text;
+
+public interface FormattedTextElement {
+ String compileMd();
+}
+
--- /dev/null
+package eu.svjatoslav.sixth.core.document.text;
+
+public class Hyperlink implements FormattedTextElement {
+
+ @Override
+ public String compileMd() {
+ return "-TODO-";
+ }
+}
--- /dev/null
+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;
+ }
+}
+
+