4 # EXPERIMENTAL Markdown processor with minimal dependencies.
5 # Meant to support all features of John Grubers basic Markdown
6 # + a number of common extensions, mostly inspired by Pandoc Markdown
9 # - HTML processing / escaping (according to environment flag)
10 # - em-dashes and arrows
12 # Supported Features / TODO:
13 # ==========================
14 # [x] done [ ] todo [-] not planned ? unsure
16 # Basic Markdown - Block elements:
17 # -------------------------------
19 # - [x] Double space line breaks
20 # - [x] Proper block element nesting
22 # - [x] ATX-Style Headings
24 # - [x] Lists (ordered, unordered)
25 # - [x] Code blocks (using indention)
26 # - [x] Horizontal rules
27 # - [x] Verbatim HTML block (disabled by default)
29 # Basic Markdown - Inline elements:
30 # ---------------------------------
32 # - [x] Reference style links
33 # - [x] Emphasis *em*/**strong** (*Asterisk*, _Underscore_)
34 # - [x] `code`, also ``code containing `backticks` ``
35 # - [x] Images / reference style images
36 # - [x] <automatic links>
37 # - [x] backslash escapes
38 # - [x] Verbatim HTML inline (disabled by default)
41 # NOTE: Set the environment variable MD_HTML=true to enable verbatim HTML
43 # Extensions - Block elements:
44 # ----------------------------
45 # - ? Heading identifiers (php md, pandoc)
46 # - [x] Automatic heading identifiers (custom)
47 # - [x] Fenced code blocks (php md, pandoc)
48 # - [x] Fenced code attributes
50 # - ? Simple table (pandoc)
51 # - ? Multiline table (pandoc)
52 # - ? Grid table (pandoc)
53 # - ? Pipe table (php md pandoc)
54 # - [x] Line blocks (pandoc)
55 # - [x] Task lists (pandoc)
56 # - [ ] Definition lists (php md, pandoc)
57 # - [-] Numbered example lists (pandoc)
58 # - [-] Metadata blocks (pandoc)
59 # - [x] Metadata blocks (custom)
60 # - [x] Fenced Divs (pandoc)
62 # Extensions - Inline elements:
63 # ----------------------------
64 # - [x] Ignore embedded_underscores (php md, pandoc)
65 # - [x] ~~strikeout~~ (pandoc)
66 # - [x] ^Superscript^ ~Subscript~ (pandoc)
67 # - [-] Bracketed spans (pandoc)
68 # - [-] Inline attributes (pandoc)
69 # - [-] TEX-Math (pandoc)
70 # - ? Footnotes (php md)
71 # - ? Abbreviations (php md)
72 # - ? "Curly quotes" (smartypants)
73 # - [ ] em-dashes (--) (smartypants old)
74 # - ? ... three-dot ellipsis (smartypants)
75 # - [-] en-dash (smartypants)
76 # - [ ] Automatic em-dash / en-dash
77 # - [ ] Automatic -> Arrows <-
79 function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; }
81 function HTML ( text ) {
82 gsub( /&/, "\\&", text );
83 gsub( /</, "\\<", text );
84 gsub( />/, "\\>", text );
85 gsub( /"/, "\\"", text );
86 gsub( /'/, "\\'", text );
87 gsub( /\\/, "\\\", text );
91 function inline( line, LOCAL, len, code, href, guard ) {
92 nu = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\_]|_[[:alnum:]])*" # not underline (except when escaped)
93 na = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\\\*])*" # not asterisk (except when escaped)
94 ieu = "_([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])_" # inner <em> (underline)
95 isu = "__([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])__" # inner <strong> (underline)
96 iea = "\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*" # inner <em> (asterisk)
97 isa = "\\*\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*\\*" # inner <strong> (asterisk)
99 if ( line ~ /^$/ ) { # Recursion End
102 # omit processing of escaped characters
103 } else if ( line ~ /^\\[]\\`\*_\{\}\(\)#\+-\.![]/) {
104 return substr(line, 2, 1) inline( substr(line, 3) );
107 } else if ( match(line, /^ \n/) ) {
108 return "<br />\n" inline( substr(line, RLENGTH + 1) );
111 } else if ( match( line, /^`+/) ) {
113 guard = substr( line, 1, len )
114 if ( match(line, guard ".*" guard) ) {
115 code = substr( line, len + 1, match( substr(line, len + 1), guard ) - 1)
116 len = 2 * length(guard) + length(code)
117 # strip single surrounding white spaces
118 code = gensub( / (.*) /, "\\1", "1" , code)
119 # escape HTML within code span
120 gsub( /&/, "\\&", code ); gsub( /</, "\\<", code ); gsub( />/, "\\>", code );
121 return "<code>" code "</code>" inline( substr( line, len + 1 ) )
124 # quick links ("automatic links" in md doc)
125 } else if ( match( line, /^<[a-zA-Z]+:\/\/([-\.[:alnum:]]+)(:[0-9]*)?(\/[^>]*)?>/ ) ) {
127 href = HTML( substr( line, 2, len - 2) );
128 return "<a href=\"" href "\">" href "</a>" inline( substr( line, len + 1) );
131 } else if ( match(line, /^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/) ) {
133 text = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\1", "g", line);
134 href = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\2", "g", line);
135 title = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\4", "g", line);
137 return "<a href=\"" HTML(href) "\" title=\"" HTML(title) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
139 return "<a href=\"" HTML(href) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
142 # reference style links
143 } else if ( match(line, /^\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
145 text = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, line);
146 id = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, line);
147 if ( ! id ) id = text;
148 if ( rl_href[id] && rl_title[id] ) {
149 return "<a href=\"" HTML(rl_href[id]) "\" title=\"" HTML(rl_title[id]) "\">" inline(text) "</a>" inline( substr( line, len + 1) );
150 } else if ( rl_href[id] ) {
151 return "<a href=\"" HTML(rl_href[id]) "\">" inline(text) "</a>" inline( substr( line, len + 1) );
153 return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
157 } else if ( match(line, /^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/) ) {
159 text = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\1", "g", line);
160 href = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\2", "g", line);
161 title = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\4", "g", line);
163 return "<img src=\"" HTML(href) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\" />" inline( substr( line, len + 1) );
165 return "<img src=\"" HTML(href) "\" alt=\"" HTML(text) "\" />" inline( substr( line, len + 1) );
168 # reference style images
169 } else if ( match(line, /^!\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
171 text = gensub(/^!\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, line);
172 id = gensub(/^!\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, line);
173 if ( ! id ) id = text;
174 if ( rl_href[id] && rl_title[id] ) {
175 return "<img src=\"" HTML(rl_href[id]) "\" alt=\"" HTML(text) "\" title=\"" HTML(rl_title[id]) "\" />" inline( substr( line, len + 1) );
176 } else if ( rl_href[id] ) {
177 return "<img src=\"" HTML(rl_href[id]) "\" alt=\"" HTML(text) "\" />" inline( substr( line, len + 1) );
179 return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
182 # ~~strikeout~~ (pandoc)
183 } else if ( match(line, /^~~([[:graph:]]|[[:graph:]]([^~]|~[^~])*[[:graph:]])~~/) ) {
185 return "<del>" inline( substr( line, 3, len - 4 ) ) "</del>" inline( substr( line, len + 1 ) );
187 # ^superscript^ (pandoc)
188 } else if ( match(line, /^\^([^[:space:]^]|\\[ ^])+\^/) ) {
190 return "<sup>" inline( substr( line, 2, len - 2 ) ) "</sup>" inline( substr( line, len + 1 ) );
192 # ~subscript~ (pandoc)
193 } else if ( match(line, /^~([^[:space:]~]|\\[ ~])+~/) ) {
195 return "<sub>" inline( substr( line, 2, len - 2 ) ) "</sub>" inline( substr( line, len + 1 ) );
197 # ignore embedded underscores (pandoc, php md)
198 } else if ( match(line, "^[[:alnum:]](__|_)") ) {
199 return HTML(substr( line, 1, RLENGTH)) inline( substr(line, RLENGTH + 1) );
202 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__$") ) {
204 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
207 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__[[:space:][:punct:]]") ) {
209 return "<strong>" inline( substr( line, 3, len - 5 ) ) "</strong>" inline( substr( line, len) );
212 } else if ( match(line, "^\\*\\*(([^\\*[:space:]]|" iea ")|([^\\*[:space:]]|" iea ")(" na "|" iea ")*([^\\*[:space:]]|" iea "))\\*\\*") ) {
214 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
217 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_$") ) {
219 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
222 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_[[:space:][:punct:]]") ) {
224 return "<em>" inline( substr( line, 2, len - 3 ) ) "</em>" inline( substr( line, len ) );
227 } else if ( match(line, "^\\*(([^\\*[:space:]]|" isa ")|([^\\*[:space:]]|" isa ")(" na "|" isa ")*([^\\*[:space:]]|" isa "))\\*") ) {
229 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
232 } else if ( AllowMacros && match( line, /^<<([^>]|>[^>])+>>/) ) {
234 return macro( substr( line, 3, len - 4 ) ) inline(substr(line, len + 1));
236 # Verbatim inline HTML
237 } else if ( AllowHTML && match( line, /^(<!--([^-]|-[^-]|--[^>])*-->|<\?([^\?]|\?[^>])*\?>|<![A-Z][^>]*>|<!\[CDATA\[([^\]]|\][^\]]|\]\][^>])*\]\]>|<\/[A-Za-z][A-Za-z0-9-]*[[:space:]]*>|<[A-Za-z][A-Za-z0-9-]*([[:space:]]+[A-Za-z_:][A-Za-z0-9_\.:-]*([[:space:]]*=[[:space:]]*([[:space:]"'=<>`]+|"[^"]*"|'[^']*'))?)*[[:space:]]*\/?>)/) ) {
239 return substr( line, 1, len) inline(substr(line, len + 1));
241 # Literal HTML entities
242 } else if ( match( line, /^&([a-zA-Z]{2,32}|#[0-9]{1,7}|#[xX][0-9a-fA-F]{1,6});/) ) {
244 return substr( line, 1, len ) inline(substr(line, len + 1));
246 # Escape lone HTML character
247 } else if ( match( line, /^[&<>"']/) ) {
248 return HTML(substr(line, 1, 1)) inline(substr(line, 2));
250 # continue walk over string
252 return substr(line, 1, 1) inline( substr(line, 2) );
256 function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib ) {
257 gsub( /^\n+|\n+$/, "", block );
263 } else if ( AllowHTML && match( block, /(^|\n) ? ? ?(<!--([^-]|-[^-]|--[^>])*(-->|$)|<\?([^\?]|\?[^>])*(\?>|$)|<![A-Z][^>]*(>|$)|<!\[CDATA\[([^\]]|\][^\]]|\]\][^>])*(\]\]>|$))/) ) {
264 len = RLENGTH; st = RSTART;
265 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
268 } else if ( AllowHTML && match( tolower(block), /(^|\n) ? ? ?<\/?(address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[123456]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)([[:space:]\n>]|\/>)([^\n]|\n[ \t]*[^\n])*(\n[[:space:]]*\n|$)/) ) {
269 len = RLENGTH; st = RSTART;
270 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
273 } else if ( AllowHTML && match( tolower(block), /(^|\n) ? ? ?<(script|pre|style)([[:space:]\n>]).*(<\/script>|<\/pre>|<\/style>|$)/) ) {
274 len = RLENGTH; st = RSTART;
275 match( tolower(substr(block, st, len)), /(<\/script>|<\/pre>|<\/style>)/);
276 len = RSTART + RLENGTH;
277 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
280 } else if ( AllowHTML && match( block, /^ ? ? ?(<\/[A-Za-z][A-Za-z0-9-]*[[:space:]]*>|<[A-Za-z][A-Za-z0-9-]*([[:space:]]+[A-Za-z_:][A-Za-z0-9_\.:-]*([[:space:]]*=[[:space:]]*([[:space:]"'=<>`]+|"[^"]*"|'[^']*'))?)*[[:space:]]*\/?>)([[:space:]]*\n)([^\n]|\n[ \t]*[^\n])*(\n[[:space:]]*\n|$)/) ) {
281 len = RLENGTH; st = RSTART;
282 return substr(block, st, len) _block(substr(block, st + len));
284 # Metadata (custom, block starting with %something)
285 # Metadata is ignored but can be interpreted externally
286 } else if ( match(block, /^%[a-zA-Z]+([[:space:]][^\n]*)?(\n|$)(%[a-zA-Z]+([[:space:]][^\n]*)?(\n|$)|%([[:space:]][^\n]*)?(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
287 len = RLENGTH; st = RSTART;
288 return _block( substr( block, len + 1) );
290 # Blockquote (leading >)
291 } else if ( match( block, /^> /) ) {
292 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/);
293 len = RLENGTH; st = RSTART;
294 return "<blockquote>\n" _block( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) "</blockquote>\n\n" \
295 _block( substr(block, st + len) );
297 # Line Blocks (pandoc)
298 } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
299 len = RLENGTH; st = RSTART;
300 code = substr(block, 1, len);
301 gsub(/\n[[:space:]]+/, " ", code);
302 gsub(/\n\| /, "\n", code);
303 gsub(/^\| |\n$/, "", code);
304 return "<div class=\"line-block\">" gensub(/\n/, "<br />\n", "g", inline( code )) "</div>\n" \
305 _block( substr( block, len + 1) );
307 # Indented Code Block
308 } else if ( match(block, /^( |\t)[^\n]+(\n|$)(( |\t)[^\n]+(\n|$)|[ \t]*(\n|$))*/) ) {
309 len = RLENGTH; st = RSTART;
310 code = substr(block, 1, len);
311 gsub(/(^|\n)( |\t)/, "\n", code);
312 gsub(/^\n|\n+$/, "", code);
313 return "<pre><code>" HTML( code ) "</code></pre>\n" \
314 _block( substr( block, len + 1 ) );
316 # Fenced Divs (pandoc, custom)
317 } else if ( match( block, /^(:::+)/ ) ) {
318 guard = substr( block, 1, RLENGTH );
319 code = gensub(/^[^\n]+\n/, "", 1, block);
320 attrib = gensub(/^:::+[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\1", 1, block);
321 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib);
322 gsub(/(^ | $)/, "", attrib);
323 if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
324 len = RLENGTH; st = RSTART;
325 return "<div class=\"" attrib "\">" _block( substr(code, 1, st - 1) ) "</div>\n" \
326 _block( substr( code, st + len ) );
328 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
329 len = RLENGTH; st = RSTART;
330 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
331 _block( substr(block, st + len) );
334 # Fenced Code Block (pandoc)
335 } else if ( match( block, /^(~~~+|```+)/ ) ) {
336 guard = substr( block, 1, RLENGTH );
337 code = gensub(/^[^\n]+\n/, "", 1, block);
338 attrib = gensub(/^:::+[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\1", 1, block);
339 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib);
340 gsub(/(^ | $)/, "", attrib);
341 if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
342 len = RLENGTH; st = RSTART;
343 return "<pre><code class=\"" attrib "\">" HTML( substr(code, 1, st - 1) ) "</code></pre>\n" \
344 _block( substr( code, st + len ) );
346 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
347 len = RLENGTH; st = RSTART;
348 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
349 _block( substr(block, st + len) );
353 } else if ( match( block, "^ ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
354 "(([ \t]*\n)* ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
355 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
356 "|[^\n]+(\n|$))*" ) ) {
357 list = substr( block, 1, RLENGTH);
358 block = substr( block, RLENGTH + 1);
359 indent = length( gensub(/[-+*][ \t]+[^\n]+.*$/, "", 1, list) );
361 gsub("(^|\n) {0," indent "}", "\n", list);
362 return "\n<ul>\n" _list( substr(list, 2) ) "</ul>\n" _block( block );
365 } else if ( match( block, "^ ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
366 "(([ \t]*\n)* ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
367 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
368 "|[^\n]+(\n|$))*" ) ) {
369 list = substr( block, 1, RLENGTH);
370 block = substr( block, RLENGTH + 1);
371 indent = length( gensub(/([0-9]+|#)\.[ \t]+[^\n]+.*$/, "", 1, list) );
373 gsub("(^|\n) {0," indent "}", "\n", list);
374 return "\n<ol>\n" _list( substr(list, 2) ) "</ol>\n" _block( block );
376 # First Order Heading
377 } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
379 HL[1]++; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
380 return "<h1 id=\"" HL[1] " - " HTML(gensub( /\n.*$/, "", "g", block )) "\">" inline( gensub( /\n.*$/, "", "g", block ) ) "</h1>\n\n" \
381 _block( substr( block, len + 1 ) );
383 # Second Order Heading
384 } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
386 HL[2]++; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
387 return "<h2 id=\"" HL[1] "." HL[2] " - " HTML(gensub( /\n.*$/, "", "g", block )) "\">" inline( gensub( /\n.*$/, "", "g", block ) ) "</h2>\n\n" \
388 _block( substr( block, len + 1) );
391 } else if ( match( block, /^#{1,6}[ \t]*[^\n]+([ \t]*#*)(\n|$)/ ) ) {
393 hlvl = length( gensub( /^(#{1,6}).*$/, "\\1", "g", block ) );
394 htxt = gensub(/^#{1,6}[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[^\n#])+)([ \t]*#*)(\n.*)?$/, "\\1", 1, block);
395 HL[hlvl]++; for ( n = hlvl + 1; n < 7; n++) { HL[n] = 0;}
396 hid = HL[1]; for ( n = 2; n <= hlvl; n++) { hid = hid "." HL[n] ; }
397 return "<h" hlvl " id=\"" hid " - " HTML(htxt) "\">" inline( htxt ) "</h" hlvl ">\n\n" \
398 _block( substr( block, len + 1) );
401 } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) {
402 len = RLENGTH; st = RSTART;
403 return _block( substr(block, 1, st - 1) ) "\n" \
404 _block( substr(block, st + len) );
407 } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
408 len = RLENGTH; st = RSTART;
409 return _block(substr(block, 1, st - 1)) "<hr />\n" _block(substr(block, st + len));
413 return "<p>" inline(block) "</p>\n";
417 function _list( block, last, LOCAL, p) {
418 if ( ! length(block) ) return "";
419 gsub(/^([-+*]|[0-9]+\.|#\.)( ? ? ?|\t)/, "", block)
421 # slice next list item from input
422 if ( match( block, /\n([-+*]|[0-9]+\.|#\.)[ \t]+[^\n]+/) ) {
423 p = substr( block, 1, RSTART);
424 block = substr( block, RSTART + 1);
426 p = block; block = "";
428 sub( /\n +([-+*]|[0-9]+\.|#\.)/, "\n&", p );
430 # if this should be a paragraph item
431 # either previous item (last) or current item (p) contains blank lines
432 if (match(last, /\n[[:space:]]*\n/) || match(p, /\n[[:space:]]*\n/) ) {
433 last = p; p = _block(p);
435 last = p; p = _block(p);
436 sub( /^<p>/, "", p );
437 sub( /<\/p>\n/, "", p );
442 if ( p ~ /^\[ \].*/ ) { p = "<input type=checkbox disabled />" substr(p, 4); }
443 else if ( p ~ /^\[[xX]\].*/ ) { p = "<input type=checkbox disabled checked />" substr(p, 4); }
444 else if ( p ~ /^<p>\[ \].*/ ) { p = "<p><input type=checkbox disabled />" substr(p, 7); }
445 else if ( p ~ /^<p>\[[xX]\].*/ ) { p = "<p><input type=checkbox disabled checked />" substr(p, 7); }
446 return "<li>" p "</li>\n" _list( block, last );
451 file = ""; rl_href[""] = ""; rl_title[""] = "";
452 if (ENVIRON["MD_HTML"] == "true") { AllowHTML = "true"; }
453 HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
455 # Buffering of full file ist necessary, e.g. to find reference links
456 while (getline) { file = file $0 "\n"; }
457 # Clean up MS-DOS line breaks
458 gsub(/\r\n/, "\n", file);
460 # Fill array of reference links
462 re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
463 # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
464 while ( match(f, re_reflink ) ) {
465 rl_id = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
466 rl_href[rl_id] = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
467 rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
468 f = substr(f, RSTART + RLENGTH);
469 rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
470 if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
472 # Clear reflinks from File
473 while( gsub(re_reflink, "\n", file ) );
474 # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
476 # Run Block Processing -> The Actual Markdown!
477 printf "%s", _block( file );