Skip to content

Commit

Permalink
Add handling of line blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
takahi-i committed May 4, 2017
1 parent eb5eee7 commit b7199a3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
31 changes: 28 additions & 3 deletions redpen-core/src/main/java/cc/redpen/parser/rest/ReSTParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,28 @@ private void processLine(Line line, Model model, State state) {
if (isDirective(target, state)) { state.inBlock = true; }

// handle source codes (literal blocks)
if (isLiteral(target, state)) { state.inBlock = true; }

// handle comments
if (isComment(target, state)) { state.inBlock = true; }

// handle block quotes

// handle line blocks
// handle line block
handlelineBlock(line); //NOTE: line block does not effect upcoming lines

// handle footnotes

// a blank line will reset any blocks element we are in
if (isEndBlock(target)) { reset(state); }
}

private void handlelineBlock(Line line) {
if (line.charAt(0) == '|' && line.charAt(1) == ' ') {
line.erase();
} else if (line.startsWith(">>>")) {
line.erase();
}
}

private boolean isEndBlock(TargetLine target) {
if (target.line.length() == 0 &&
((target.nextLine.charAt(0) != ' ' && target.nextLine.charAt(1) != ' ')
Expand All @@ -129,6 +137,23 @@ private boolean isEndBlock(TargetLine target) {
return false;
}

private boolean isLiteral(TargetLine target, State state) {
Line line = target.line;
// check if in comment?
if (state.inBlock) {
line.erase();
return true;
}

// check if block comment start?
if (line.length() == 2 && (line.charAt(0) == ':' && line.charAt(1) == ':')
&& target.nextLine.length() == 0) {
return true;
}
return false;
}


private boolean isComment(TargetLine target, State state) {
Line line = target.line;
// check if in comment?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,51 @@ public void testComments() {
assertEquals("Finished a comment yay!", section.getParagraph(1).getSentence(0).getContent());
}

@Test
public void testLiterals() {
String sampleText = "Before literals.\n";
sampleText += "\n";
sampleText += "::\n" +
"\n" +
" This is in a literal block.\n" +
"\n" +
"Finished a literal yay!";

Document doc = createFileContent(sampleText);
assertNotNull("doc is null", doc);
assertEquals(1, doc.size());

Section section = doc.getSection(0);
assertEquals(2, section.getParagraphs().size());
assertEquals(1, section.getParagraph(0).getNumberOfSentences());
assertEquals("Before literals.", section.getParagraph(0).getSentence(0).getContent());
assertEquals(1, section.getParagraph(1).getNumberOfSentences());
assertEquals("Finished a literal yay!", section.getParagraph(1).getSentence(0).getContent());
}

@Test
public void testLineBlock() {
String sampleText = "Before line block.\n";
sampleText += "\n";
sampleText += "::\n" +
"\n" +
"| This is in a line block.\n" +
"| This is also in a line block.\n" +
"\n" +
"Finished a line block yay!";

Document doc = createFileContent(sampleText);
assertNotNull("doc is null", doc);
assertEquals(1, doc.size());

Section section = doc.getSection(0);
assertEquals(2, section.getParagraphs().size());
assertEquals(1, section.getParagraph(0).getNumberOfSentences());
assertEquals("Before line block.", section.getParagraph(0).getSentence(0).getContent());
assertEquals(1, section.getParagraph(1).getNumberOfSentences());
assertEquals("Finished a line block yay!", section.getParagraph(1).getSentence(0).getContent());
}

private Document createFileContent(String inputDocumentString) {
DocumentParser parser = DocumentParser.REST;
Document doc = null;
Expand Down

0 comments on commit b7199a3

Please sign in to comment.