Search in sources :

Example 1 with ResolvedLink

use of com.vladsch.flexmark.html.renderer.ResolvedLink in project jspwiki by apache.

the class JSPWikiLinkRenderer method getNodeRenderingHandlers.

/**
 * {@inheritDoc}
 *
 * @see com.vladsch.flexmark.html.renderer.NodeRenderer#getNodeRenderingHandlers()
 */
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
    HashSet<NodeRenderingHandler<?>> set = new HashSet<NodeRenderingHandler<?>>();
    set.add(new NodeRenderingHandler<JSPWikiLink>(JSPWikiLink.class, new CustomNodeRenderer<JSPWikiLink>() {

        /**
         * {@inheritDoc}
         */
        @Override
        public void render(final JSPWikiLink node, final NodeRendererContext context, final HtmlWriter html) {
            if (context.isDoNotRenderLinks()) {
                context.renderChildren(node);
            } else {
                // standard Link Rendering
                ResolvedLink resolvedLink = context.resolveLink(LinkType.LINK, node.getUrl().unescape(), null);
                html.attr("href", resolvedLink.getUrl());
                if (node.getTitle().isNotNull()) {
                    html.attr("title", node.getTitle().unescape());
                }
                html.srcPos(node.getChars()).withAttr(resolvedLink).tag("a");
                context.renderChildren(node);
                html.tag("/a");
            }
        }
    }));
    return set;
}
Also used : JSPWikiLink(org.apache.wiki.markdown.nodes.JSPWikiLink) HtmlWriter(com.vladsch.flexmark.html.HtmlWriter) NodeRenderingHandler(com.vladsch.flexmark.html.renderer.NodeRenderingHandler) NodeRendererContext(com.vladsch.flexmark.html.renderer.NodeRendererContext) ResolvedLink(com.vladsch.flexmark.html.renderer.ResolvedLink) CustomNodeRenderer(com.vladsch.flexmark.html.CustomNodeRenderer) HashSet(java.util.HashSet)

Example 2 with ResolvedLink

use of com.vladsch.flexmark.html.renderer.ResolvedLink in project flexmark-java by vsch.

the class CoreNodeDocxRenderer method render.

private void render(final LinkRef node, final DocxRendererContext docx) {
    ResolvedLink resolvedLink = null;
    if (!node.isDefined() && recheckUndefinedReferences) {
        if (node.getReferenceNode(referenceRepository) != null) {
            node.setDefined(true);
        }
    }
    BasedSequence urlSrc = node.getReference();
    Reference reference = null;
    if (node.isDefined()) {
        reference = node.getReferenceNode(referenceRepository);
        urlSrc = reference.getUrl();
        String url = urlSrc.unescape();
        resolvedLink = docx.resolveLink(LinkType.LINK, 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 = node.getReference().unescape();
        resolvedLink = docx.resolveLink(LinkType.LINK_REF, normalizeRef, null, null);
        if (resolvedLink.getStatus() == UNKNOWN) {
            resolvedLink = null;
        }
    }
    if (resolvedLink == null) {
        // empty ref, we treat it as text
        assert !node.isDefined();
        if (!node.hasChildren()) {
            docx.text(node.getChars().unescape());
        } else {
            docx.text(node.getChars().prefixOf(node.getChildChars()).unescape());
            docx.renderChildren(node);
            docx.text(node.getChars().suffixOf(node.getChildChars()).unescape());
        }
    } else {
        Attributes attributes = resolvedLink.getNonNullAttributes();
        if (reference != null) {
            attributes = docx.extendRenderingNodeAttributes(reference, AttributablePart.NODE, attributes);
        }
        attributes = docx.extendRenderingNodeAttributes(AttributablePart.NODE, attributes);
        renderURL(urlSrc, docx, resolvedLink.getUrl(), attributes, new Runnable() {

            @Override
            public void run() {
                docx.renderChildren(node);
            }
        });
    }
}
Also used : BasedSequence(com.vladsch.flexmark.util.sequence.BasedSequence) Attributes(com.vladsch.flexmark.util.html.Attributes) ResolvedLink(com.vladsch.flexmark.html.renderer.ResolvedLink)

Example 3 with ResolvedLink

use of com.vladsch.flexmark.html.renderer.ResolvedLink 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 4 with ResolvedLink

use of com.vladsch.flexmark.html.renderer.ResolvedLink in project flexmark-java by vsch.

the class CoreNodeDocxRenderer method render.

private void render(final Link node, final DocxRendererContext docx) {
    ResolvedLink resolvedLink = docx.resolveLink(LinkType.LINK, node.getUrl().unescape(), null, null);
    // we have a title part, use that
    Attributes attributes = resolvedLink.getNonNullAttributes();
    if (node.getTitle().isNotNull()) {
        attributes.replaceValue(Attribute.TITLE_ATTR, node.getTitle().unescape());
    } else {
        attributes.remove(Attribute.TITLE_ATTR);
    }
    attributes = docx.extendRenderingNodeAttributes(AttributablePart.NODE, attributes);
    renderURL(node.getUrl(), docx, resolvedLink.getUrl(), attributes, new Runnable() {

        @Override
        public void run() {
            docx.renderChildren(node);
        }
    });
}
Also used : Attributes(com.vladsch.flexmark.util.html.Attributes) ResolvedLink(com.vladsch.flexmark.html.renderer.ResolvedLink)

Example 5 with ResolvedLink

use of com.vladsch.flexmark.html.renderer.ResolvedLink 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", "&amp;");
        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)

Aggregations

ResolvedLink (com.vladsch.flexmark.html.renderer.ResolvedLink)6 Attributes (com.vladsch.flexmark.util.html.Attributes)5 TextCollectingVisitor (com.vladsch.flexmark.ast.util.TextCollectingVisitor)2 EnumRefTextCollectingVisitor (com.vladsch.flexmark.ext.enumerated.reference.internal.EnumRefTextCollectingVisitor)2 EmojiResolvedShortcut (com.vladsch.flexmark.ext.emoji.internal.EmojiResolvedShortcut)1 CustomNodeRenderer (com.vladsch.flexmark.html.CustomNodeRenderer)1 HtmlWriter (com.vladsch.flexmark.html.HtmlWriter)1 NodeRendererContext (com.vladsch.flexmark.html.renderer.NodeRendererContext)1 NodeRenderingHandler (com.vladsch.flexmark.html.renderer.NodeRenderingHandler)1 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)1 HashSet (java.util.HashSet)1 JSPWikiLink (org.apache.wiki.markdown.nodes.JSPWikiLink)1