@@ -99,26 +99,60 @@ class WordCountVisitor extends AbstractVisitor {
9999}
100100```
101101
102- #### Customize HTML rendering
102+ #### Add or change attributes of HTML elements
103103
104- Sometimes you might want to customize how HTML is rendered, or you added
105- custom node subclasses that you want to render to HTML. Both can be done
106- using node renderers .
104+ Sometimes you might want to customize how HTML is rendered. If all you
105+ want to do is add or change attributes on some elements, there's a
106+ simple way to do that .
107107
108- In this example, we're changing the rendering of indented code blocks:
108+ In this example, we register a factory for an ` AttributeProvider ` on the
109+ renderer to set a ` class="border" ` attribute on ` img ` elements.
109110
110111``` java
111112Parser parser = Parser . builder(). build();
112- HtmlNodeRendererFactory factory = new HtmlNodeRendererFactory () {
113+ HtmlRenderer renderer = HtmlRenderer . builder()
114+ .attributeProviderFactory(new AttributeProviderFactory () {
115+ public AttributeProvider create (AttributeProviderContext context ) {
116+ return new ImageAttributeProvider ();
117+ }
118+ })
119+ .build();
120+
121+ Node document = parser. parse(" " );
122+ renderer. render(document);
123+ // "<p><img src=\"/url.png\" alt=\"text\" class=\"border\" /></p>\n"
124+
125+ class ImageAttributeProvider implements AttributeProvider {
113126 @Override
114- public NodeRenderer create (HtmlNodeRendererContext context ) {
115- return new IndentedCodeBlockNodeRenderer (context);
127+ public void setAttributes (Node node , Map<String , String > attributes ) {
128+ if (node instanceof Image ) {
129+ attributes. put(" class" , " border" );
130+ }
116131 }
117- };
118- HtmlRenderer renderer = HtmlRenderer . builder(). nodeRendererFactory(factory). build();
132+ }
133+ ```
134+
135+ #### Customize HTML rendering
136+
137+ If you want to do more than just change attributes, there's also a way
138+ to take complete control over how HTML is rendered.
139+
140+ In this example, we're changing the rendering of indented code blocks to
141+ only wrap them in ` pre ` instead of ` pre ` and ` code ` :
142+
143+ ``` java
144+ Parser parser = Parser . builder(). build();
145+ HtmlRenderer renderer = HtmlRenderer . builder()
146+ .nodeRendererFactory(new HtmlNodeRendererFactory () {
147+ public NodeRenderer create (HtmlNodeRendererContext context ) {
148+ return new IndentedCodeBlockNodeRenderer (context);
149+ }
150+ })
151+ .build();
119152
120153Node document = parser. parse(" Example:\n\n code" );
121- renderer. render(document); // "<p>Example:</p>\n<pre>code\n</pre>\n"
154+ renderer. render(document);
155+ // "<p>Example:</p>\n<pre>code\n</pre>\n"
122156
123157class IndentedCodeBlockNodeRenderer implements NodeRenderer {
124158
@@ -147,6 +181,15 @@ class IndentedCodeBlockNodeRenderer implements NodeRenderer {
147181}
148182```
149183
184+ #### Add your own node types
185+
186+ In case you want to store additional data in the document or have custom
187+ elements in the resulting HTML, you can create your own subclass of
188+ ` CustomNode ` and add instances as child nodes to existing nodes.
189+
190+ To define the HTML rendering for them, you can use a ` NodeRenderer ` as
191+ explained above.
192+
150193### API documentation
151194
152195Javadocs are available online on
0 commit comments