org.commonmark.node.Visitor Java Examples

The following examples show how to use org.commonmark.node.Visitor. 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: MarkwonVisitorImplTest.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Test
public void all_known_nodes_visit_methods_are_overridden() {
    // checks that all methods from Visitor (commonmark-java) interface are implemented

    final List<Method> methods = Ix.fromArray(Visitor.class.getDeclaredMethods())
            .filter(new IxPredicate<Method>() {

                @Override
                public boolean test(Method method) {

                    // if it's present in our impl -> remove
                    // else keep (to report)

                    try {
                        MarkwonVisitorImpl.class
                                .getDeclaredMethod(method.getName(), method.getParameterTypes());
                        return false;
                    } catch (NoSuchMethodException e) {
                        return true;
                    }
                }
            })
            .toList();

    assertEquals(methods.toString(), 0, methods.size());
}
 
Example #2
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 #3
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 #4
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 #5
Source File: MarkwonImplTest.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@Test
public void render_calls_plugins() {
    // before parsing each plugin is called `configureRenderProps` and `beforeRender`
    // after parsing each plugin is called `afterRender`

    final MarkwonPlugin plugin = mock(MarkwonPlugin.class);

    final MarkwonVisitorFactory visitorFactory = mock(MarkwonVisitorFactory.class);
    final MarkwonVisitor visitor = mock(MarkwonVisitor.class);
    final SpannableBuilder builder = mock(SpannableBuilder.class);

    final MarkwonImpl impl = new MarkwonImpl(
            TextView.BufferType.SPANNABLE,
            null,
            mock(Parser.class),
            visitorFactory,
            mock(MarkwonConfiguration.class),
            Collections.singletonList(plugin),
            true
    );

    when(visitorFactory.create()).thenReturn(visitor);
    when(visitor.builder()).thenReturn(builder);

    final Node node = mock(Node.class);

    final AtomicBoolean flag = new AtomicBoolean(false);

    // we will validate _before_ part here
    doAnswer(new Answer() {
        @Override
        public Object answer(InvocationOnMock invocation) {

            // mark this flag (we must ensure that this method body is executed)
            flag.set(true);

            verify(plugin, times(1)).beforeRender(eq(node));
            verify(plugin, times(0)).afterRender(any(Node.class), any(MarkwonVisitor.class));

            return null;
        }
    }).when(node).accept(any(Visitor.class));

    impl.render(node);

    // validate that Answer was called (it has assertions about _before_ part
    assertTrue(flag.get());

    verify(plugin, times(1)).afterRender(eq(node), eq(visitor));
}
 
Example #6
Source File: SimpleExtNode.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@Override
public void accept(Visitor visitor) {
    visitor.visit(this);
}
 
Example #7
Source File: NoticeBlock.java    From maven-confluence-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void accept(Visitor visitor) {
    visitor.visit(this);
}