Skip to content

Commit

Permalink
Merge pull request #92 from Doist/update-text-content-renderer
Browse files Browse the repository at this point in the history
Update text content renderer
  • Loading branch information
robinst committed Sep 6, 2017
2 parents fddcba1 + d75e480 commit 8ad5351
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.commonmark.node.*;
import org.commonmark.renderer.NodeRenderer;
import org.commonmark.renderer.text.holder.BulletListHolder;
import org.commonmark.renderer.text.holder.ListHolder;
import org.commonmark.renderer.text.holder.OrderedListHolder;

import java.util.Arrays;
import java.util.HashSet;
Expand All @@ -15,10 +18,7 @@ public class CoreTextContentNodeRenderer extends AbstractVisitor implements Node
protected final TextContentNodeRendererContext context;
private final TextContentWriter textContent;

private Integer orderedListCounter;
private Character orderedListDelimiter;

private Character bulletListMarker;
private ListHolder listHolder;

public CoreTextContentNodeRenderer(TextContentNodeRendererContext context) {
this.context = context;
Expand Down Expand Up @@ -68,15 +68,22 @@ public void visit(BlockQuote blockQuote) {
visitChildren(blockQuote);
textContent.write('»');

writeEndOfLine(blockQuote, null);
writeEndOfLineIfNeeded(blockQuote, null);
}

@Override
public void visit(BulletList bulletList) {
bulletListMarker = bulletList.getBulletMarker();
if (listHolder != null) {
writeEndOfLine();
}
listHolder = new BulletListHolder(listHolder, bulletList);
visitChildren(bulletList);
writeEndOfLine(bulletList, null);
bulletListMarker = null;
writeEndOfLineIfNeeded(bulletList, null);
if (listHolder.getParent() != null) {
listHolder = listHolder.getParent();
} else {
listHolder = null;
}
}

@Override
Expand All @@ -90,29 +97,29 @@ public void visit(Code code) {
public void visit(FencedCodeBlock fencedCodeBlock) {
if (context.stripNewlines()) {
textContent.writeStripped(fencedCodeBlock.getLiteral());
writeEndOfLine(fencedCodeBlock, null);
writeEndOfLineIfNeeded(fencedCodeBlock, null);
} else {
textContent.write(fencedCodeBlock.getLiteral());
}
}

@Override
public void visit(HardLineBreak hardLineBreak) {
writeEndOfLine(hardLineBreak, null);
writeEndOfLineIfNeeded(hardLineBreak, null);
}

@Override
public void visit(Heading heading) {
visitChildren(heading);
writeEndOfLine(heading, ':');
writeEndOfLineIfNeeded(heading, ':');
}

@Override
public void visit(ThematicBreak thematicBreak) {
if (!context.stripNewlines()) {
textContent.write("***");
}
writeEndOfLine(thematicBreak, null);
writeEndOfLineIfNeeded(thematicBreak, null);
}

@Override
Expand All @@ -134,7 +141,7 @@ public void visit(Image image) {
public void visit(IndentedCodeBlock indentedCodeBlock) {
if (context.stripNewlines()) {
textContent.writeStripped(indentedCodeBlock.getLiteral());
writeEndOfLine(indentedCodeBlock, null);
writeEndOfLineIfNeeded(indentedCodeBlock, null);
} else {
textContent.write(indentedCodeBlock.getLiteral());
}
Expand All @@ -147,42 +154,50 @@ public void visit(Link link) {

@Override
public void visit(ListItem listItem) {
if (orderedListCounter != null) {
textContent.write(String.valueOf(orderedListCounter) + orderedListDelimiter + " ");
if (listHolder != null && listHolder instanceof OrderedListHolder) {
OrderedListHolder orderedListHolder = (OrderedListHolder) listHolder;
String indent = context.stripNewlines() ? "" : orderedListHolder.getIndent();
textContent.write(indent + orderedListHolder.getCounter() + orderedListHolder.getDelimiter() + " ");
visitChildren(listItem);
writeEndOfLine(listItem, null);
orderedListCounter++;
} else if (bulletListMarker != null) {
writeEndOfLineIfNeeded(listItem, null);
orderedListHolder.increaseCounter();
} else if (listHolder != null && listHolder instanceof BulletListHolder) {
BulletListHolder bulletListHolder = (BulletListHolder) listHolder;
if (!context.stripNewlines()) {
textContent.write(bulletListMarker + " ");
textContent.write(bulletListHolder.getIndent() + bulletListHolder.getMarker() + " ");
}
visitChildren(listItem);
writeEndOfLine(listItem, null);
writeEndOfLineIfNeeded(listItem, null);
}
}

@Override
public void visit(OrderedList orderedList) {
orderedListCounter = orderedList.getStartNumber();
orderedListDelimiter = orderedList.getDelimiter();
if (listHolder != null) {
writeEndOfLine();
}
listHolder = new OrderedListHolder(listHolder, orderedList);
visitChildren(orderedList);
writeEndOfLine(orderedList, null);
orderedListCounter = null;
orderedListDelimiter = null;
writeEndOfLineIfNeeded(orderedList, null);
if (listHolder.getParent() != null) {
listHolder = listHolder.getParent();
} else {
listHolder = null;
}
}

@Override
public void visit(Paragraph paragraph) {
visitChildren(paragraph);
// Add "end of line" only if its "root paragraph.
if (paragraph.getParent() == null || paragraph.getParent() instanceof Document) {
writeEndOfLine(paragraph, null);
writeEndOfLineIfNeeded(paragraph, null);
}
}

@Override
public void visit(SoftLineBreak softLineBreak) {
writeEndOfLine(softLineBreak, null);
writeEndOfLineIfNeeded(softLineBreak, null);
}

@Override
Expand Down Expand Up @@ -210,7 +225,7 @@ private void writeText(String text) {

private void writeLink(Node node, String title, String destination) {
boolean hasChild = node.getFirstChild() != null;
boolean hasTitle = title != null;
boolean hasTitle = title != null && !title.equals(destination);
boolean hasDestination = destination != null && !destination.equals("");

if (hasChild) {
Expand Down Expand Up @@ -240,7 +255,7 @@ private void writeLink(Node node, String title, String destination) {
}
}

private void writeEndOfLine(Node node, Character c) {
private void writeEndOfLineIfNeeded(Node node, Character c) {
if (context.stripNewlines()) {
if (c != null) {
textContent.write(c);
Expand All @@ -254,4 +269,12 @@ private void writeEndOfLine(Node node, Character c) {
}
}
}

private void writeEndOfLine() {
if (context.stripNewlines()) {
textContent.whitespace();
} else {
textContent.line();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void line() {
}

public void writeStripped(String s) {
append(s.replaceAll("[\\r\\n\\s]+", " ").trim());
append(s.replaceAll("[\\r\\n\\s]+", " "));
}

public void write(String s) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.commonmark.renderer.text.holder;

import org.commonmark.node.BulletList;

public class BulletListHolder extends ListHolder {
private final char marker;

public BulletListHolder(ListHolder parent, BulletList list) {
super(parent);
marker = list.getBulletMarker();
}

public char getMarker() {
return marker;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.commonmark.renderer.text.holder;

public abstract class ListHolder {
private static final String INDENT_DEFAULT = " ";
private static final String INDENT_EMPTY = "";

private final ListHolder parent;
private final String indent;

ListHolder(ListHolder parent) {
this.parent = parent;

if (parent != null) {
indent = parent.indent + INDENT_DEFAULT;
} else {
indent = INDENT_EMPTY;
}
}

public ListHolder getParent() {
return parent;
}

public String getIndent() {
return indent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.commonmark.renderer.text.holder;

import org.commonmark.node.OrderedList;

public class OrderedListHolder extends ListHolder {
private final char delimiter;
private int counter;

public OrderedListHolder(ListHolder parent, OrderedList list) {
super(parent);
delimiter = list.getDelimiter();
counter = list.getStartNumber();
}

public char getDelimiter() {
return delimiter;
}

public int getCounter() {
return counter;
}

public void increaseCounter() {
counter++;
}
}
Loading

0 comments on commit 8ad5351

Please sign in to comment.