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

The following examples show how to use org.commonmark.node.Node#getNext() . 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: EmphasisDelimiterProcessor.java    From 1Rramp-Android with MIT License 6 votes vote down vote up
@Override
public void process(Text opener, Text closer, int delimiterUse) {
    String singleDelimiter = String.valueOf(getOpeningCharacter());
    Node emphasis = delimiterUse == 1
            ? new Emphasis(singleDelimiter)
            : new StrongEmphasis(singleDelimiter + singleDelimiter);

    Node tmp = opener.getNext();
    while (tmp != null && tmp != closer) {
        Node next = tmp.getNext();
        emphasis.appendChild(tmp);
        tmp = next;
    }

    opener.insertAfter(emphasis);
}
 
Example 2
Source File: InlineParserUtils.java    From Markwon with Apache License 2.0 6 votes vote down vote up
public static void mergeIfNeeded(Text first, Text last, int textLength) {
    if (first != null && last != null && first != last) {
        StringBuilder sb = new StringBuilder(textLength);
        sb.append(first.getLiteral());
        Node node = first.getNext();
        Node stop = last.getNext();
        while (node != stop) {
            sb.append(((Text) node).getLiteral());
            Node unlink = node;
            node = node.getNext();
            unlink.unlink();
        }
        String literal = sb.toString();
        first.setLiteral(literal);
    }
}
 
Example 3
Source File: SimpleExtDelimiterProcessor.java    From Markwon with Apache License 2.0 6 votes vote down vote up
@Override
public void process(Text opener, Text closer, int delimiterUse) {

    final Node node = new SimpleExtNode(spanFactory);

    Node tmp = opener.getNext();
    Node next;

    while (tmp != null && tmp != closer) {
        next = tmp.getNext();
        node.appendChild(tmp);
        tmp = next;
    }

    opener.insertAfter(node);
}
 
Example 4
Source File: InsTextContentNodeRenderer.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void renderChildren(Node parent) {
    Node node = parent.getFirstChild();
    while (node != null) {
        Node next = node.getNext();
        context.render(node);
        node = next;
    }
}
 
Example 5
Source File: InsHtmlNodeRenderer.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void renderChildren(Node parent) {
    Node node = parent.getFirstChild();
    while (node != null) {
        Node next = node.getNext();
        context.render(node);
        node = next;
    }
}
 
Example 6
Source File: InsDelimiterProcessor.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void process(Text opener, Text closer, int delimiterCount) {
    // Wrap nodes between delimiters in ins.
    Node ins = new Ins();

    Node tmp = opener.getNext();
    while (tmp != null && tmp != closer) {
        Node next = tmp.getNext();
        ins.appendChild(tmp);
        tmp = next;
    }

    opener.insertAfter(ins);
}
 
Example 7
Source File: StrikethroughTextContentNodeRenderer.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void renderChildren(Node parent) {
    Node node = parent.getFirstChild();
    while (node != null) {
        Node next = node.getNext();
        context.render(node);
        node = next;
    }
}
 
Example 8
Source File: StrikethroughHtmlNodeRenderer.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void renderChildren(Node parent) {
    Node node = parent.getFirstChild();
    while (node != null) {
        Node next = node.getNext();
        context.render(node);
        node = next;
    }
}
 
Example 9
Source File: StrikethroughDelimiterProcessor.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void process(Text opener, Text closer, int delimiterCount) {
    // Wrap nodes between delimiters in strikethrough.
    Node strikethrough = new Strikethrough();

    Node tmp = opener.getNext();
    while (tmp != null && tmp != closer) {
        Node next = tmp.getNext();
        strikethrough.appendChild(tmp);
        tmp = next;
    }

    opener.insertAfter(strikethrough);
}
 
Example 10
Source File: TaskListItemHtmlNodeRenderer.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void renderChildren(Node parent) {
    Node node = parent.getFirstChild();
    while (node != null) {
        Node next = node.getNext();
        context.render(node);
        node = next;
    }
}
 
Example 11
Source File: InlineParserUtils.java    From Markwon with Apache License 2.0 5 votes vote down vote up
public static void mergeTextNodesBetweenExclusive(Node fromNode, Node toNode) {
    // No nodes between them
    if (fromNode == toNode || fromNode.getNext() == toNode) {
        return;
    }

    mergeTextNodesInclusive(fromNode.getNext(), toNode.getPrevious());
}
 
Example 12
Source File: TableHtmlNodeRenderer.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void renderChildren(Node parent) {
    Node node = parent.getFirstChild();
    while (node != null) {
        Node next = node.getNext();
        context.render(node);
        node = next;
    }
}
 
Example 13
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 14
Source File: DelimiterProcessorTest.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Override
public void process(Text opener, Text closer, int delimiterUse) {
    UpperCaseNode content = new UpperCaseNode();
    Node tmp = opener.getNext();
    while (tmp != null && tmp != closer) {
        Node next = tmp.getNext();
        content.appendChild(tmp);
        tmp = next;
    }
    opener.insertAfter(content);
}
 
