1 package eu.svjatoslav.sixth.core.document.text;
3 import eu.svjatoslav.commons.string.tokenizer.Tokenizer;
4 import eu.svjatoslav.commons.string.tokenizer.TokenizerMatch;
6 import java.util.ArrayList;
9 import static eu.svjatoslav.sixth.core.document.Helper.TG_HYPERLINK;
10 import static eu.svjatoslav.sixth.core.document.Utils.addIndentExceptFirstLine;
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 addIndentExceptFirstLine(sb.toString(), indent);
47 private Tokenizer getTokenizer(String contents) {
48 final Tokenizer tokenizer = new Tokenizer(contents);
49 tokenizer.addTerminator(Hyperlink.orgTerminator);
50 tokenizer.addTerminator(Hyperlink.orgTerminator2);
55 public String toString(){