Search in sources :

Example 11 with Attributes

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

the class FlexmarkHtmlParser method transferToParentOnly.

private void transferToParentOnly(String... includes) {
    if (myStateStack.isEmpty())
        throw new IllegalStateException("transferIdToParent with an empty stack");
    final Attributes attributes = new Attributes();
    for (String include : includes) {
        Attribute attribute = myState.myAttributes.get(include);
        if (attribute != null) {
            myState.myAttributes.remove(include);
            attributes.addValue(attribute);
        }
    }
    if (!attributes.isEmpty()) {
        final State parentState = myStateStack.peek();
        for (String attrName : attributes.keySet()) {
            parentState.myAttributes.addValue(attributes.get(attrName));
        }
    }
}
Also used : Attribute(com.vladsch.flexmark.util.html.Attribute) Attributes(com.vladsch.flexmark.util.html.Attributes)

Example 12 with Attributes

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

the class TablesTest method attributeProviderIsApplied.

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

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

                @Override
                public void setAttributes(Node node, AttributablePart part, Attributes attributes) {
                    if (node instanceof TableBlock) {
                        attributes.replaceValue("test", "block");
                    } else if (node instanceof TableHead) {
                        attributes.replaceValue("test", "head");
                    } else if (node instanceof TableBody) {
                        attributes.replaceValue("test", "body");
                    } else if (node instanceof TableRow) {
                        attributes.replaceValue("test", "row");
                    } else if (node instanceof TableCell) {
                        attributes.replaceValue("test", "cell");
                    }
                }
            };
        }
    };
    HtmlRenderer renderer = HtmlRenderer.builder().attributeProviderFactory(factory).extensions(EXTENSIONS).build();
    String rendered = renderer.render(PARSER.parse("Abc|Def\n---|---\n1|2"));
    assertThat(rendered, is("<table test=\"block\">\n" + "<thead test=\"head\">\n" + "<tr test=\"row\"><th test=\"cell\">Abc</th><th test=\"cell\">Def</th></tr>\n" + "</thead>\n" + "<tbody test=\"body\">\n" + "<tr test=\"row\"><td test=\"cell\">1</td><td test=\"cell\">2</td></tr>\n" + "</tbody>\n" + "</table>\n"));
}
Also used : Node(com.vladsch.flexmark.ast.Node) Attributes(com.vladsch.flexmark.util.html.Attributes) AttributablePart(com.vladsch.flexmark.html.renderer.AttributablePart) AttributeProviderFactory(com.vladsch.flexmark.html.AttributeProviderFactory) IndependentAttributeProviderFactory(com.vladsch.flexmark.html.IndependentAttributeProviderFactory) IndependentAttributeProviderFactory(com.vladsch.flexmark.html.IndependentAttributeProviderFactory) LinkResolverContext(com.vladsch.flexmark.html.renderer.LinkResolverContext) HtmlRenderer(com.vladsch.flexmark.html.HtmlRenderer) AttributeProvider(com.vladsch.flexmark.html.AttributeProvider) Test(org.junit.Test)

Example 13 with Attributes

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

the class FlexmarkHtmlParser method processAttributes.

void processAttributes(Node node) {
    Attributes attributes = myState.myAttributes;
    if (myOptions.outputAttributesIdAttr || !myOptions.outputAttributesNamesRegex.isEmpty()) {
        final org.jsoup.nodes.Attributes nodeAttributes = node.attributes();
        boolean idDone = false;
        if (myOptions.outputAttributesIdAttr) {
            String id = nodeAttributes.get("id");
            if (id == null || id.isEmpty()) {
                id = nodeAttributes.get("name");
            }
            if (id != null && !id.isEmpty()) {
                attributes.replaceValue("id", id);
                idDone = true;
            }
        }
        if (!myOptions.outputAttributesNamesRegex.isEmpty()) {
            for (org.jsoup.nodes.Attribute attribute : nodeAttributes) {
                if (idDone && (attribute.getKey().equals("id") || attribute.getKey().equals("name"))) {
                    continue;
                }
                if (attribute.getKey().matches(myOptions.outputAttributesNamesRegex)) {
                    attributes.replaceValue(attribute.getKey(), attribute.getValue());
                }
            }
        }
    }
}
Also used : org.jsoup.nodes(org.jsoup.nodes) Attributes(com.vladsch.flexmark.util.html.Attributes)

Example 14 with Attributes

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

the class FlexmarkHtmlParser method outputAttributes.

void outputAttributes(FormattingAppendable out) {
    Attributes attributes = myState.myAttributes;
    if (!attributes.isEmpty()) {
        // have some
        String sep = "";
        out.append("{");
        for (String attrName : attributes.keySet()) {
            String value = attributes.getValue(attrName);
            out.append(sep);
            if (attrName.equals("id") || attrName.equals("name")) {
                out.append("#").append(value);
            } else if (attrName.equals("class")) {
                out.append(".").append(value);
            } else {
                out.append(attrName).append("=");
                if (!value.contains("\"")) {
                    out.append('"').append(value).append('"');
                } else if (!value.contains("\'")) {
                    out.append('\'').append(value).append('\'');
                } else {
                    out.append('"').append(value.replace("\"", "\\\"")).append('"');
                }
            }
            sep = " ";
        }
        out.append("}");
    }
}
Also used : Attributes(com.vladsch.flexmark.util.html.Attributes)

Example 15 with Attributes

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

the class FlexmarkHtmlParser method transferToParentExcept.

private void transferToParentExcept(String... excludes) {
    if (myStateStack.isEmpty())
        throw new IllegalStateException("transferIdToParent with an empty stack");
    final Attributes attributes = new Attributes(myState.myAttributes);
    myState.myAttributes.clear();
    for (String exclude : excludes) {
        myState.myAttributes.addValue(attributes.get(exclude));
        attributes.remove(exclude);
    }
    if (!attributes.isEmpty()) {
        final State parentState = myStateStack.peek();
        for (String attrName : attributes.keySet()) {
            parentState.myAttributes.addValue(attributes.get(attrName));
        }
    }
}
Also used : Attributes(com.vladsch.flexmark.util.html.Attributes)

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