1 package eu.svjatoslav.sixth.core.document.text;
3 import eu.svjatoslav.commons.string.String2;
4 import eu.svjatoslav.commons.string.tokenizer.Tokenizer;
5 import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
7 import java.util.ArrayList;
10 import static eu.svjatoslav.sixth.core.document.Helper.TG_HYPERLINK;
12 public class FormattedText {
13 List<FormattedTextElement> elements = new ArrayList<>();
15 public void parseOrgSyntax(String orgText) {
17 Tokenizer tokenizer = getTokenizer(orgText);
18 while (tokenizer.hasMoreContent()) {
19 final TokenizerMatch token = tokenizer.getNextToken();
21 if (token.isGroup(TG_HYPERLINK)){
22 elements.add(Hyperlink.fromOrg(token));
26 PlainText plainText = new PlainText(token.token);
27 elements.add(plainText);
32 public static FormattedText fromOrg(String orgText){
33 FormattedText formattedText = new FormattedText();
34 formattedText.parseOrgSyntax(orgText);
38 public String toMD(int indent){
39 StringBuilder sb = new StringBuilder();
41 for (FormattedTextElement element : elements)
42 sb.append(element.toMD());
44 return ensureIndent(sb.toString(), indent);
47 public static String ensureIndent(String input, int indent) {
48 String[] lines = input.split("\\r?\\n");
50 StringBuilder sb = new StringBuilder();
54 for (int i = 1; i< lines.length; i++) {
56 sb.append(new String2(" ").repeat(indent).toString());
63 private Tokenizer getTokenizer(String contents) {
64 final Tokenizer tokenizer = new Tokenizer(contents);
65 tokenizer.addTerminator(Hyperlink.orgTerminator);
66 tokenizer.addTerminator(Hyperlink.orgTerminator2);