Example 15
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 16
Source File: MentionDelimiterProcessor.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
@Override public void process(Text opener, Text closer, int delimiterCount) {
    Node mention = new Mention();
    Node tmp = opener.getNext();
    while (tmp != null && tmp != closer) {
        Node next = tmp.getNext();
        mention.appendChild(tmp);
        tmp = next;
    }
    opener.insertAfter(mention);
}
 
Example 17
Source File: TableTextContentNodeRenderer.java    From commonmark-java with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private void renderChildren(Node parent) {
    Node node = parent.getFirstChild();
    while (node != null) {
        Node next = node.getNext();

        // For last cell in row, we dont render the delimiter.
        if (node instanceof TableCell && next == null) {
            renderLastCell((TableCell) node);
        } else {
            context.render(node);
        }

        node = next;
    }
}
 
Example 18
Source File: EmojiNodeRenderer.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
private void renderChildren(Node parent) {
    Node node = parent.getFirstChild();
    while (node != null) {
        Node next = node.getNext();
        context.render(node);
        node = next;
    }
}
 
Example 19
Source File: IconProcessor.java    From Markwon with Apache License 2.0 4 votes vote down vote up
@Override
public void process(Text opener, Text closer, int delimiterUse) {

    final IconGroupNode iconGroupNode = new IconGroupNode();

    final Node next = opener.getNext();

    boolean handled = false;

    // process only if we have exactly one Text node
    if (next instanceof Text && next.getNext() == closer) {

        final String text = ((Text) next).getLiteral();

        if (!TextUtils.isEmpty(text)) {

            // attempt to match
            final Matcher matcher = PATTERN.matcher(text);
            if (matcher.matches()) {
                final IconNode iconNode = new IconNode(
                        matcher.group(1),
                        matcher.group(2),
                        matcher.group(3)
                );
                iconGroupNode.appendChild(iconNode);
                next.unlink();
                handled = true;
            }
        }
    }

    if (!handled) {

        // restore delimiters if we didn't match

        iconGroupNode.appendChild(new Text(IconNode.DELIMITER_STRING));

        Node node;
        for (Node tmp = opener.getNext(); tmp != null && tmp != closer; tmp = node) {
            node = tmp.getNext();
            // append a child anyway
            iconGroupNode.appendChild(tmp);
        }

        iconGroupNode.appendChild(new Text(IconNode.DELIMITER_STRING));
    }

    opener.insertBefore(iconGroupNode);
}
 
Example 20
Source File: ImageAttributesDelimiterProcessor.java    From commonmark-java with BSD 2-Clause "Simplified" License 4 votes vote down vote up
@Override
public void process(Text opener, Text closer, int delimiterCount) {
    // Check if the attributes can be applied - if the previous node is an Image, and if all the attributes are in
    // the set of SUPPORTED_ATTRIBUTES
    if (opener.getPrevious() instanceof Image) {
        boolean canApply = true;
        List<Node> toUnlink = new ArrayList<>();

        Map<String, String> attributesMap = new LinkedHashMap<>();
        Node tmp = opener.getNext();
        while (tmp != null && tmp != closer) {
            Node next = tmp.getNext();
            // Only Text nodes can be used for attributes
            if (tmp instanceof Text) {
                String attributes = ((Text) tmp).getLiteral();
                for (String s : attributes.split("\\s+")) {
                    String[] attribute = s.split("=");
                    if (attribute.length > 1 && SUPPORTED_ATTRIBUTES.contains(attribute[0].toLowerCase())) {
                        attributesMap.put(attribute[0], attribute[1]);
                        // The tmp node can be unlinked, as we have retrieved its value.
                        toUnlink.add(tmp);
                    } else {
                        // This attribute is not supported, so break here (no need to check any further ones).
                        canApply = false;
                        break;
                    }
                }
            } else {
                // This node type is not supported, so break here (no need to check any further ones).
                canApply = false;
                break;
            }
            tmp = next;
        }

        // Only if all of the above checks pass can the attributes be applied.
        if (canApply) {
            // Unlink the tmp nodes
            for (Node node : toUnlink) {
                node.unlink();
            }

            if (attributesMap.size() > 0) {
                ImageAttributes imageAttributes = new ImageAttributes(attributesMap);

                // The new node is added as a child of the image node to which the attributes apply.
                Node nodeToStyle = opener.getPrevious();
                nodeToStyle.appendChild(imageAttributes);
            }
            return;
        }
    }

    // If we got here then the attributes cannot be applied, so fallback to leaving the text unchanged.
    // Need to add back the opening and closing characters (which are removed elsewhere).
    if (opener.getPrevious() == null) {
        opener.getParent().prependChild(new Text("" + getOpeningCharacter()));
    } else {
        opener.getPrevious().insertAfter(new Text("" + getOpeningCharacter()));
    }
    closer.getParent().appendChild(new Text("" + getClosingCharacter()));
}