-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
173 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package markdown; | ||
|
||
import com.vladsch.flexmark.ast.Document; | ||
import com.vladsch.flexmark.ast.Node; | ||
import com.vladsch.flexmark.ast.Paragraph; | ||
import com.vladsch.flexmark.ast.Text; | ||
import com.vladsch.flexmark.util.sequence.BasedSequence; | ||
import org.languagetool.markup.AnnotatedText; | ||
import org.languagetool.markup.AnnotatedTextBuilder; | ||
|
||
public class AnnotatedTextBuildingVisitor { | ||
private AnnotatedTextBuilder builder = new AnnotatedTextBuilder(); | ||
|
||
private int idx = 0; | ||
|
||
private boolean inParagraph = false; | ||
|
||
public void visit(Document node) { | ||
visitChildren(node); | ||
} | ||
|
||
private void visitChildren(final Node parent) { | ||
parent.getChildren().forEach(this::visit); | ||
} | ||
|
||
private void visit(Node node) { | ||
if (node.getClass() == Paragraph.class) { | ||
BasedSequence originalText = node.getChars().getBaseSequence(); | ||
BasedSequence passedOver = originalText.subSequence(idx, node.getStartOffset()); | ||
|
||
if (inParagraph) { | ||
throw new UnsupportedOperationException("Nested paragraphs are not supported"); | ||
} | ||
|
||
processProceedingCharacters(passedOver); | ||
|
||
idx = node.getStartOffset(); | ||
|
||
inParagraph = true; | ||
visitChildren(node); | ||
inParagraph = false; | ||
} else if (node.getClass() == Text.class) { | ||
BasedSequence originalText = node.getChars().getBaseSequence(); | ||
BasedSequence passedOver = originalText.subSequence(idx, node.getStartOffset()); | ||
|
||
processProceedingCharacters(passedOver); | ||
|
||
idx = node.getEndOffset(); | ||
|
||
builder.addText(node.getChars().toString()); | ||
} else { | ||
visitChildren(node); | ||
} | ||
} | ||
|
||
private void processProceedingCharacters(BasedSequence passedOver) { | ||
for (char c : passedOver.toString().toCharArray()) { | ||
if (c == '\r' || c == '\n') { | ||
if (inParagraph) { | ||
builder.addText(" "); | ||
} else { | ||
builder.addText(new String(new char[]{c})); | ||
} | ||
} else { | ||
builder.addMarkup(new String(new char[]{c})); | ||
} | ||
} | ||
} | ||
|
||
public AnnotatedText getText() { | ||
return builder.build(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/test/java/markdown/AnnotatedTextBuildingVisitorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package markdown; | ||
|
||
import com.vladsch.flexmark.ast.Document; | ||
import com.vladsch.flexmark.ast.Node; | ||
import com.vladsch.flexmark.parser.Parser; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.languagetool.markup.AnnotatedText; | ||
|
||
import java.io.IOException; | ||
|
||
public class AnnotatedTextBuildingVisitorTest { | ||
@Test | ||
void test() throws IOException { | ||
Parser p = Parser.builder().build(); | ||
|
||
Node document = p.parse("# Heading\n" + | ||
"Paragraph with\n" + | ||
"multiple lines and [link](example.com)"); | ||
|
||
AnnotatedTextBuildingVisitor visitor = new AnnotatedTextBuildingVisitor(); | ||
visitor.visit((Document) document); | ||
AnnotatedText text = visitor.getText(); | ||
|
||
Assertions.assertEquals("Heading\nParagraph with multiple lines and link", text.getPlainText()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package markdown; | ||
|
||
import com.vladsch.flexmark.ast.Node; | ||
|
||
class PrintAstVisitor { | ||
public void visit(Node node, int indent) { | ||
String i = ""; | ||
|
||
for (int j = 0; j < indent; j++) { | ||
i += " "; | ||
} | ||
|
||
System.out.println(i + node.toAstString(true)); | ||
|
||
node.getChildIterator().forEachRemaining(n -> this.visit(n, indent + 1)); | ||
} | ||
} |