Search in sources :

Example 16 with Attributes

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

the class AttributesNodeRenderer method getNodeRenderingHandlers.

// only registered if assignTextAttributes is enabled
@Override
public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
    HashSet<NodeRenderingHandler<?>> set = new HashSet<NodeRenderingHandler<?>>();
    set.add(new NodeRenderingHandler<AttributesNode>(AttributesNode.class, new CustomNodeRenderer<AttributesNode>() {

        @Override
        public void render(AttributesNode node, NodeRendererContext context, HtmlWriter html) {
            int tmp = 0;
        }
    }));
    set.add(new NodeRenderingHandler<TextBase>(TextBase.class, new CustomNodeRenderer<TextBase>() {

        @Override
        public void render(TextBase node, NodeRendererContext context, HtmlWriter html) {
            if (myOptions.assignTextAttributes) {
                final Attributes nodeAttributes = context.extendRenderingNodeAttributes(AttributablePart.NODE, null);
                if (!nodeAttributes.isEmpty()) {
                    // has attributes then we wrap it in a span
                    html.setAttributes(nodeAttributes).withAttr().tag("span");
                    context.delegateRender();
                    html.closeTag("span");
                    return;
                }
            }
            context.delegateRender();
        }
    }));
    return set;
}
Also used : TextBase(com.vladsch.flexmark.ast.TextBase) HtmlWriter(com.vladsch.flexmark.html.HtmlWriter) Attributes(com.vladsch.flexmark.util.html.Attributes) CustomNodeRenderer(com.vladsch.flexmark.html.CustomNodeRenderer) AttributesNode(com.vladsch.flexmark.ext.attributes.AttributesNode) HashSet(java.util.HashSet)

Example 17 with Attributes

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

the class CoreNodeRenderer method render.

private void render(LinkRef 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.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 = context.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()) {
            html.text(node.getChars().unescape());
        } else {
            html.text(node.getChars().prefixOf(node.getChildChars()).unescape());
            renderChildrenSourceLineWrapped(node, node.getText(), context, html);
            html.text(node.getChars().suffixOf(node.getChildChars()).unescape());
        }
    } else {
        if (context.isDoNotRenderLinks()) {
            context.renderChildren(node);
        } else {
            Attributes attributes = resolvedLink.getNonNullAttributes();
            html.attr("href", resolvedLink.getUrl());
            if (reference != null) {
                attributes = context.extendRenderingNodeAttributes(reference, AttributablePart.NODE, attributes);
            }
            html.attr(attributes);
            html.srcPos(node.getChars()).withAttr(resolvedLink).tag("a");
            renderChildrenSourceLineWrapped(node, node.getText(), context, html);
            html.tag("/a");
        }
    }
}
Also used : Attributes(com.vladsch.flexmark.util.html.Attributes)

