-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMDParser.java
More file actions
369 lines (314 loc) · 13.3 KB
/
Copy pathMDParser.java
File metadata and controls
369 lines (314 loc) · 13.3 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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 inTable = false;
boolean tableHeaderDone = 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; }
if (inTable){ html.append("</table>\n"); inTable = false; tableHeaderDone = 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; }
if (inTable){ html.append("</table>\n"); inTable = false; tableHeaderDone = 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 list continuation lines (indented text following a list item)
if ((inUl || inOl) && line.length()>0 && (line.charAt(0)==' ' || line.charAt(0)=='\t') && !line.trim().isEmpty()){
// Remove trailing </li>\n from previous item and append continuation
String ending = "</li>\n";
int idx = html.length() - ending.length();
if (idx >= 0 && html.substring(idx).equals(ending)){
html.setLength(idx);
html.append(" ").append(processInline(line.trim())).append("</li>\n");
}
continue;
}
//Handle table rows (lines starting and ending with |)
if (line.trim().startsWith("|") && line.trim().endsWith("|")){
//Close other open blocks
closeParagraph(html, paragraph, inParagraph);
inParagraph = false;
if (inUl){ html.append("</ul>\n"); inUl = false; }
if (inOl){ html.append("</ol>\n"); inOl = false; }
//Skip separator rows (e.g. |---|---|)
if (line.trim().matches("^\\|[\\s\\-:|]+\\|$")){
tableHeaderDone = true;
continue;
}
if (!inTable){
html.append("<table>\n");
inTable = true;
tableHeaderDone = false;
}
//Parse cells
String trimmed = line.trim();
trimmed = trimmed.substring(1, trimmed.length()-1); // strip outer pipes
String[] cells = trimmed.split("\\|");
String cellTag = !tableHeaderDone ? "th" : "td";
html.append("<tr>");
for (String cell : cells){
html.append("<").append(cellTag).append(">")
.append(processInline(cell.trim()))
.append("</").append(cellTag).append(">");
}
html.append("</tr>\n");
continue;
}
//Close table if we're in one and hit a non-table line
if (inTable){
html.append("</table>\n");
inTable = false;
tableHeaderDone = false;
}
//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 (inTable) html.append("</table>\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 (code, bold, italic, links,
* em-dashes) within a line of text.
*/
private static String processInline(String text){
//Convert inline code: `text` -> <code>text</code>
//Must come first so content inside backticks is not processed further
StringBuilder code = new StringBuilder();
int cpos = 0;
while (cpos < text.length()){
int start = text.indexOf('`', cpos);
if (start == -1){
code.append(text.substring(cpos));
break;
}
int end = text.indexOf('`', start+1);
if (end == -1){
code.append(text.substring(cpos));
break;
}
code.append(text, cpos, start);
String inner = text.substring(start+1, end);
code.append("<code>").append(escapeHtml(inner)).append("</code>");
cpos = end + 1;
}
text = code.toString();
//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 italic: *text* -> <i>text</i>
//Must come after bold so ** is consumed first
StringBuilder italic = new StringBuilder();
int ipos = 0;
while (ipos < text.length()){
int start = text.indexOf('*', ipos);
if (start == -1){
italic.append(text.substring(ipos));
break;
}
int end = text.indexOf('*', start+1);
if (end == -1){
italic.append(text.substring(ipos));
break;
}
italic.append(text, ipos, start);
italic.append("<i>").append(text, start+1, end).append("</i>");
ipos = end + 1;
}
text = italic.toString();
//Convert inline images and links:
//  -> <img src="url" alt="alt">
// [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;
}
boolean isImage = (linkStart > 0 && text.charAt(linkStart-1) == '!');
String innerText = text.substring(linkStart+1, linkTextEnd);
String url = text.substring(linkTextEnd+2, urlEnd);
if (isImage){
sb.append(text, pos, linkStart-1); // exclude the '!'
sb.append("<img src=\"").append(url).append("\" alt=\"").append(innerText).append("\">");
}
else{
sb.append(text, pos, linkStart);
sb.append("<a href=\"").append(url).append("\">").append(innerText).append("</a>");
}
pos = urlEnd + 1;
}
else{
sb.append(text, pos, linkTextEnd+1);
pos = linkTextEnd + 1;
}
}
text = sb.toString();
//Convert em-dashes: space--space -> —
//Also handles -- at start/end of text adjacent to a space
text = text.replace(" -- ", " — ");
return text;
}
//**************************************************************************
//** escapeHtml
//**************************************************************************
/** Escapes HTML special characters in code blocks.
*/
private static String escapeHtml(String text){
return text.replace("&", "&")
.replace("<", "<")
.replace(">", ">");
}
}