use of com.vladsch.flexmark.test.AstCollectingVisitor in project flexmark-java by vsch.
the class FormatConverterCommonMark method main.
public static void main(String[] args) {
final String markdown = "Text\n" + "\n" + "1. numbered list one\n" + " - unnumbered list\n" + " unnumbered list cont. same line\n" + " - unnumbered list \n" + " unnumbered list cont. next line\n" + "\n" + " numbered list one cont. after unnumbered list" + "";
System.out.println("\nMarkdown: --------------------------------------------------------------------------------\n");
System.out.println(markdown);
System.out.println("\n--------------------------------------------------------------------------------\n");
final Parser PARSER = Parser.builder(OPTIONS).build();
final Formatter RENDERER = Formatter.builder(OPTIONS).build();
final Formatter RENDERER_FIXED4 = Formatter.builder(FORMATTER_OPTIONS).build();
Node document = PARSER.parse(markdown);
System.out.println(new AstCollectingVisitor().collectAndGetAstText(document));
System.out.println("\n--------------------------------------------------------------------------------\n");
String formatted = RENDERER.render(document);
// or to control the package
System.out.println("\nFormatted as is: --------------------------------------------------------------------------------\n");
System.out.println(formatted);
System.out.println("\n--------------------------------------------------------------------------------\n");
String formattedFixed4 = RENDERER_FIXED4.render(document);
// or to control the package
System.out.println("\nFormatted fixed 4: --------------------------------------------------------------------------------\n");
System.out.println(formattedFixed4);
System.out.println("\n--------------------------------------------------------------------------------\n");
}
use of com.vladsch.flexmark.test.AstCollectingVisitor in project flexmark-java by vsch.
the class FormatterModifiedAST method test_LinkAnchors.
@Test
public void test_LinkAnchors() throws Exception {
final String input = "" + "- [link](link.txt) and ![image](img.jpg)\n" + "\n" + "next line\n" + "\n";
final String expected = "" + "- [link](replaced.txt#anchor) and ![image](replaced.png)\n" + "\n" + "next line\n" + "\n";
Node document = PARSER.parse(input);
final NodeVisitor[] visitor = new NodeVisitor[1];
visitor[0] = new NodeVisitor(new VisitHandler<Link>(Link.class, new Visitor<Link>() {
@Override
public void visit(Link node) {
FormatterModifiedAST.this.visit(node, "replaced.txt#anchor");
visitor[0].visitChildren(node);
}
}), new VisitHandler<Image>(Image.class, new Visitor<Image>() {
@Override
public void visit(Image node) {
FormatterModifiedAST.this.visit(node, "replaced.png");
visitor[0].visitChildren(node);
}
}));
visitor[0].visit(document);
String ast = new AstCollectingVisitor().collectAndGetAstText(document);
assertEquals("Document[0, 53]\n" + " BulletList[0, 41] isTight\n" + " BulletListItem[0, 41] open:[0, 1, \"-\"] isTight hadBlankLineAfter\n" + " Paragraph[2, 41] isTrailingBlankLine\n" + " Link[2, 17] textOpen:[2, 3, \"[\"] text:[3, 7, \"link\"] textClose:[7, 8, \"]\"] linkOpen:[8, 9, \"(\"] url:[2, 2, \"replaced.txt#anchor\"] pageRef:[2, 2, \"replaced.txt\"] anchorMarker:[2, 2, \"#\"] anchorRef:[2, 2, \"anchor\"] linkClose:[17, 18, \")\"]\n" + " Text[3, 7] chars:[3, 7, \"link\"]\n" + " Text[18, 23] chars:[18, 23, \" and \"]\n" + " Image[23, 39] textOpen:[23, 25, \"![\"] text:[25, 30, \"image\"] textClose:[30, 31, \"]\"] linkOpen:[31, 32, \"(\"] url:[23, 23, \"replaced.png\"] pageRef:[23, 23, \"replaced.png\"] linkClose:[39, 40, \")\"]\n" + " Text[25, 30] chars:[25, 30, \"image\"]\n" + " Paragraph[42, 52] isTrailingBlankLine\n" + " Text[42, 51] chars:[42, 51, \"next line\"]\n", ast);
String formatted = RENDERER.render(document);
assertEquals(expected, formatted);
// System.out.println("input\n");
// System.out.println(input);
// System.out.println("\n\nFormatted\n");
// System.out.println(formatted);
}
use of com.vladsch.flexmark.test.AstCollectingVisitor in project flexmark-java by vsch.
the class TextCollectingVisitorTest method test_basic.
@Test
public void test_basic() {
DataHolder options = new MutableDataSet().set(Parser.EXTENSIONS, Arrays.asList(TablesExtension.create()));
Parser parser = Parser.builder(options).build();
String markdown = "| First Header | Second Header |\n" + "| ------------- | ------------- |\n" + "| Content Cell | Content Cell |\n" + "\n" + "| Left-aligned | Center-aligned | Right-aligned |\n" + "| :--- | :---: | ---: |\n" + "| git status | git status | git status |\n" + "| git diff | git diff | git diff |\n" + "";
Node document = parser.parse(markdown);
TextCollectingVisitor collectingVisitor = new TextCollectingVisitor();
final String text = collectingVisitor.collectAndGetText(document);
System.out.println(text);
final String astText = new AstCollectingVisitor().collectAndGetAstText(document);
System.out.println(astText);
}
Aggregations