-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathJekyllIncludeFileSample2.java
106 lines (87 loc) · 3.83 KB
/
JekyllIncludeFileSample2.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
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
package com.vladsch.flexmark.java.samples;
import com.vladsch.flexmark.ext.autolink.AutolinkExtension;
import com.vladsch.flexmark.ext.jekyll.tag.JekyllTagExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Document;
import com.vladsch.flexmark.util.data.MutableDataHolder;
import com.vladsch.flexmark.util.data.MutableDataSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* This sample computes the html content map without examining the main document AST.
*
* This means that the main document is parsed once.
*
* For a sample which needs to examine the document AST include tags to compute
* the embedded content map, which results in the main document being parsed twice,
* see: {@link JekyllIncludeFileSample}
*/
public class JekyllIncludeFileSample2 {
static String commonMark(String markdown, Map<String, String> included) {
MutableDataHolder options = new MutableDataSet();
options.set(Parser.EXTENSIONS, Arrays.asList(AutolinkExtension.create(), JekyllTagExtension.create()));
// change soft break to hard break
options.set(HtmlRenderer.SOFT_BREAK, "<br/>");
// parser and renderer used to convert markdown included content to HTML
Parser parser = Parser.builder(options).build();
HtmlRenderer renderer = HtmlRenderer.builder(options).build();
Map<String, String> includeHtmlMap = new HashMap<>();
ArrayList<Document> includedDocs = new ArrayList<>();
for (Map.Entry<String, String> entry : included.entrySet()) {
String includeFile = entry.getKey();
String text = entry.getValue();
if (includeFile.endsWith(".md")) {
Document includeDoc = parser.parse(text);
String includeHtml = renderer.render(includeDoc);
includeHtmlMap.put(includeFile, includeHtml);
// copy any definition of reference elements from included file to our document
includedDocs.add(includeDoc);
} else {
includeHtmlMap.put(includeFile, text);
}
}
if (!includeHtmlMap.isEmpty()) {
options.set(JekyllTagExtension.INCLUDED_HTML, includeHtmlMap);
options.set(JekyllTagExtension.EMBED_INCLUDED_CONTENT, true);
// use new options for main document parsing/rendering
parser = Parser.builder(options).build();
renderer = HtmlRenderer.builder(options).build();
}
Document document = parser.parse(markdown);
// transfer any references defined in the included documents to the main document
for (Document includedDoc : includedDocs) {
parser.transferReferences(document, includedDoc, null);
}
return renderer.render(document);
}
public static void main(String[] args) {
Map<String, String> included = new HashMap<>();
included.put("test.md", "## Included Heading\n" +
"\n" +
"Included paragraph\n" +
"\n" +
"[ref]: http://example.com\n" +
"");
included.put("test.html", "<p>test.html: some text</p>\n" +
"");
String html = commonMark("http://github.com/vsch/flexmark-java\n" +
"\n" +
"[ref]\n" +
"\n" +
"{% include test.md %}\n" +
"\n" +
"", included);
System.out.println(html);
html = commonMark("http://github.com/vsch/flexmark-java\n" +
"\n" +
"[ref]\n" +
"\n" +
"{% include test.html %}\n" +
"\n" +
"", included);
System.out.println(html);
}
}