org.commonmark.node.ListItem Java Examples

The following examples show how to use org.commonmark.node.ListItem. 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: CorePlugin.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Override
public void configureSpansFactory(@NonNull MarkwonSpansFactory.Builder builder) {

    // reuse this one for both code-blocks (indent & fenced)
    final CodeBlockSpanFactory codeBlockSpanFactory = new CodeBlockSpanFactory();

    builder
            .setFactory(StrongEmphasis.class, new StrongEmphasisSpanFactory())
            .setFactory(Emphasis.class, new EmphasisSpanFactory())
            .setFactory(BlockQuote.class, new BlockQuoteSpanFactory())
            .setFactory(Code.class, new CodeSpanFactory())
            .setFactory(FencedCodeBlock.class, codeBlockSpanFactory)
            .setFactory(IndentedCodeBlock.class, codeBlockSpanFactory)
            .setFactory(ListItem.class, new ListItemSpanFactory())
            .setFactory(Heading.class, new HeadingSpanFactory())
            .setFactory(Link.class, new LinkSpanFactory())
            .setFactory(ThematicBreak.class, new ThematicBreakSpanFactory());
}
 
Example #2
Source File: MarkwonSpansFactoryImplTest.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Test
public void require_class() {

    // register one TextNode
    final MarkwonSpansFactoryImpl impl = new MarkwonSpansFactoryImpl(
            Collections.<Class<? extends Node>, SpanFactory>singletonMap(Text.class, mock(SpanFactory.class)));

    // text must be present
    assertNotNull(impl.require(Text.class));

    // we haven't registered ListItem, so null here
    try {
        impl.require(ListItem.class);
        fail();
    } catch (NullPointerException e) {
        assertTrue(true);
    }
}
 
Example #3
Source File: ListItemParser.java    From commonmark-java with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Override
public BlockContinue tryContinue(ParserState state) {
    if (state.isBlank()) {
        if (block.getFirstChild() == null) {
            // Blank line after empty list item
            return BlockContinue.none();
        } else {
            Block activeBlock = state.getActiveBlockParser().getBlock();
            // If the active block is a code block, blank lines in it should not affect if the list is tight.
            hadBlankLine = activeBlock instanceof Paragraph || activeBlock instanceof ListItem;
            return BlockContinue.atIndex(state.getNextNonSpaceIndex());
        }
    }

    if (state.getIndent() >= contentIndent) {
        return BlockContinue.atColumn(state.getColumn() + contentIndent);
    } else {
        return BlockContinue.none();
    }
}
 
Example #4
Source File: MarkdownAssert.java    From doov with Apache License 2.0 5 votes vote down vote up
public IntegerAssert countListItem() {
    final AtomicInteger count = new AtomicInteger();
    actual.accept(new AbstractVisitor() {
        @Override
        public void visit(ListItem listItem) {
            count.incrementAndGet();
            super.visit(listItem);
        }
    });
    return new IntegerAssert(count.get());
}
 
Example #5
Source File: CorePlugin.java    From Markwon with Apache License 2.0 5 votes vote down vote up
private static int listLevel(@NonNull Node node) {
    int level = 0;
    Node parent = node.getParent();
    while (parent != null) {
        if (parent instanceof ListItem) {
            level += 1;
        }
        parent = parent.getParent();
    }
    return level;
}
 
Example #6
Source File: MarkwonSpansFactoryTest.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Test
public void builder_get_present() {
    final SpanFactory factory = mock(SpanFactory.class);
    builder.setFactory(ListItem.class, factory);
    assertEquals(factory, builder.getFactory(ListItem.class));
    assertEquals(factory, builder.requireFactory(ListItem.class));
}
 
Example #7
Source File: MarkwonSpansFactoryImplTest.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Test
public void get_class() {

    // register one TextNode
    final MarkwonSpansFactoryImpl impl = new MarkwonSpansFactoryImpl(
            Collections.<Class<? extends Node>, SpanFactory>singletonMap(Text.class, mock(SpanFactory.class)));

    // text must be present
    assertNotNull(impl.get(Text.class));

    // we haven't registered ListItem, so null here
    assertNull(impl.get(ListItem.class));
}
 
Example #8
Source File: TableOfContentsPlugin.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(Heading heading) {
    this.isInsideHeading = true;
    try {
        // reset build from previous content
        builder.setLength(0);

        // obtain level (can additionally filter by level, to skip lower ones)
        final int level = heading.getLevel();

        // build heading title
        visitChildren(heading);

        // initial list item
        final ListItem listItem = new ListItem();

        Node parent = listItem;
        Node node = listItem;

        for (int i = 1; i < level; i++) {
            final ListItem li = new ListItem();
            final BulletList bulletList = new BulletList();
            bulletList.appendChild(li);
            parent.appendChild(bulletList);
            parent = li;
            node = li;
        }

        final String content = builder.toString();
        final Link link = new Link("#" + AnchorHeadingPlugin.createAnchor(content), null);
        final Text text = new Text(content);
        link.appendChild(text);
        node.appendChild(link);
        bulletList.appendChild(listItem);


    } finally {
        isInsideHeading = false;
    }
}
 
