Search in sources :

Example 1 with AttributeProviderFactory

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

Aggregations

Node (com.vladsch.flexmark.ast.Node)1 AttributeProvider (com.vladsch.flexmark.html.AttributeProvider)1 AttributeProviderFactory (com.vladsch.flexmark.html.AttributeProviderFactory)1 HtmlRenderer (com.vladsch.flexmark.html.HtmlRenderer)1 IndependentAttributeProviderFactory (com.vladsch.flexmark.html.IndependentAttributeProviderFactory)1 AttributablePart (com.vladsch.flexmark.html.renderer.AttributablePart)1 LinkResolverContext (com.vladsch.flexmark.html.renderer.LinkResolverContext)1 Attributes (com.vladsch.flexmark.util.html.Attributes)1 Test (org.junit.Test)1