Search in sources :

Example 1 with TextCollectingVisitor

use of com.vladsch.flexmark.ast.util.TextCollectingVisitor in project flexmark-java by vsch.

the class CoreNodeDocxRenderer method render.

private void render(final ImageRef node, final DocxRendererContext docx) {
    ResolvedLink resolvedLink = null;
    if (!node.isDefined() && recheckUndefinedReferences) {
        if (node.getReferenceNode(referenceRepository) != null) {
            node.setDefined(true);
        }
    }
    Reference reference = null;
    if (node.isDefined()) {
        reference = node.getReferenceNode(referenceRepository);
        String url = reference.getUrl().unescape();
        resolvedLink = docx.resolveLink(LinkType.IMAGE, url, null, null);
        if (reference.getTitle().isNotNull()) {
            resolvedLink.getNonNullAttributes().replaceValue(Attribute.TITLE_ATTR, reference.getTitle().unescape());
        } else {
            resolvedLink.getNonNullAttributes().remove(Attribute.TITLE_ATTR);
        }
    } else {
        // see if have reference resolver and this is resolved
        String normalizeRef = referenceRepository.normalizeKey(node.getReference());
        resolvedLink = docx.resolveLink(LinkType.IMAGE_REF, normalizeRef, null, null);
        if (resolvedLink.getStatus() == UNKNOWN) {
            resolvedLink = null;
        }
    }
    if (resolvedLink == null) {
        // empty ref, we treat it as text
        docx.text(node.getChars().unescape());
        if (options.logImageProcessing) {
            System.out.println("render image ref of " + referenceRepository.normalizeKey(node.getReference()) + " skipped because it was not defined");
        }
    } else {
        String altText = new TextCollectingVisitor().collectAndGetText(node);
        String url = resolvedLink.getUrl();
        Attributes attributes = resolvedLink.getNonNullAttributes();
        if (!altText.isEmpty()) {
            attributes.replaceValue("alt", altText);
        }
        if (reference != null) {
            attributes = docx.extendRenderingNodeAttributes(reference, AttributablePart.NODE, attributes);
        }
        attributes = docx.extendRenderingNodeAttributes(AttributablePart.NODE, attributes);
        renderImage(docx, url, attributes);
    }
}
Also used : EnumRefTextCollectingVisitor(com.vladsch.flexmark.ext.enumerated.reference.internal.EnumRefTextCollectingVisitor) TextCollectingVisitor(com.vladsch.flexmark.ast.util.TextCollectingVisitor) Attributes(com.vladsch.flexmark.util.html.Attributes) ResolvedLink(com.vladsch.flexmark.html.renderer.ResolvedLink)

Example 2 with TextCollectingVisitor

use of com.vladsch.flexmark.ast.util.TextCollectingVisitor in project flexmark-java by vsch.

the class CoreNodeDocxRenderer method render.

private void render(final Image node, final DocxRendererContext docx) {
    String altText = new TextCollectingVisitor().collectAndGetText(node);
    ResolvedLink resolvedLink = docx.resolveLink(LinkType.IMAGE, node.getUrl().unescape(), null, null);
    String url = resolvedLink.getUrl();
    Attributes attributes = resolvedLink.getNonNullAttributes();
    if (!node.getUrlContent().isEmpty()) {
        // reverse URL encoding of =, &
        String content = Escaping.percentEncodeUrl(node.getUrlContent()).replace("+", "%2B").replace("%3D", "=").replace("%26", "&");
        url += content;
    }
    if (!altText.isEmpty()) {
        attributes.replaceValue("alt", altText);
    }
    attributes = docx.extendRenderingNodeAttributes(AttributablePart.NODE, attributes);
    // String alt = node.getText().unescape();
    renderImage(docx, url, attributes);
}
Also used : EnumRefTextCollectingVisitor(com.vladsch.flexmark.ext.enumerated.reference.internal.EnumRefTextCollectingVisitor) TextCollectingVisitor(com.vladsch.flexmark.ast.util.TextCollectingVisitor) Attributes(com.vladsch.flexmark.util.html.Attributes) ResolvedLink(com.vladsch.flexmark.html.renderer.ResolvedLink)

Example 3 with TextCollectingVisitor

use of com.vladsch.flexmark.ast.util.TextCollectingVisitor in project flexmark-java by vsch.

the class MarkdownToText method main.

