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
|
# Status:
# [.] Inline HTML
# [x] Autoescape
# [ ] Block elements
# [x] Paragraph
# [x] Line break
# [x] Headers (atx and setex)
# [x] Blockquotes
# [x] Lists
# [ ] Code blocks
# [x] Horizontal rules
# [ ] Span elements
# [.] Links
# [x] Inline
# [ ] Reference
# [x] Emphasis
# [x] Code
# [.] Images
# [x] Inline
# [ ] Reference
# [ ] Other
# [ ] Autolinks
# [ ] Escapes
#
# [ ] Non-standard features
# [x] Strikethrough ~~...~~
# [x] Superscript ^...^
# [x] Subscript ~...~
# [ ] Footnote [^...] (with user customizations)
# [x] Task list - [ ] (readonly)
# [.] Definition lists
# [x] Single line
# [ ] Multi-line
# [ ] Fenced code blocks ```lang ```
# [ ] Tables
# No character classes, since they aren't supported on every system
BEGIN {
prev_blankline = blankline = 1
skipprint = 1
newStack("bq-level")
newStack("list-level")
}
function println(line) {
if (0 == line) line = $0
print prev
prev = line
}
function autoinsert(arrname, isbefore) {
if (isbefore == "") isbefore = 0
for (confstr in arrname) {
split(confstr, conf, SUBSEP)
## FILENAME regex, ".*" (default)
if (2 in conf && FILENAME !~ conf[2]) { continue }
## Text regex, ".*" (default)
if (3 in conf && text !~ conf[3]) { continue }
if (isbefore) prev = arrname[confstr] prev
else prev = prev arrname[confstr]
## When, FIRST | ALL
if (conf[1] == FIRST) { delete arrname[confstr] }
}
}
{
prev_blankline = blankline
blankline = 1
}
/[^ \t]/ {
blankline = 0
}
# Inline HTML
/^[ \t]*<\/?[0-9A-Za-z]+/ {
println()
next
}
# Link definition
/^\[.+\]:/ {
blankline = 1
next
}
# Auto-escape &
/&/ {
ampstart = 0
## gsub(/&(?![[:alnum:]#]+;)/, "&")
while (match(substr($0, ampstart + 1), /&/)) {
ampstart += RSTART
if (match(substr($0, ampstart), /&[0-9A-Za-z#]+;/)) { continue }
$0 = substr($0, 1, ampstart - 1) "&" substr($0, ampstart + 1)
}
}
# Auto-escape <
/</ {
gsub(/</, "\\<")
}
# Blockquote Part 1/2
/^>[ \t]*$/ { next }
/^> / {
level = 0
while (match($0, /^> [ \t]*/)) {
$0 = substr($0, RLENGTH + 1)
level += RSTART
while (topStack("bq-level") > level) {
println("</blockquote>")
autoinsert(user_BLOCKQUOTE_AFTER)
popStack("bq-level")
}
if (topStack("bq-level") < level) {
autoinsert(user_BLOCKQUOTE_BEFORE, 1)
println("<blockquote>")
pushStack("bq-level", level)
}
}
}
# atx-style headers
/^[ \t]*#+/ {
match($0, /#+/)
if (RLENGTH <= 6) {
## Remove prefix/suffix
gsub(/^[ \t]*#+[ \t]*/, "")
gsub(/[ \t]*#+[ \t]*$/, "")
if (RLENGTH == 1) autoinsert(user_H1_BEFORE, 1)
else if (RLENGTH == 2) autoinsert(user_H2_BEFORE, 1)
else if (RLENGTH == 3) autoinsert(user_H3_BEFORE, 1)
else if (RLENGTH == 4) autoinsert(user_H4_BEFORE, 1)
else if (RLENGTH == 5) autoinsert(user_H5_BEFORE, 1)
else if (RLENGTH == 6) autoinsert(user_H6_BEFORE, 1)
autoinsert(user_H_BEFORE, 1)
println("<h" RLENGTH ">" $0 "</h" RLENGTH ">")
autoinsert(user_H_AFTER)
if (RLENGTH == 1) autoinsert(user_H1_AFTER)
else if (RLENGTH == 2) autoinsert(user_H2_AFTER)
else if (RLENGTH == 3) autoinsert(user_H3_AFTER)
else if (RLENGTH == 4) autoinsert(user_H4_AFTER)
else if (RLENGTH == 5) autoinsert(user_H5_AFTER)
else if (RLENGTH == 6) autoinsert(user_H6_AFTER)
blankline = 1
next
}
}
# setex-style headers
!prev_blankline && /^[ \t]*(=|-)+[ \t]*$/ {
sub(/^<p>/, "", prev) ## from paragraph logic
hlevel = 1 + ($0 ~ /-/)
prev = "<h" hlevel ">" prev "</h" hlevel ">"
if (hlevel == 1) autoinsert(user_H1_BEFORE, 1)
else autoinsert(user_H2_BEFORE, 1)
autoinsert(user_H_BEFORE, 1)
autoinsert(user_H_AFTER)
if (hlevel == 1) autoinsert(user_H1_AFTER)
else autoinsert(user_H2_AFTER)
blankline = 1
next
}
# Horizontal rules
/^(-|\*|_) ?(-|\*|_) ?(-|\*|_|[ \t])+$/ {
autoinsert(user_HR_BEFORE, 1)
println("<hr>")
autoinsert(user_HR_AFTER)
blankline = 1
next
}
# Line break
/ $/ {
sub(/[ \t]* $/, "</br>")
}
# Code blocks
/^( |\t)[ \t]*/ {
}
# Check boxes
/^[ \t]*- \[[ x]\][ \t]/ {
match($0, /- \[[ x]\][ \t]/)
if (substr($0, RSTART + 3, 1) == "x") checked = " checked=\"checked\""
else checked = ""
$0 = "<input type=\"checkbox\" disabled=\"disabled\"" checked ">" substr($0, RSTART + RLENGTH) "</br>"
}
# Unordered lists
function abs(x) { return ((x < 0) ? -x : x) }
!blankline && lengthStack("list-level") > 0 {
match($0, /^ */)
indent = RLENGTH
match($0, /(\*|\+|-|[0-9]+\.) /)
if (RLENGTH == -1) RLENGTH = 0
## indent + RLENGTH = length(" * " or " 1. ")
while (indent + RLENGTH < abs(topStack("list-level"))) {
if (topStack("list-level") < 0) {
println("</ol>")
autoinsert(user_L_AFTER)
autoinsert(user_OL_AFTER)
}
else {
println("</ul>")
autoinsert(user_L_AFTER)
autoinsert(user_UL_AFTER)
}
popStack("list-level")
}
}
/^[ \t]*(\*|\+|-|[0-9]+\.) / {
match($0, /(\*|\+|-|[0-9]+\.) /)
if (abs(topStack("list-level")) < RSTART) {
if (RLENGTH > 2) {
xl = "<ol>"; mult = -1
autoinsert(user_OL_BEFORE, 1)
}
else {
xl = "<ul>"; mult = 1
autoinsert(user_UL_BEFORE, 1)
}
autoinsert(user_L_BEFORE, 1)
println(xl "<li>")
pushStack("list-level", mult * (RSTART + RLENGTH - 1))
}
else {
println("</li><li>")
}
$0 = substr($0, RSTART + RLENGTH)
}
# Paragraphs
prev_blankline && !blankline && lengthStack("bq-level") == 0 && lengthStack("list-level") == 0 { $0 = "<p>" $0 }
!prev_blankline && blankline && lengthStack("bq-level") == 0 && lengthStack("list-level") == 0 { prev = prev "</p>" }
# Blockquote Part 2/2
blankline {
while (lengthStack("bq-level") > 0) {
println("</blockquote>")
autoinsert(user_BLOCKQUOTE_AFTER)
popStack("bq-level")
}
}
# Links
/!?\[[^\]]*\] ?[(\[][^)\]]*[\])]/ {
isimg = 0
while (match($0, /!?\[[^\]]*\] ?[(\[]/)) {
if (substr($0, RSTART, 1) == "!") {
isimg = 1
RSTART++
RLENGTH--
}
astart = RSTART ## astart = indexof("[")
has_space = substr($0, RSTART + RLENGTH - 2, 1) == " "
inner_anchor = substr($0, RSTART + 1, RLENGTH - 3 - has_space) ## ...[xxxxx](...)...
aend = RSTART + RLENGTH - 1
match(substr($0, astart), /\] ?[(\[][^)\]]*[\])]/)
aend += RLENGTH - 2 - has_space ## aend = indexof(")")
href = substr($0, astart + RSTART + 1 + has_space, RLENGTH - 3 - has_space) ## ...[...](xxxxx)...
title = "\"\""
if (substr($0, astart + RSTART + has_space, 1) == "[") { ## is reference link
if (href == "") href = inner_anchor
title = reference[href, "title"]
href = reference[href, "href"]
}
else if (match(href, /[ \t]"[^"]*"[ \t]*$/)) { ## has title
title = substr(href, RSTART + 1, RLENGTH - 1)
href = substr(href, 1, RSTART - 1)
}
sub(/[ \t]+/, "", href)
if (isimg) elem = "<img src=\"" href "\" alt=\"" inner_anchor "\" title=" title ">"
else elem = "<a href=\"" href "\" title=" title ">" inner_anchor "</a>"
$0 = substr($0, 1, astart - 1 - isimg) elem substr($0, aend + 1)
isimg = 0
}
}
# Emphasis
function surround(brace, opening, closing) {
while ($0 ~ brace ".*" brace) {
sub(brace, opening)
sub(brace, closing)
}
}
{
surround("__" , "<strong>", "</strong>")
surround("_" , "<em>" , "</em>")
surround("\\*\\*", "<strong>", "</strong>")
surround("\\*" , "<em>" , "</em>")
surround("~~" , "<s>" , "</s>")
surround("~" , "<sub>" , "</sub>")
surround("\\^" , "<sup>" , "</sup>")
surround("``" , "<code>" , "</code>")
surround("`" , "<code>" , "</code>")
}
!prev_blankline && /^: / {
sub(/^<p>/, "", prev)
sub(/^: /, "")
prev = "<dl><dt>" prev "</dt>" ORS "<dd>" $0 "</dd></dl>"
blankline = 1
next
}
{
println()
}
END {
print prev
if (!blankline && lengthStack("bq-level") == 0) { print "</p>" }
## Flush stacks
while (lengthStack("bq-level") > 0) {
print "</blockquote>"
popStack("bq-level")
}
while (lengthStack("list-level") > 0) {
if (topStack("list-level") < 0) print "</ol>"
else print "</ul>"
popStack("list-level")
}
}
|