-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMDParser.java
More file actions
246 lines (206 loc) · 8.51 KB
/
MDParser.java
File metadata and controls
246 lines (206 loc) · 8.51 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package javaxt.express.utils;
//******************************************************************************
//** Markdown Parser
//******************************************************************************
/**
* Simple, standalone Markdown-to-HTML converter. Supports headers, lists,
* fenced code blocks, inline links, and paragraphs.
*
******************************************************************************/
public class MDParser {
//**************************************************************************
//** toHTML
//**************************************************************************
/** Converts a Markdown string to an HTML snippet.
*/
public static String toHTML(String markdown){
if (markdown==null) return "";
String[] lines = markdown.split("\n");
StringBuilder html = new StringBuilder();
boolean inCodeBlock = false;
boolean inUl = false;
boolean inOl = false;
boolean inParagraph = false;
StringBuilder paragraph = new StringBuilder();
for (int i=0; i<lines.length; i++){
String line = lines[i];
//Strip trailing carriage return
if (line.endsWith("\r")) line = line.substring(0, line.length()-1);
//Handle fenced code blocks
if (line.trim().startsWith("```")){
if (!inCodeBlock){
//Close any open block
closeParagraph(html, paragraph, inParagraph);
inParagraph = false;
if (inUl){ html.append("</ul>\n"); inUl = false; }
if (inOl){ html.append("</ol>\n"); inOl = false; }
//Get optional language
String lang = line.trim().substring(3).trim();
if (lang.isEmpty()){
html.append("<pre>");
}
else{
html.append("<pre class=\"brush: ").append(lang).append(";\">");
}
inCodeBlock = true;
}
else{
html.append("</pre>\n");
inCodeBlock = false;
}
continue;
}
if (inCodeBlock){
html.append(escapeHtml(line)).append("\n");
continue;
}
//Handle headers
if (line.startsWith("#")){
//Close any open block
closeParagraph(html, paragraph, inParagraph);
inParagraph = false;
if (inUl){ html.append("</ul>\n"); inUl = false; }
if (inOl){ html.append("</ol>\n"); inOl = false; }
int level = 0;
while (level < line.length() && line.charAt(level) == '#') level++;
if (level > 6) level = 6;
String text = line.substring(level).trim();
text = processInline(text);
html.append("<h").append(level).append(">")
.append(text)
.append("</h").append(level).append(">\n");
continue;
}
//Handle unordered list items
if (line.matches("^\\s*[\\-\\*]\\s+.*")){
//Close paragraph or ordered list if open
closeParagraph(html, paragraph, inParagraph);
inParagraph = false;
if (inOl){ html.append("</ol>\n"); inOl = false; }
if (!inUl){
html.append("<ul>\n");
inUl = true;
}
String text = line.replaceFirst("^\\s*[\\-\\*]\\s+", "");
html.append("<li>").append(processInline(text)).append("</li>\n");
continue;
}
//Handle ordered list items
if (line.matches("^\\s*\\d+\\.\\s+.*")){
//Close paragraph or unordered list if open
closeParagraph(html, paragraph, inParagraph);
inParagraph = false;
if (inUl){ html.append("</ul>\n"); inUl = false; }
if (!inOl){
html.append("<ol>\n");
inOl = true;
}
String text = line.replaceFirst("^\\s*\\d+\\.\\s+", "");
html.append("<li>").append(processInline(text)).append("</li>\n");
continue;
}
//Handle blank lines
if (line.trim().isEmpty()){
closeParagraph(html, paragraph, inParagraph);
inParagraph = false;
if (inUl){ html.append("</ul>\n"); inUl = false; }
if (inOl){ html.append("</ol>\n"); inOl = false; }
continue;
}
//Paragraph text
if (inUl){ html.append("</ul>\n"); inUl = false; }
if (inOl){ html.append("</ol>\n"); inOl = false; }
if (!inParagraph){
inParagraph = true;
paragraph.setLength(0);
}
else{
paragraph.append(" ");
}
paragraph.append(line.trim());
}
//Close any remaining open blocks
closeParagraph(html, paragraph, inParagraph);
if (inUl) html.append("</ul>\n");
if (inOl) html.append("</ol>\n");
if (inCodeBlock) html.append("</pre>\n");
return html.toString().trim();
}
//**************************************************************************
//** closeParagraph
//**************************************************************************
private static void closeParagraph(StringBuilder html, StringBuilder paragraph, boolean inParagraph){
if (inParagraph && paragraph.length()>0){
html.append("<p>").append(processInline(paragraph.toString())).append("</p>\n");
paragraph.setLength(0);
}
}
//**************************************************************************
//** processInline
//**************************************************************************
/** Processes inline markdown elements (bold, links) within a line of text.
*/
private static String processInline(String text){
//Convert bold: **text** -> <b>text</b>
StringBuilder bold = new StringBuilder();
int bpos = 0;
while (bpos < text.length()){
int start = text.indexOf("**", bpos);
if (start == -1){
bold.append(text.substring(bpos));
break;
}
int end = text.indexOf("**", start+2);
if (end == -1){
bold.append(text.substring(bpos));
break;
}
bold.append(text, bpos, start);
bold.append("<b>").append(text, start+2, end).append("</b>");
bpos = end + 2;
}
text = bold.toString();
//Convert inline links: [text](url) -> <a href="url">text</a>
StringBuilder sb = new StringBuilder();
int pos = 0;
while (pos < text.length()){
int linkStart = text.indexOf('[', pos);
if (linkStart == -1){
sb.append(text.substring(pos));
break;
}
int linkTextEnd = text.indexOf(']', linkStart);
if (linkTextEnd == -1){
sb.append(text.substring(pos));
break;
}
if (linkTextEnd+1 < text.length() && text.charAt(linkTextEnd+1) == '('){
int urlEnd = text.indexOf(')', linkTextEnd+2);
if (urlEnd == -1){
sb.append(text.substring(pos));
break;
}
sb.append(text, pos, linkStart);
String linkText = text.substring(linkStart+1, linkTextEnd);
String url = text.substring(linkTextEnd+2, urlEnd);
sb.append("<a href=\"").append(url).append("\">").append(linkText).append("</a>");
pos = urlEnd + 1;
}
else{
sb.append(text, pos, linkTextEnd+1);
pos = linkTextEnd + 1;
}
}
return sb.toString();
}
//**************************************************************************
//** escapeHtml
//**************************************************************************
/** Escapes HTML special characters in code blocks.
*/
private static String escapeHtml(String text){
return text.replace("&", "&")
.replace("<", "<")
.replace(">", ">");
}
}