Search in sources :

Example 1 with Attributes

use of com.vladsch.flexmark.util.html.Attributes in project flexmark-java by vsch.

the class CoreNodeDocxRenderer method render.

private void render(EnumeratedReferenceLink node, final DocxRendererContext docx) {
    final String text = node.getText().toString();
    if (text.isEmpty()) {
        // placeholder for ordinal
        docx.text(String.valueOf(ordinal));
    } else {
        final Node referenceFormat = enumeratedOrdinals.getFormatNode(text);
        int wasOrdinal = ordinal;
        ordinal = enumeratedOrdinals.getOrdinal(text);
        final String defaultText = String.format("%s %d", EnumeratedReferences.getType(text), ordinal);
        String title = referenceFormat != null ? new EnumRefTextCollectingVisitor(ordinal).collectAndGetText(referenceFormat) : defaultText;
        Attributes attributes = new Attributes();
        if (title != null) {
            attributes.replaceValue(Attribute.TITLE_ATTR, title);
        }
        attributes = docx.extendRenderingNodeAttributes(AttributablePart.NODE, attributes);
        renderURL(node.getText(), docx, "#" + text, attributes, new Runnable() {

            @Override
            public void run() {
                if (referenceFormat != null) {
                    docx.renderChildren(referenceFormat);
                } else {
                    // no format, just output ordinal
                    docx.text(defaultText);
                }
            }
        });
        ordinal = wasOrdinal;
    }
}
Also used : EnumRefTextCollectingVisitor(com.vladsch.flexmark.ext.enumerated.reference.internal.EnumRefTextCollectingVisitor) AttributesNode(com.vladsch.flexmark.ext.attributes.AttributesNode) Attributes(com.vladsch.flexmark.util.html.Attributes)

Example 2 with Attributes

use of com.vladsch.flexmark.util.html.Attributes 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 Attributes

use of com.vladsch.flexmark.util.html.Attributes 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 Attributes

use of com.vladsch.flexmark.util.html.Attributes 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 Attributes

use of com.vladsch.flexmark.util.html.Attributes 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)

Aggregations

Attributes (com.vladsch.flexmark.util.html.Attributes)19 ResolvedLink (com.vladsch.flexmark.html.renderer.ResolvedLink)5 TextCollectingVisitor (com.vladsch.flexmark.ast.util.TextCollectingVisitor)3 EnumRefTextCollectingVisitor (com.vladsch.flexmark.ext.enumerated.reference.internal.EnumRefTextCollectingVisitor)3 Test (org.junit.Test)3 Node (com.vladsch.flexmark.ast.Node)2 AttributesNode (com.vladsch.flexmark.ext.attributes.AttributesNode)2 FencedCodeBlock (com.vladsch.flexmark.ast.FencedCodeBlock)1 TextBase (com.vladsch.flexmark.ast.TextBase)1 EmojiResolvedShortcut (com.vladsch.flexmark.ext.emoji.internal.EmojiResolvedShortcut)1 AttributeProvider (com.vladsch.flexmark.html.AttributeProvider)1 AttributeProviderFactory (com.vladsch.flexmark.html.AttributeProviderFactory)1 CustomNodeRenderer (com.vladsch.flexmark.html.CustomNodeRenderer)1 HtmlRenderer (com.vladsch.flexmark.html.HtmlRenderer)1 HtmlWriter (com.vladsch.flexmark.html.HtmlWriter)1 IndependentAttributeProviderFactory (com.vladsch.flexmark.html.IndependentAttributeProviderFactory)1 AttributablePart (com.vladsch.flexmark.html.renderer.AttributablePart)1 LinkResolverContext (com.vladsch.flexmark.html.renderer.LinkResolverContext)1 Attribute (com.vladsch.flexmark.util.html.Attribute)1 BasedSequence (com.vladsch.flexmark.util.sequence.BasedSequence)1