--- /dev/null
+package eu.svjatoslav.sixth.core.document.content;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Things like:
+ * #+RESULTS:
+ * #+LATEX_HEADER: \\usepackage{parskip}
+ * #+OPTIONS: H:20 num:20
+ * #+attr_latex: :width 300px
+ */
+
+public class DocumentPropertyCollection implements Content {
+ private Map<String, List<String>> keyToValue = new HashMap<>();
+
+ @Override
+ public void toMD(StringBuilder sb, int indent) {
+ }
+
+ public void addProperty(String key, String value){
+ getOrCreateValueList(key).add(value);
+ }
+
+ private List<String> getOrCreateValueList(String key){
+ String actualKey = key.toLowerCase();
+ if (keyToValue.containsKey(actualKey))
+ return keyToValue.get(actualKey);
+
+ List valueList = new ArrayList<String>();
+ keyToValue.put(actualKey, valueList);
+ return valueList;
+ }
+
+ public boolean hasProperty(String key){
+ return keyToValue.containsKey(key.toLowerCase());
+ }
+}
import java.util.List;
import static eu.svjatoslav.sixth.core.document.Helper.*;
+import static eu.svjatoslav.sixth.core.document.Utils.isBlank;
public class ListElement implements Content {
public final FormattedText name;
public final ListElement parent;
private final String type;
private final List<Content> content = new ArrayList<>();
- StringBuilder normalTextAccumulator = new StringBuilder();
public ListElement(FormattedText name, int indent, ListElement parent, String type) {
this.indent = indent;
this.parent = parent;
}
- public void addContent(Content content) {
- applyTextAccumulator();
- this.content.add(content);
+ private Content findCurrentContentElement(){
+ if (content.isEmpty()) return null;
+ return content.get(content.size()-1);
}
- private void applyTextAccumulator(){
- if (normalTextAccumulator.length() == 0)
- return;
+ private boolean isLastContentElement(Class aClass){
+ Content contentElement = findCurrentContentElement();
+ if (contentElement == null) return false;
- content.add(new TextBlock(normalTextAccumulator.toString()));
+ return contentElement.getClass().isInstance(aClass);
+ }
- normalTextAccumulator.setLength(0);
+ public void addContent(Content contentElement){
+ content.add(contentElement);
}
public void parse(TokenizerMatch tm) {
if (tm.isGroup(TG_DOCUMENT_PROPERTY)) {
- // TODO
- // System.out.println("DOCUMENT PROPERTY!!!: " + tm.token);
+ parseDocumentProperty(tm);
return;
}
}
if (tm.isGroup(TG_NORMAL_TEXT)) {
- normalTextAccumulator.append(tm.token);
+ if (isBlank(tm.token)){
+ parseSeparator();
+ return;
+ }
+
+ parseTextBlock(tm);
return;
}
if (tm.isGroup(TG_MULTILINE_CODE)){
// System.out.println(tm.toString());
String[] groups = tm.getRegExpGroups();
- addContent(new MultilineCode(
+ content.add(new MultilineCode(
groups[3], // language
groups[5] // code
));
if (tm.isGroup(TG_VERSE)){
String[] groups = tm.getRegExpGroups();
- addContent(new Verse(groups[5]));
+ content.add(new Verse(groups[5]));
return;
}
System.out.println("ERROR!!!! Unable to handle: " + tm);
}
+ private void parseTextBlock(TokenizerMatch tm) {
+ TextBlock textBlock;
+ if (isLastContentElement(TextBlock.class)) {
+ textBlock = ((TextBlock) findCurrentContentElement());
+ } else {
+ textBlock = new TextBlock();
+ content.add(textBlock);
+ }
+
+ textBlock.addContent(tm.token + "\n");
+ }
+
+ private void parseDocumentProperty(TokenizerMatch tm) {
+ DocumentPropertyCollection documentPropertyCollection;
+
+ if (isLastContentElement(DocumentPropertyCollection.class)){
+ documentPropertyCollection = (DocumentPropertyCollection)findCurrentContentElement();
+ } else {
+ documentPropertyCollection = new DocumentPropertyCollection();
+ content.add(documentPropertyCollection);
+ }
+
+ documentPropertyCollection.addProperty(
+ tm.getRegExpGroups()[0],
+ tm.getRegExpGroups()[1]);
+ }
+
+ private void parseSeparator() {
+ if (isLastContentElement(Separator.class)){
+ ((Separator)findCurrentContentElement()).addLine();
+ } else {
+ content.add(new Separator());
+ }
+ }
+
public void toMD(StringBuilder sb, int indent) {
- applyTextAccumulator();
+ disablePlantUmlExport();
if (this.indent >= 0) {
String2 s = new String2();
c.toMD(sb, this.indent + 2);
}
}
+
+ private void disablePlantUmlExport() {
+
+ for (int i = 0; i< (content.size()-2); i++){
+ if (!(content.get(i) instanceof MultilineCode)) continue;
+
+ MultilineCode code = (MultilineCode) content.get(i);
+ if (!"plantuml".equalsIgnoreCase(code.language)) continue;;
+
+ if (!(content.get(i+1) instanceof DocumentPropertyCollection)) continue;
+ DocumentPropertyCollection property = (DocumentPropertyCollection) content.get(i+1);
+
+ if (!property.hasProperty("results")) continue;;
+
+ if (!(content.get(i+2) instanceof TextBlock)) continue;
+ TextBlock textBlock = (TextBlock) content.get(i+2);
+
+ textBlock.disableForExport();
+ }
+ }
}
package eu.svjatoslav.sixth.core.document.content;
import eu.svjatoslav.commons.string.String2;
+import eu.svjatoslav.commons.string.tokenizer.Tokenizer;
+
+import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP;
public class MultilineCode implements Content {
public final String language;
public final String code;
- public MultilineCode(String language, String code) {
- this.language = language;
+ public MultilineCode(String languageAndParams, String code) {
+
+ final Tokenizer tokenizer = new Tokenizer(languageAndParams);
+ tokenizer.addTerminator(DROP, "[ \\t]+", "whitespace");
+ if (tokenizer.hasMoreContent()){
+ language = tokenizer.getNextToken().token;
+ } else {
+ language = null;
+ }
+
this.code = code;
}
}
public String getMDlanguage(){
- // TODO: do not append ORG parameters to language, like: file: ....
if (language == null) return "";
return language;
}