Search in sources :

Example 6 with TextCollectingVisitor

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

the class CoreNodeRenderer 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, null);
        String url = resolvedLink.getUrl();
        if (!node.getUrlContent().isEmpty()) {
            // reverse URL encoding of =, &
            String content = Escaping.percentEncodeUrl(node.getUrlContent()).replace("+", "%2B").replace("%3D", "=").replace("%26", "&");
            url += content;
        }
        html.attr("src", url);
        html.attr("alt", altText);
        // we have a title part, use that
        if (node.getTitle().isNotNull()) {
            resolvedLink.getNonNullAttributes().replaceValue(Attribute.TITLE_ATTR, node.getTitle().unescape());
        } else {
            resolvedLink.getNonNullAttributes().remove(Attribute.TITLE_ATTR);
        }
        html.attr(resolvedLink.getAttributes());
        html.srcPos(node.getChars()).withAttr(resolvedLink).tagVoid("img");
    }
}
Also used : TextCollectingVisitor(com.vladsch.flexmark.ast.util.TextCollectingVisitor)

Example 7 with TextCollectingVisitor

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

the class CoreNodeRenderer method render.

private void render(ImageRef node, NodeRendererContext context, HtmlWriter html) {
    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 = context.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 = context.resolveLink(LinkType.IMAGE_REF, normalizeRef, null, null);
        if (resolvedLink.getStatus() == UNKNOWN) {
            resolvedLink = null;
        }
    }
    if (resolvedLink == null) {
        // empty ref, we treat it as text
        html.text(node.getChars().unescape());
    } else {
        if (!context.isDoNotRenderLinks()) {
            String altText = new TextCollectingVisitor().collectAndGetText(node);
            Attributes attributes = resolvedLink.getNonNullAttributes();
            html.attr("src", resolvedLink.getUrl());
            html.attr("alt", altText);
            // need to take attributes for reference definition, then overlay them with ours
            if (reference != null) {
                attributes = context.extendRenderingNodeAttributes(reference, AttributablePart.NODE, attributes);
            }
            html.attr(attributes);
            html.srcPos(node.getChars()).withAttr(resolvedLink).tagVoid("img");
        }
    }
}
Also used : TextCollectingVisitor(com.vladsch.flexmark.ast.util.TextCollectingVisitor) Attributes(com.vladsch.flexmark.util.html.Attributes)

Example 8 with TextCollectingVisitor

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

the class TocUtils method markdownHeaderTexts.

public static List<String> markdownHeaderTexts(List<Heading> headings, TocOptions tocOptions) {
    ArrayList<String> headingTexts = new ArrayList<String>(headings.size());
    for (Heading header : headings) {
        String headerText;
        // need to skip anchor links but render emphasis
        if (tocOptions.isTextOnly) {
            headerText = new TextCollectingVisitor().collectAndGetText(header);
        } else {
            headerText = header.getChars().toString();
        }
        String headerId = header.getAnchorRefId();
        String headerLink;
        if (headerId == null || headerText.isEmpty()) {
            headerLink = headerText;
        } else {
            headerLink = "[" + headerText + "](#" + headerId + ")";
        }
        headingTexts.add(headerLink);
    }
    return headingTexts;
}
Also used : TextCollectingVisitor(com.vladsch.flexmark.ast.util.TextCollectingVisitor) Heading(com.vladsch.flexmark.ast.Heading)

Example 9 with TextCollectingVisitor

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

the class WikiLinkLinkRefProcessor method updateNodeElements.

@Override
public void updateNodeElements(final Document document, final Node node) {
    assert (node instanceof WikiNode);
    final WikiNode wikiNode = (WikiNode) node;
    if (node instanceof WikiLink && WikiLinkExtension.ALLOW_INLINES.getFrom(document)) {
        // need to update link and pageRef with plain text versions
        if (wikiNode.getText().isNull()) {
            BasedSequence link = new TextCollectingVisitor().collectAndGetSequence(node);
            wikiNode.setLink(link, WikiLinkExtension.ALLOW_ANCHORS.getFrom(document), WikiLinkExtension.ALLOW_ANCHOR_ESCAPE.getFrom(document));
        }
    }
}
Also used : TextCollectingVisitor(com.vladsch.flexmark.ast.util.TextCollectingVisitor) WikiNode(com.vladsch.flexmark.ext.wikilink.WikiNode) BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) WikiLink(com.vladsch.flexmark.ext.wikilink.WikiLink)

Example 10 with TextCollectingVisitor

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

the class JiraConverterNodeRenderer method render.

private void render(ImageRef node, NodeRendererContext context, HtmlWriter html) {
    if (!node.isDefined()) {
        // empty ref, we treat it as text
        assert !node.isDefined();
        html.text(node.getChars().unescape());
    } else {
        if (!context.isDoNotRenderLinks()) {
            Reference reference = node.getReferenceNode(referenceRepository);
            assert reference != null;
            String altText = new TextCollectingVisitor().collectAndGetText(node);
            ResolvedLink resolvedLink = context.resolveLink(LinkType.IMAGE, reference.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