// use the PARSER to parse and RENDERER to parse pegdown indentation rules and render CommonMark
public static void main(String[] args) {
    final String pegdown = "#Heading\n" + "-----\n" + "paragraph text \n" + "lazy continuation\n" + "\n" + "* list item\n" + "    > block quote\n" + "    lazy continuation\n" + "\n" + "~~~info\n" + "        with uneven indent\n" + "           with uneven indent\n" + "     indented code\n" + "~~~ \n" + "\n" + "        with uneven indent\n" + "           with uneven indent\n" + "     indented code\n" + "1. numbered item 1   \n" + "1. numbered item 2   \n" + "1. numbered item 3   \n" + "    - bullet item 1   \n" + "    - bullet item 2   \n" + "    - bullet item 3   \n" + "        1. numbered sub-item 1   \n" + "        1. numbered sub-item 2   \n" + "        1. numbered sub-item 3   \n" + "    \n" + "    ~~~info\n" + "            with uneven indent\n" + "               with uneven indent\n" + "         indented code\n" + "    ~~~ \n" + "    \n" + "            with uneven indent\n" + "               with uneven indent\n" + "         indented code\n" + "";
    System.out.println("pegdown\n");
    System.out.println(pegdown);
    Node document = PARSER.parse(pegdown);
    TextCollectingVisitor textCollectingVisitor = new TextCollectingVisitor();
    String text = textCollectingVisitor.collectAndGetText(document);
    System.out.println("\n\nText\n");
    System.out.println(text);
}
Also used : TextCollectingVisitor(com.vladsch.flexmark.ast.util.TextCollectingVisitor) Node(com.vladsch.flexmark.ast.Node)

Example 4 with TextCollectingVisitor

use of com.vladsch.flexmark.ast.util.TextCollectingVisitor in project flexmark-java by vsch.

the class TitleExtract method findTitle.

private static String findTitle(Node root) {
    if (root instanceof Heading) {
        Heading h = (Heading) root;
        if (h.getLevel() == 1 && h.hasChildren()) {
            TextCollectingVisitor collectingVisitor = new TextCollectingVisitor();
            return collectingVisitor.collectAndGetText(h);
        }
    }
    if (root instanceof Block && root.hasChildren()) {
        Node child = root.getFirstChild();
        while (child != null) {
            String title = findTitle(child);
            if (title != null) {
                return title;
            }
            child = child.getNext();
        }
    }
    return null;
}
Also used : TextCollectingVisitor(com.vladsch.flexmark.ast.util.TextCollectingVisitor) Heading(com.vladsch.flexmark.ast.Heading) Node(com.vladsch.flexmark.ast.Node) Block(com.vladsch.flexmark.ast.Block)

Example 5 with TextCollectingVisitor

use of com.vladsch.flexmark.ast.util.TextCollectingVisitor in project flexmark-java by vsch.

the class JiraConverterNodeRenderer method render.

private void render(Image node, NodeRendererContext context, HtmlWriter html) {
    if (!context.isDoNotRenderLinks()) {
        String altText = new TextCollectingVisitor().collectAndGetText(node);
        ResolvedLink resolvedLink = context.resolveLink(LinkType.IMAGE, node.getUrl().unescape(), null);
        html.raw("!").raw(resolvedLink.getUrl()).raw("!");
    }
}
Also used : TextCollectingVisitor(com.vladsch.flexmark.ast.util.TextCollectingVisitor)

Aggregations

TextCollectingVisitor (com.vladsch.flexmark.ast.util.TextCollectingVisitor)13 Node (com.vladsch.flexmark.ast.Node)3 Attributes (com.vladsch.flexmark.util.html.Attributes)3 Heading (com.vladsch.flexmark.ast.Heading)2 EnumRefTextCollectingVisitor (com.vladsch.flexmark.ext.enumerated.reference.internal.EnumRefTextCollectingVisitor)2 ResolvedLink (com.vladsch.flexmark.html.renderer.ResolvedLink)2 Block (com.vladsch.flexmark.ast.Block)1 WikiLink (com.vladsch.flexmark.ext.wikilink.WikiLink)1 WikiNode (com.vladsch.flexmark.ext.wikilink.WikiNode)1 Parser (com.vladsch.flexmark.parser.Parser)1 AstCollectingVisitor (com.vladsch.flexmark.test.AstCollectingVisitor)1 DataHolder (com.vladsch.flexmark.util.options.DataHolder)1 MutableDataSet (com.vladsch.flexmark.util.options.MutableDataSet)1 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)1 Test (org.junit.Test)1