Example 18 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 Emoji node, final DocxRendererContext docx) {
    final EmojiResolvedShortcut shortcut = EmojiResolvedShortcut.getEmojiText(node, emojiOptions.useShortcutType, emojiOptions.useImageType, emojiOptions.rootImagePath);
    if (shortcut.emoji == null || shortcut.emojiText == null) {
        // output as text
        docx.text(":");
        docx.renderChildren(node);
        docx.text(":");
    } else {
        if (shortcut.isUnicode) {
            docx.text(shortcut.emojiText);
        } else {
            ResolvedLink resolvedLink = docx.resolveLink(LinkType.IMAGE, shortcut.emojiText, null, null);
            String altText = shortcut.alt;
            String url = resolvedLink.getUrl();
            Attributes attributes = resolvedLink.getNonNullAttributes();
            if (shortcut.alt != null) {
                attributes.replaceValue("alt", shortcut.alt);
            }
            // need to determine the font point size from the style
            RPr rPr = docx.getFactory().createRPr();
            RPr paraRPr = docx.getFactory().createRPr();
            PPr pPr = docx.getFactory().createPPr();
            docx.getBlockFormatProvider().getPPr(pPr);
            docx.getBlockFormatProvider().getParaRPr(paraRPr);
            paraRPr = docx.getHelper().getExplicitRPr(paraRPr, pPr);
            docx.getRunFormatProvider().getRPr(rPr);
            rPr = docx.getHelper().getExplicitRPr(rPr);
            StyleUtil.apply(rPr, paraRPr);
            StyleUtil.apply(paraRPr, rPr);
            // now see if we have line height
            final HpsMeasure sz = rPr.getSz();
            long l = -1;
            if (sz != null) {
                l = sz.getVal().longValue();
                attributes.replaceValue("height", String.valueOf(Math.round(l / 2 / options.docEmojiImageVertSize)));
                attributes.replaceValue("width", String.valueOf(Math.round(l / 2 / options.docEmojiImageVertSize)));
            } else if (!emojiOptions.attrImageSize.isEmpty()) {
                attributes.replaceValue("height", emojiOptions.attrImageSize);
                attributes.replaceValue("width", emojiOptions.attrImageSize);
                // page dimensions not used, these are not %
                l = getSizeInfo(attributes, "width", 100.0);
                // now revert from twips to points
                l /= 20;
            }
            if (!emojiOptions.attrAlign.isEmpty()) {
                attributes.replaceValue("align", emojiOptions.attrAlign);
            }
            attributes = docx.extendRenderingNodeAttributes(AttributablePart.NODE, attributes);
            R r = renderImage(docx, url, attributes);
            // <w:position w:val="-4"/>
            if (r != null) {
                long adj = Math.round(l * options.docEmojiImageVertOffset);
                if (adj < 0) {
                    RPr rPr1 = r.getRPr();
                    if (rPr1 == null) {
                        rPr1 = docx.getFactory().createRPr();
                        r.setRPr(rPr1);
                    }
                    CTSignedHpsMeasure hpsMeasure = docx.getFactory().createCTSignedHpsMeasure();
                    rPr1.setPosition(hpsMeasure);
                    hpsMeasure.setVal(BigInteger.valueOf(adj));
                }
            }
        }
    }
}
Also used : EmojiResolvedShortcut(com.vladsch.flexmark.ext.emoji.internal.EmojiResolvedShortcut) Attributes(com.vladsch.flexmark.util.html.Attributes) ResolvedLink(com.vladsch.flexmark.html.renderer.ResolvedLink)

Example 19 with Attributes

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

the class HtmlRendererTest method attributeProviderForCodeBlock.

@Test
public void attributeProviderForCodeBlock() {
    AttributeProviderFactory factory = new IndependentAttributeProviderFactory() {

        @Override
        public AttributeProvider create(LinkResolverContext context) {
            // noinspection ReturnOfInnerClass
            return new AttributeProvider() {

                @Override
                public void setAttributes(Node node, AttributablePart part, Attributes attributes) {
                    if (node instanceof FencedCodeBlock && part == CoreNodeRenderer.CODE_CONTENT) {
                        FencedCodeBlock fencedCodeBlock = (FencedCodeBlock) node;
                        // Remove the default attribute for info
                        attributes.remove("class");
                        // Put info in custom attribute instead
                        attributes.replaceValue("data-custom", fencedCodeBlock.getInfo().toString());
                    }
                }
            };
        }
    };
    HtmlRenderer renderer = HtmlRenderer.builder().attributeProviderFactory(factory).build();
    String rendered = renderer.render(parse("```info\ncontent\n```"));
    assertEquals("<pre><code data-custom=\"info\">content\n</code></pre>\n", rendered);
    String rendered2 = renderer.render(parse("```evil\"\ncontent\n```"));
    assertEquals("<pre><code data-custom=\"evil&quot;\">content\n</code></pre>\n", rendered2);
}
Also used : Node(com.vladsch.flexmark.ast.Node) Attributes(com.vladsch.flexmark.util.html.Attributes) FencedCodeBlock(com.vladsch.flexmark.ast.FencedCodeBlock) Test(org.junit.Test)

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