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

The following examples show how to use org.commonmark.node.Node#appendChild() . 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: 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 3
Source File: MarkwonInlineParser.java    From Markwon with Apache License 2.0 6 votes vote down vote up
/**
 * Parse content in block into inline children, using reference map to resolve references.
 */
@Override
public void parse(String content, Node block) {
    reset(content.trim());

    // we still reference it
    this.block = block;

    while (true) {
        Node node = parseInline();
        if (node != null) {
            block.appendChild(node);
        } else {
            break;
        }
    }

    processDelimiters(null);
    mergeChildTextNodes(block);
}
 
Example 4
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 5
Source File: EmphasisDelimiterProcessor.java    From commonmark-java with BSD 2-Clause "Simplified" 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 6
Source File: TableBlockParser.java    From js-dossier with Apache License 2.0 6 votes vote down vote up
@Override
public void parseInlines(InlineParser inlineParser) {
  Node headNode = new TableHeadNode();
  block.appendChild(headNode);
  headNode.appendChild(parseRow(headerRow.toString(), inlineParser));

  // The first row of data is always the column alignments, which we've already parsed.
  Node bodyNode = new TableBodyNode();
  block.appendChild(bodyNode);
  String caption = null;
  for (CharSequence line : Iterables.skip(rowData, 1)) {
    Matcher captionMatcher = CAPTION_LINE.matcher(line);
    if (captionMatcher.matches()) {
      caption = captionMatcher.group("content").trim();
    } else {
      TableRowNode row = parseRow(line.toString(), inlineParser);
      bodyNode.appendChild(row);
    }
  }

  if (!isNullOrEmpty(caption)) {
    TableCaptionNode captionNode = new TableCaptionNode();
    headNode.insertBefore(captionNode);
    inlineParser.parse(caption.trim(), captionNode);
  }
}
 
Example 7
Source File: EmojiDelimiterProcessor.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 emoji = new Emoji();
    Node tmp = opener.getNext();
    while (tmp != null && tmp != closer) {
        Node next = tmp.getNext();
        emoji.appendChild(tmp);
        tmp = next;
    }
    opener.insertAfter(emoji);
}
 
Example 8
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 9
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 10
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 11
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 12
Source File: TableOfContentsPlugin.java    From Markwon with Apache License 2.0 4 votes vote down vote up
HeadingVisitor(@NonNull Node node) {
    node.appendChild(bulletList);
}
 
Example 13
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()));
}