Example #9
Source File: BulletListIsOrderedWithLettersWhenNestedPlugin.java    From Markwon with Apache License 2.0 5 votes vote down vote up
@Override
public void configureSpansFactory(@NonNull MarkwonSpansFactory.Builder builder) {
    builder.setFactory(ListItem.class, (configuration, props) -> {
        final Object spans;

        if (CoreProps.ListItemType.BULLET == CoreProps.LIST_ITEM_TYPE.require(props)) {
            final String letter = BULLET_LETTER.get(props);
            if (!TextUtils.isEmpty(letter)) {
                // NB, we are using OrderedListItemSpan here!
                spans = new OrderedListItemSpan(
                        configuration.theme(),
                        letter
                );
            } else {
                spans = new BulletListItemSpan(
                        configuration.theme(),
                        CoreProps.BULLET_LIST_ITEM_LEVEL.require(props)
                );
            }
        } else {

            final String number = String.valueOf(CoreProps.ORDERED_LIST_ITEM_NUMBER.require(props))
                    + "." + '\u00a0';

            spans = new OrderedListItemSpan(
                    configuration.theme(),
                    number
            );
        }

        return spans;
    });
}
 
Example #10
Source File: BulletListIsOrderedWithLettersWhenNestedPlugin.java    From Markwon with Apache License 2.0 5 votes vote down vote up
private static int listLevel(@NonNull Node node) {
    int level = 0;
    Node parent = node.getParent();
    while (parent != null) {
        if (parent instanceof ListItem) {
            level += 1;
        }
        parent = parent.getParent();
    }
    return level;
}
 
Example #11
Source File: MarkwonVisitorImpl.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@Override
public void visit(ListItem listItem) {
    visit((Node) listItem);
}
 
Example #12
Source File: ListHandler.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@Override
public void handle(
        @NonNull MarkwonVisitor visitor,
        @NonNull MarkwonHtmlRenderer renderer,
        @NonNull HtmlTag tag) {

    if (!tag.isBlock()) {
        return;
    }

    final HtmlTag.Block block = tag.getAsBlock();
    final boolean ol = "ol".equals(block.name());
    final boolean ul = "ul".equals(block.name());

    if (!ol && !ul) {
        return;
    }

    final MarkwonConfiguration configuration = visitor.configuration();
    final RenderProps renderProps = visitor.renderProps();
    final SpanFactory spanFactory = configuration.spansFactory().get(ListItem.class);

    int number = 1;
    final int bulletLevel = currentBulletListLevel(block);

    for (HtmlTag.Block child : block.children()) {

        visitChildren(visitor, renderer, child);

        if (spanFactory != null && "li".equals(child.name())) {

            // insert list item here
            if (ol) {
                CoreProps.LIST_ITEM_TYPE.set(renderProps, CoreProps.ListItemType.ORDERED);
                CoreProps.ORDERED_LIST_ITEM_NUMBER.set(renderProps, number++);
            } else {
                CoreProps.LIST_ITEM_TYPE.set(renderProps, CoreProps.ListItemType.BULLET);
                CoreProps.BULLET_LIST_ITEM_LEVEL.set(renderProps, bulletLevel);
            }

            SpannableBuilder.setSpans(
                    visitor.builder(),
                    spanFactory.getSpans(configuration, renderProps),
                    child.start(),
                    child.end());
        }
    }
}
 
Example #13
Source File: BulletListIsOrderedWithLettersWhenNestedPlugin.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@Override
public void configureVisitor(@NonNull MarkwonVisitor.Builder builder) {
    // NB that both ordered and bullet lists are represented
    //  by ListItem (must inspect parent to detect the type)
    builder.on(ListItem.class, (visitor, listItem) -> {
        // mimic original behaviour (copy-pasta from CorePlugin)

        final int length = visitor.length();

        visitor.visitChildren(listItem);

        final Node parent = listItem.getParent();
        if (parent instanceof OrderedList) {

            final int start = ((OrderedList) parent).getStartNumber();

            CoreProps.LIST_ITEM_TYPE.set(visitor.renderProps(), CoreProps.ListItemType.ORDERED);
            CoreProps.ORDERED_LIST_ITEM_NUMBER.set(visitor.renderProps(), start);

            // after we have visited the children increment start number
            final OrderedList orderedList = (OrderedList) parent;
            orderedList.setStartNumber(orderedList.getStartNumber() + 1);

        } else {
            CoreProps.LIST_ITEM_TYPE.set(visitor.renderProps(), CoreProps.ListItemType.BULLET);

            if (isBulletOrdered(parent)) {
                // obtain current count value
                final int count = currentBulletCountIn(parent);
                BULLET_LETTER.set(visitor.renderProps(), createBulletLetter(count));
                // update current count value
                setCurrentBulletCountIn(parent, count + 1);
            } else {
                CoreProps.BULLET_LIST_ITEM_LEVEL.set(visitor.renderProps(), listLevel(listItem));
                // clear letter info when regular bullet list is used
                BULLET_LETTER.clear(visitor.renderProps());
            }
        }

        visitor.setSpansForNodeOptional(listItem, length);

        if (visitor.hasNext(listItem)) {
            visitor.ensureNewLine();
        }
    });
}