Java Code Examples for org.commonmark.node.Node#accept()

The following examples show how to use org.commonmark.node.Node#accept() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: YamlFrontMatterTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void nonMatchedStartTag() {
    final String input = "----\n" +
            "test";
    final String rendered = "<hr />\n<p>test</p>\n";

    YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
    Node document = PARSER.parse(input);
    document.accept(visitor);

    Map<String, List<String>> data = visitor.getData();

    assertTrue(data.isEmpty());

    assertRendering(input, rendered);
}
 
Example 2
Source File: YamlFrontMatterTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void yamlInParagraph() {
    final String input = "# hello\n" +
            "\nhello markdown world!" +
            "\n---" +
            "\nhello: world" +
            "\n---";
    final String rendered = "<h1>hello</h1>\n<h2>hello markdown world!</h2>\n<h2>hello: world</h2>\n";

    YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
    Node document = PARSER.parse(input);
    document.accept(visitor);

    Map<String, List<String>> data = visitor.getData();

    assertTrue(data.isEmpty());

    assertRendering(input, rendered);
}
 
Example 3
Source File: CommonmarkPreviewRenderer.java    From markdown-writer-fx with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public List<Range> findSequences(int startOffset, int endOffset) {
	ArrayList<Range> sequences = new ArrayList<>();

	Node astRoot = toAstRoot();
	if (astRoot == null)
		return sequences;

	Visitor visitor = new AbstractVisitor() {
		@Override
		protected void visitChildren(Node node) {
			Range range = toSourcePositions().get(node);
			if (range != null && isInRange(startOffset, endOffset, range))
				sequences.add(range);

			super.visitChildren(node);
		}
	};
	astRoot.accept(visitor);
	return sequences;
}
 
Example 4
Source File: TableOfContentsPlugin.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeRender(@NonNull Node node) {

    // custom block to hold TOC
    final TableOfContentsBlock block = new TableOfContentsBlock();

    // create TOC title
    {
        final Text text = new Text("Table of contents");
        final Heading heading = new Heading();
        // important one - set TOC heading level
        heading.setLevel(1);
        heading.appendChild(text);
        block.appendChild(heading);
    }

    final HeadingVisitor visitor = new HeadingVisitor(block);
    node.accept(visitor);

    // make it the very first node in rendered markdown
    node.prependChild(block);
}
 
Example 5
Source File: YamlFrontMatterTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void visitorIgnoresOtherCustomNodes() {
    final String input = "---" +
            "\nhello: world" +
            "\n---" +
            "\n";

    YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
    Node document = PARSER.parse(input);
    document.appendChild(new TestNode());
    document.accept(visitor);

    Map<String, List<String>> data = visitor.getData();
    assertEquals(1, data.size());
    assertTrue(data.containsKey("hello"));
    assertEquals(Collections.singletonList("world"), data.get("hello"));
}
 
Example 6
Source File: YamlFrontMatterTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void literalValue2() {
    final String input = "---" +
            "\nliteral: |" +
            "\n  - hello markdown!" +
            "\n---" +
            "\n" +
            "\ngreat";
    final String rendered = "<p>great</p>\n";

    YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
    Node document = PARSER.parse(input);
    document.accept(visitor);

    Map<String, List<String>> data = visitor.getData();

    assertEquals(1, data.size());
    assertTrue(data.containsKey("literal"));
    assertEquals(1, data.get("literal").size());
    assertEquals("- hello markdown!", data.get("literal").get(0));

    assertRendering(input, rendered);
}
 
Example 7
Source File: YamlFrontMatterTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void simpleValue() {
    final String input = "---" +
            "\nhello: world" +
            "\n..." +
            "\n" +
            "\ngreat";
    final String rendered = "<p>great</p>\n";

    YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
    Node document = PARSER.parse(input);
    document.accept(visitor);

    Map<String, List<String>> data = visitor.getData();

    assertEquals(1, data.size());
    assertEquals("hello", data.keySet().iterator().next());
    assertEquals(1, data.get("hello").size());
    assertEquals("world", data.get("hello").get(0));

    assertRendering(input, rendered);
}
 
