Skip to content

Commit

Permalink
gobble spaces after backslash-hardline-break
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Schulz authored and Thilo-Acrolinx committed Aug 22, 2024
1 parent 5354c00 commit bbcbfcc
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,77 @@ public void blockquotesWithCrLfLineBreaks() {
assertFalse(it.hasNext());
}

@Test
public void assertSpacesHardLineBreak() {

//---------------------- - -1----------
//--------------01234567 8 901234567890
String given = "line1 \r\n line2";
Parser parser = Parser.builder().build();
Document document = parser.parse(given);

assertThat(document.getFirstChild(), instanceOf(Paragraph.class));
ReversiblePeekingIterator<Node> it = document.getFirstChild().getChildIterator();

assertTrue(it.hasNext());
Node node = it.next();
assertThat(node, instanceOf(Text.class));
assertEquals("line1", node.getChars().toString());
assertEquals(0, node.getStartOffset());
assertEquals(5, node.getEndOffset());

assertTrue(it.hasNext());
node = it.next();
assertThat(node, instanceOf(HardLineBreak.class));
assertEquals(5, node.getStartOffset());
assertEquals(9, node.getEndOffset());

assertTrue(it.hasNext());
node = it.next();
assertThat(node, instanceOf(Text.class));
assertEquals("line2", node.getChars().toString());
assertEquals(14, node.getStartOffset());
assertEquals(19, node.getEndOffset());

assertFalse(it.hasNext());
}

@Test
public void assertBackslashHardLineBreak() {

//-------------------- - - --1----------
//--------------012345 6 7 8901234567890
String given = "line1\\\r\n line2";
Parser parser = Parser.builder().build();
Document document = parser.parse(given);

assertThat(document.getFirstChild(), instanceOf(Paragraph.class));
ReversiblePeekingIterator<Node> it = document.getFirstChild().getChildIterator();

assertTrue(it.hasNext());
Node node = it.next();
assertThat(node, instanceOf(Text.class));
assertEquals("line1", node.getChars().toString());
assertEquals(0, node.getStartOffset());
assertEquals(5, node.getEndOffset());

assertTrue(it.hasNext());
node = it.next();
assertThat(node, instanceOf(HardLineBreak.class));
assertEquals(5, node.getStartOffset());
assertEquals(8, node.getEndOffset());

assertTrue(it.hasNext());
node = it.next();
assertThat(node, instanceOf(Text.class));
assertEquals("line2", node.getChars().toString());
assertEquals(13, node.getStartOffset());
assertEquals(18, node.getEndOffset());

assertFalse(it.hasNext());
}


String escape(String input, Parser parser) {
BasedSequence baseSeq = BasedSequence.of(input);
List<SpecialLeadInHandler> handlers = Parser.SPECIAL_LEAD_IN_HANDLERS.get(parser.getOptions());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -524,9 +524,8 @@ public boolean parseNewline() {
}

// gobble leading spaces in next line
while (peek() == ' ') {
index++;
}
gobbleLeadingSpaces();

return true;
}

Expand All @@ -542,6 +541,7 @@ protected boolean parseBackslash() {
int charsMatched = peek(1) == '\n' ? 2 : 1;
appendNode(new HardLineBreak(input.subSequence(index - 1, index + charsMatched)));
index += charsMatched;
gobbleLeadingSpaces();
} else if (index < input.length() && myParsing.ESCAPABLE.matcher(input.subSequence(index, index + 1)).matches()) {
appendText(input, index - 1, index + 1);
index++;
Expand All @@ -550,6 +550,12 @@ protected boolean parseBackslash() {
}
return true;
}

private void gobbleLeadingSpaces() {
while (peek() == ' ') {
index++;
}
}

/**
* Attempt to parse backticks, adding either a backtick code span or a literal sequence of backticks.
Expand Down

0 comments on commit bbcbfcc

Please sign in to comment.