-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathNodeInsertingPostProcessorSample.java
More file actions
142 lines (115 loc) · 5.67 KB
/
NodeInsertingPostProcessorSample.java
File metadata and controls
142 lines (115 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package com.vladsch.flexmark.java.samples;
import com.vladsch.flexmark.ast.Image;
import com.vladsch.flexmark.ast.ImageRef;
import com.vladsch.flexmark.ast.Paragraph;
import com.vladsch.flexmark.ast.Text;
import com.vladsch.flexmark.html.EmbeddedAttributeProvider;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.parser.block.NodePostProcessor;
import com.vladsch.flexmark.parser.block.NodePostProcessorFactory;
import com.vladsch.flexmark.test.util.AstCollectingVisitor;
import com.vladsch.flexmark.util.ast.Document;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.ast.NodeTracker;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.data.MutableDataHolder;
import com.vladsch.flexmark.util.data.MutableDataSet;
import com.vladsch.flexmark.util.html.Attributes;
import com.vladsch.flexmark.util.html.MutableAttributes;
import com.vladsch.flexmark.util.sequence.BasedSequence;
import com.vladsch.flexmark.util.sequence.PrefixedSubSequence;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
/**
* A sample that demonstrates how to strip (replace) specific tokens from a parsed
* {@link Document} prior to rendering.
*/
public class NodeInsertingPostProcessorSample {
static final DataHolder OPTIONS = new MutableDataSet()
.set(Parser.EXTENSIONS, Collections.singletonList(NodeInsertingPostProcessorExtension.create()))
.toImmutable();
static final Parser PARSER = Parser.builder(OPTIONS).build();
static final HtmlRenderer RENDERER = HtmlRenderer.builder(OPTIONS).build();
static class NodeInsertingPostProcessor extends NodePostProcessor {
private static class NodeInsertingFactory extends NodePostProcessorFactory {
NodeInsertingFactory(DataHolder options) {
super(false);
addNodes(Image.class);
addNodes(ImageRef.class);
}
@NotNull
@Override
public NodePostProcessor apply(@NotNull Document document) {
return new NodeInsertingPostProcessor();
}
}
public static NodePostProcessorFactory Factory(DataHolder options) {
return new NodeInsertingFactory(options);
}
@Override
public void process(@NotNull NodeTracker state, @NotNull Node node) {
BasedSequence paragraphText = BasedSequence.NULL;
if (node instanceof ImageRef) { // [foo](http://example.com)
ImageRef imageRef = (ImageRef) node;
paragraphText = imageRef.isReferenceTextCombined() ? imageRef.getReference() : imageRef.getText();
} else if (node instanceof Image) { // 
Image image = (Image) node;
paragraphText = image.getText();
}
if (!paragraphText.isBlank()) {
Node paragraphParent = node.getAncestorOfType(Paragraph.class);
// should always have a paragraph wrapper
assert paragraphParent != null;
// create a text element to hold the text
Text text = new Text(PrefixedSubSequence.prefixOf(paragraphText.toString(), paragraphParent.getChars().getEmptySuffix()));
// create a paragraph for the text
Paragraph paragraph = new Paragraph(text.getChars());
// this will allows us add attributes in the AST without needing to modify the attribute provider
MutableAttributes attributes = new MutableAttributes();
attributes.addValue("class", "caption");
paragraph.appendChild(new EmbeddedAttributeProvider.EmbeddedNodeAttributes(paragraph, attributes));
paragraph.appendChild(text);
paragraph.setCharsFromContent();
paragraphParent.insertAfter(paragraph);
paragraphParent.setCharsFromContent();
state.nodeAddedWithChildren(paragraph);
}
}
}
/**
* An extension that registers a post processor which intentionally strips (replaces)
* specific link and image-link tokens after parsing.
*/
static class NodeInsertingPostProcessorExtension implements Parser.ParserExtension {
private NodeInsertingPostProcessorExtension() { }
@Override
public void parserOptions(MutableDataHolder options) { }
@Override
public void extend(Parser.Builder parserBuilder) {
parserBuilder.postProcessorFactory(NodeInsertingPostProcessor.Factory(parserBuilder));
}
public static NodeInsertingPostProcessorExtension create() {
return new NodeInsertingPostProcessorExtension();
}
}
public static void main(String[] args) {
String original = " **baz**\n" +
"\n" +
"![bar]\n" +
"\n" +
"![Figure 1. Some description here.][bar]\n" +
"\n" +
"[bar]: http://example.com/image.png 'Image Title'\n" +
"\n";
Node document = PARSER.parse(original);
String html = RENDERER.render(document);
// <p>foo bar <strong>baz</strong></p>
System.out.println("\n---- Markdown ------------------------\n");
System.out.println(original);
System.out.println("\n---- HTML ------------------------\n");
System.out.println(html);
System.out.println("\n---- AST ------------------------\n");
System.out.println(new AstCollectingVisitor().collectAndGetAstText(document));
}
}