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();
+ }
+ }
}