Example 8
Source File: YamlFrontMatterTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void emptyValue() {
    final String input = "---" +
            "\nkey:" +
            "\n---" +
            "\n" +
            "\ngreat";
    final String rendered = "<p>great</p>\n";

    YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
    Node document = PARSER.parse(input);
    document.accept(visitor);

    Map<String, List<String>> data = visitor.getData();

    assertEquals(1, data.size());
    assertEquals("key", data.keySet().iterator().next());
    assertEquals(0, data.get("key").size());

    assertRendering(input, rendered);
}
 
Example 9
Source File: YamlFrontMatterTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void listValues() {
    final String input = "---" +
            "\nlist:" +
            "\n  - value1" +
            "\n  - value2" +
            "\n..." +
            "\n" +
            "\ngreat";
    final String rendered = "<p>great</p>\n";

    YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
    Node document = PARSER.parse(input);
    document.accept(visitor);

    Map<String, List<String>> data = visitor.getData();

    assertEquals(1, data.size());
    assertTrue(data.containsKey("list"));
    assertEquals(2, data.get("list").size());
    assertEquals("value1", data.get("list").get(0));
    assertEquals("value2", data.get("list").get(1));

    assertRendering(input, rendered);
}
 
Example 10
Source File: YamlFrontMatterTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Test
public void literalValue1() {
    final String input = "---" +
            "\nliteral: |" +
            "\n  hello markdown!" +
            "\n  literal thing..." +
            "\n---" +
            "\n" +
            "\ngreat";
    final String rendered = "<p>great</p>\n";

    YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
    Node document = PARSER.parse(input);
    document.accept(visitor);

    Map<String, List<String>> data = visitor.getData();

    assertEquals(1, data.size());
    assertTrue(data.containsKey("literal"));
    assertEquals(1, data.get("literal").size());
    assertEquals("hello markdown!\nliteral thing...", data.get("literal").get(0));

    assertRendering(input, rendered);
}
 
Example 11
Source File: MarkdownUtils.java    From blog-sharon with Apache License 2.0 5 votes vote down vote up
/**
 * 获取元数据
 *
 * @param content content
 * @return Map
 */
public static Map<String, List<String>> getFrontMatter(String content) {
    YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
    Node document = PARSER.parse(content);
    document.accept(visitor);
    return visitor.getData();
}
 
Example 12
Source File: DocMaker.java    From jeka with Apache License 2.0 5 votes vote down vote up
private List<MenuItem> addAnchorAndNumberingToHeaders(Node node) {
    final List<MenuItem> menuItems = new LinkedList<>();
    final int[] counters = new int[10];
    node.accept(new AbstractVisitor() {

        @Override
        public void visit(Heading heading) {
            final Text text = (Text) heading.getFirstChild();
            final String content = text.getLiteral();
            final boolean intro = "Introduction".equals(content);  // Do not number Introduction
            if (!intro) {
                counters[heading.getLevel()]++;
                for (int i = heading.getLevel() + 1; i < 6; i++) {
                    counters[i] = 0;
                }
            }
            final StringBuilder sb = new StringBuilder();
            for (int i = 1; i <= heading.getLevel(); i++) {
                sb.append(counters[i]).append(".");
            }
            if (sb.length() > 1 && heading.getLevel() > 1) {
                sb.delete(sb.length() - 1, sb.length() );
            }
            final String anchorId = content.replace(" ", "");
            final HtmlInline htmlInline = new HtmlInline();
            htmlInline.setLiteral("<a name=\"" + anchorId + "\"></a>");
            heading.insertBefore(htmlInline);
            final MenuItem menuItem = new MenuItem(content, anchorId, heading.getLevel());
            menuItems.add(menuItem);
        }
    });
    return menuItems;
}
 
