Skip to content
Snippets Groups Projects
Commit f5b04efd authored by Robin Stocker's avatar Robin Stocker
Browse files

Disregard prefixes for line start logic

parent 64d163d2
No related branches found
No related tags found
No related merge requests found
...@@ -88,7 +88,7 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen ...@@ -88,7 +88,7 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen
@Override @Override
public void visit(BlockQuote blockQuote) { public void visit(BlockQuote blockQuote) {
writer.write("> "); writer.writePrefix("> ");
writer.pushPrefix("> "); writer.pushPrefix("> ");
visitChildren(blockQuote); visitChildren(blockQuote);
writer.popPrefix(); writer.popPrefix();
...@@ -124,16 +124,16 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen ...@@ -124,16 +124,16 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen
if (listHolder instanceof BulletListHolder) { if (listHolder instanceof BulletListHolder) {
BulletListHolder bulletListHolder = (BulletListHolder) listHolder; BulletListHolder bulletListHolder = (BulletListHolder) listHolder;
String marker = repeat(" ", listItem.getMarkerIndent()) + bulletListHolder.bulletMarker; String marker = repeat(" ", listItem.getMarkerIndent()) + bulletListHolder.bulletMarker;
writer.write(marker); writer.writePrefix(marker);
writer.write(repeat(" ", contentIndent - marker.length())); writer.writePrefix(repeat(" ", contentIndent - marker.length()));
writer.pushPrefix(repeat(" ", contentIndent)); writer.pushPrefix(repeat(" ", contentIndent));
pushedPrefix = true; pushedPrefix = true;
} else if (listHolder instanceof OrderedListHolder) { } else if (listHolder instanceof OrderedListHolder) {
OrderedListHolder orderedListHolder = (OrderedListHolder) listHolder; OrderedListHolder orderedListHolder = (OrderedListHolder) listHolder;
String marker = repeat(" ", listItem.getMarkerIndent()) + orderedListHolder.number + orderedListHolder.delimiter; String marker = repeat(" ", listItem.getMarkerIndent()) + orderedListHolder.number + orderedListHolder.delimiter;
orderedListHolder.number++; orderedListHolder.number++;
writer.write(marker); writer.writePrefix(marker);
writer.write(repeat(" ", contentIndent - marker.length())); writer.writePrefix(repeat(" ", contentIndent - marker.length()));
writer.pushPrefix(repeat(" ", contentIndent)); writer.pushPrefix(repeat(" ", contentIndent));
pushedPrefix = true; pushedPrefix = true;
} }
...@@ -190,7 +190,7 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen ...@@ -190,7 +190,7 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen
if (indent > 0) { if (indent > 0) {
String indentPrefix = repeat(" ", indent); String indentPrefix = repeat(" ", indent);
writer.write(indentPrefix); writer.writePrefix(indentPrefix);
writer.pushPrefix(indentPrefix); writer.pushPrefix(indentPrefix);
} }
...@@ -286,7 +286,7 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen ...@@ -286,7 +286,7 @@ public class CoreMarkdownNodeRenderer extends AbstractVisitor implements NodeRen
String literal = indentedCodeBlock.getLiteral(); String literal = indentedCodeBlock.getLiteral();
// We need to respect line prefixes which is why we need to write it line by line (e.g. an indented code block // We need to respect line prefixes which is why we need to write it line by line (e.g. an indented code block
// within a block quote) // within a block quote)
writer.write(" "); writer.writePrefix(" ");
writer.pushPrefix(" "); writer.pushPrefix(" ");
List<String> lines = getLines(literal); List<String> lines = getLines(literal);
for (int i = 0; i < lines.size(); i++) { for (int i = 0; i < lines.size(); i++) {
......
...@@ -11,7 +11,8 @@ public class MarkdownWriter { ...@@ -11,7 +11,8 @@ public class MarkdownWriter {
private int blockSeparator = 0; private int blockSeparator = 0;
private boolean tight; private boolean tight;
private char lastChar = '\n'; private char lastChar;
private boolean atLineStart = true;
private final LinkedList<String> prefixes = new LinkedList<>(); private final LinkedList<String> prefixes = new LinkedList<>();
public MarkdownWriter(Appendable out) { public MarkdownWriter(Appendable out) {
...@@ -22,8 +23,11 @@ public class MarkdownWriter { ...@@ -22,8 +23,11 @@ public class MarkdownWriter {
return lastChar; return lastChar;
} }
/**
* @return whether we're at the line start (not counting any prefixes), i.e. after a {@link #line} or {@link #block}.
*/
public boolean isAtLineStart() { public boolean isAtLineStart() {
return lastChar == '\n' || blockSeparator > 0; return atLineStart;
} }
public void write(String s) { public void write(String s) {
...@@ -54,11 +58,13 @@ public class MarkdownWriter { ...@@ -54,11 +58,13 @@ public class MarkdownWriter {
} }
lastChar = s.charAt(s.length() - 1); lastChar = s.charAt(s.length() - 1);
atLineStart = false;
} }
public void line() { public void line() {
append('\n'); append('\n');
writePrefixes(); writePrefixes();
atLineStart = true;
} }
/** /**
...@@ -69,12 +75,19 @@ public class MarkdownWriter { ...@@ -69,12 +75,19 @@ public class MarkdownWriter {
// Remember whether this should be a tight or loose separator now because tight could get changed in between // Remember whether this should be a tight or loose separator now because tight could get changed in between
// this and the next flush. // this and the next flush.
blockSeparator = tight ? 1 : 2; blockSeparator = tight ? 1 : 2;
atLineStart = true;
} }
public void pushPrefix(String prefix) { public void pushPrefix(String prefix) {
prefixes.addLast(prefix); prefixes.addLast(prefix);
} }
public void writePrefix(String prefix) {
boolean tmp = atLineStart;
write(prefix);
atLineStart = tmp;
}
public void popPrefix() { public void popPrefix() {
prefixes.removeLast(); prefixes.removeLast();
} }
...@@ -90,6 +103,7 @@ public class MarkdownWriter { ...@@ -90,6 +103,7 @@ public class MarkdownWriter {
if (length != 0) { if (length != 0) {
lastChar = s.charAt(length - 1); lastChar = s.charAt(length - 1);
} }
atLineStart = false;
} }
private void append(char c) { private void append(char c) {
...@@ -100,6 +114,7 @@ public class MarkdownWriter { ...@@ -100,6 +114,7 @@ public class MarkdownWriter {
} }
lastChar = c; lastChar = c;
atLineStart = false;
} }
private void writePrefixes() { private void writePrefixes() {
......
...@@ -137,6 +137,11 @@ public class MarkdownRendererTest { ...@@ -137,6 +137,11 @@ public class MarkdownRendererTest {
assertRoundTrip("\\## Test\n"); assertRoundTrip("\\## Test\n");
assertRoundTrip("\\#\n"); assertRoundTrip("\\#\n");
assertRoundTrip("Foo\n\\===\n"); assertRoundTrip("Foo\n\\===\n");
// The beginning of the line within the block, so disregarding prefixes
assertRoundTrip("> \\- Test\n");
assertRoundTrip("- \\- Test\n");
// That's not the beginning of the line
assertRoundTrip("`a`- foo\n");
// This is a bit more tricky as we need to check for a list start // This is a bit more tricky as we need to check for a list start
assertRoundTrip("1\\. Foo\n"); assertRoundTrip("1\\. Foo\n");
......
...@@ -62,7 +62,7 @@ public class SpecMarkdownRendererTest { ...@@ -62,7 +62,7 @@ public class SpecMarkdownRendererTest {
System.out.println(); System.out.println();
} }
int expectedPassed = 647; int expectedPassed = 650;
assertTrue("Expected at least " + expectedPassed + " examples to pass but was " + passes.size(), passes.size() >= expectedPassed); assertTrue("Expected at least " + expectedPassed + " examples to pass but was " + passes.size(), passes.size() >= expectedPassed);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment