-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathXWikiMacrosSample.java
65 lines (51 loc) · 2.12 KB
/
XWikiMacrosSample.java
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
package com.vladsch.flexmark.java.samples;
import com.vladsch.flexmark.ext.xwiki.macros.*;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.ast.NodeVisitor;
import com.vladsch.flexmark.util.data.MutableDataHolder;
import com.vladsch.flexmark.util.data.MutableDataSet;
import java.util.Collections;
import java.util.Map;
@SuppressWarnings({ "WeakerAccess" })
public class XWikiMacrosSample implements MacroVisitor {
// Sample XWiki processing
// NOTE: using raw text content of the node means that any nested macros will be taken as is
// to handle nested macros would require more logic to allow nested macros to provide their content
// to parent macros
NodeVisitor visitor = new NodeVisitor(
MacroVisitorExt.VISIT_HANDLERS(this)
);
@Override
public void visit(Macro node) {
if (!node.isBlockMacro()) {
// collect attributes
Map<String, String> attributes = node.getAttributes();
String content = node.getMacroContentChars().toString();
boolean isBlock = false;
// use content, attributes, isBlock
}
}
@Override
public void visit(MacroClose node) {
}
@Override
public void visit(MacroBlock node) {
// collect attributes
Map<String, String> attributes = node.getAttributes();
String content = node.getMacroContentChars().toString();
boolean isBlock = true;
// use content, attributes, isBlock
}
public static void main(String[] args) {
MutableDataHolder options = new MutableDataSet();
options.set(Parser.EXTENSIONS, Collections.singletonList(MacroExtension.create()));
Parser parser = Parser.builder(options).build();
HtmlRenderer renderer = HtmlRenderer.builder(options).build();
String markdown = "markdown content here";
Node document = parser.parse(markdown);
XWikiMacrosSample macroProcessor = new XWikiMacrosSample();
macroProcessor.visitor.visitChildren(document);
}
}