Example 13
Source File: YamlFrontMatterTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Test
public void complexValues() {
    final String input = "---" +
            "\nsimple: value" +
            "\nliteral: |" +
            "\n  hello markdown!" +
            "\n" +
            "\n  literal literal" +
            "\nlist:" +
            "\n    - value1" +
            "\n    - value2" +
            "\n---" +
            "\ngreat";
    final String rendered = "<p>great</p>\n";

    YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
    Node document = PARSER.parse(input);
    document.accept(visitor);

    Map<String, List<String>> data = visitor.getData();

    assertEquals(3, data.size());

    assertTrue(data.containsKey("simple"));
    assertEquals(1, data.get("simple").size());
    assertEquals("value", data.get("simple").get(0));

    assertTrue(data.containsKey("literal"));
    assertEquals(1, data.get("literal").size());
    assertEquals("hello markdown!\n\nliteral literal", data.get("literal").get(0));

    assertTrue(data.containsKey("list"));
    assertEquals(2, data.get("list").size());
    assertEquals("value1", data.get("list").get(0));
    assertEquals("value2", data.get("list").get(1));

    assertRendering(input, rendered);
}
 
Example 14
Source File: MarkDownParser.java    From blog-app-android with MIT License 5 votes vote down vote up
/**
 * Parse a given simple text and return html notation of it.
 *
 * @param toParse the string containing Markdown markup.
 * @return a String containing html representation of input, or null on error.
 */
public String parse(String toParse) {
  if (toParse == null) {
    return null;
  }

  final Node node = parser.parse(toParse);
  node.accept(simplifyInlineMarkdown);

  return renderer.render(node);
}
 
Example 15
Source File: MarkdownUtils.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * 获取元数据
 *
 * @param content content
 * @return Map
 */
public static Map<String, List<String>> getFrontMatter(String content) {
    YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
    Node document = PARSER.parse(content);
    document.accept(visitor);
    return visitor.getData();
}
 
Example 16
Source File: MarkwonVisitorImpl.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Override
public void visitChildren(@NonNull Node parent) {
    Node node = parent.getFirstChild();
    while (node != null) {
        // A subclass of this visitor might modify the node, resulting in getNext returning a different node or no
        // node after visiting it. So get the next node before visiting.
        Node next = node.getNext();
        node.accept(this);
        node = next;
    }
}
 
Example 17
Source File: DumpNodes.java    From Markwon with Apache License 2.0 5 votes vote down vote up
private static void visitChildren(@NonNull Visitor visitor, @NonNull Node parent) {
    Node node = parent.getFirstChild();
    while (node != null) {
        // A subclass of this visitor might modify the node, resulting in getNext returning a different node or no
        // node after visiting it. So get the next node before visiting.
        Node next = node.getNext();
        node.accept(visitor);
        node = next;
    }
}
 
Example 18
Source File: DumpNodes.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@NonNull
public static String dump(@NonNull Node node, @Nullable NodeProcessor nodeProcessor) {

    final NodeProcessor processor = nodeProcessor != null
            ? nodeProcessor
            : new NodeProcessorToString();

    final Indent indent = new Indent();
    final StringBuilder builder = new StringBuilder();
    final Visitor visitor = (Visitor) Proxy.newProxyInstance(
            Visitor.class.getClassLoader(),
            new Class[]{Visitor.class},
            new InvocationHandler() {
                @Override
                public Object invoke(Object proxy, Method method, Object[] args) {

                    final Node argument = (Node) args[0];

                    // initial indent
                    indent.appendTo(builder);

                    // node info
                    builder.append(processor.process(argument));

                    if (argument instanceof Block) {
                        builder.append(" [\n");
                        indent.increment();
                        visitChildren((Visitor) proxy, argument);
                        indent.decrement();
                        indent.appendTo(builder);
                        builder.append("]\n");
                    } else {
                        builder.append('\n');
                    }
                    return null;
                }
            });
    node.accept(visitor);
    return builder.toString();
}
 
Example 19
Source File: AutolinkPostProcessor.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public Node process(Node node) {
    AutolinkVisitor autolinkVisitor = new AutolinkVisitor();
    node.accept(autolinkVisitor);
    return node;
}
 
Example 20
Source File: MarkdownUtils.java    From stone with GNU General Public License v3.0 3 votes vote down vote up
/**
 * 获取元数据
 *
 * @param content content
 *
 * @return Map
 */
public static Map<String, List<String>> getFrontMatter(String content) {
    final YamlFrontMatterVisitor visitor = new YamlFrontMatterVisitor();
    final Node document = PARSER.parse(content);
    document.accept(visitor);
    return visitor.getData();
}