]> git.plutz.net Git - cgilite/blob - markdown.awk
markdown: avoid adding newlines to blockquote, css: style for blockquote
[cgilite] / markdown.awk
1 #!/bin/awk -f
2 #!/opt/busybox/awk -f
3
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
7
8 # Supported Features / TODO:
9 # ==========================
10 # [x] done    [ ] todo    [-] not planned    ? unsure
11 #
12 # Basic Markdown - Block elements:
13 # -------------------------------
14 # - [x] Paragraphs
15 #   - [x] Double space line breaks
16 # - [x] Proper block element nesting
17 # - [x] Headings
18 # - [x] ATX-Style Headings
19 # - [x] Blockquotes
20 # - [x] Lists (ordered, unordered)
21 # - [x] Code blocks (using indention)
22 # - [x] Horizontal rules
23 # - [x] Verbatim HTML block (disabled by default)
24 #
25 # Basic Markdown - Inline elements:
26 # ---------------------------------
27 # - [x] Links
28 # - [x] Reference style links
29 # - [x] Emphasis *em*/**strong** (*Asterisk*, _Underscore_)
30 # - [x] `code`, also ``code containing `backticks` ``
31 # - [x] Images / reference style images
32 # - [x] <automatic links>
33 # - [x] backslash escapes
34 # - [x] Verbatim HTML inline (disabled by default)
35 # - [x] HTML escaping
36 #
37 # NOTE: Set the environment variable MD_HTML=true to enable verbatim HTML
38 #
39 # Extensions - Block elements:
40 # ----------------------------
41 # - [x] Automatic <section>-wrapping (custom)
42 # -  ?  Heading identifiers (php md, pandoc)
43 #   - [x] Heading attributes (custom)
44 # - [x] Automatic heading identifiers (custom)
45 # - [x] Fenced code blocks (php md, pandoc)
46 #   - [x] Fenced code attributes
47 # - [x] Images (as block elements, <figure>-wrapped) (custom)
48 #   - [x] reference style block images
49 # - [/] Tables
50 #   -  ?  Simple table (pandoc)
51 #   -  ?  Multiline table (pandoc)
52 #   - [x] Grid table (pandoc)
53 #     - [x] Headerless
54 #   - [x] Pipe table (php md, pandoc)
55 # - [x] Line blocks (pandoc)
56 # - [x] Task lists (pandoc, custom)
57 # - [ ] Definition lists (php md, pandoc)
58 # - [-] Numbered example lists (pandoc)
59 # - [-] Metadata blocks (pandoc)
60 # - [x] Metadata blocks (custom)
61 # - [x] Fenced Divs (pandoc)
62 #
63 # Extensions - Inline elements:
64 # ----------------------------
65 # - [x] Ignore embedded_underscores (php md, pandoc)
66 # - [x] ~~strikeout~~ (pandoc)
67 # - [x] ^Superscript^ ~Subscript~ (pandoc)
68 # - [-] Bracketed spans (pandoc)
69 #   - [-] Inline attributes (pandoc)
70 # - [x] Image attributes (custom, pandoc inspired, not for reference style)
71 # - [x] Wiki style links [[PageName]] / [[PageName|Link Text]]
72 # - [-] TEX-Math (pandoc)
73 # -  ?  Footnotes (php md)
74 # -  ?  Abbreviations (php md)
75 # -  ?  "Curly quotes" (smartypants)
76 # - [ ] em-dashes (--) (smartypants old)
77 # -  ?  ... three-dot ellipsis (smartypants)
78 # - [-] en-dash (smartypants)
79 # - [ ] Automatic em-dash / en-dash
80 # - [x] Automatic -> Arrows <- (custom)
81
82 function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; }
83
84 function HTML ( text ) {
85   gsub( /&/,  "\\&amp;",  text );
86   gsub( /</,  "\\&lt;",   text );
87   gsub( />/,  "\\&gt;",   text );
88   gsub( /"/,  "\\&quot;", text );
89   gsub( /'/,  "\\&#x27;", text );
90   gsub( /\\/, "\\&#x5C;", text );
91   return text;
92 }
93
94 function URL ( text, sharp ) {
95   gsub( /&/,  "%26",  text );
96   gsub( /"/,  "%22", text );
97   gsub( /'/,  "%27", text );
98   gsub( /`/,  "%60", text );
99   gsub( /\?/,  "%3F", text );
100   if (sharp) gsub( /#/,  "%23", text );
101   gsub( /\[/,  "%5B", text );
102   gsub( /\]/,  "%5D", text );
103   gsub( / /,  "%20", text );
104   gsub( /       /,  "%09", text );
105   gsub( /\\/, "%5C", text );
106   return text;
107 }
108
109 function inline( line, LOCAL, len, code, href, guard ) {
110   nu = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\_]|_[[:alnum:]])*"    # not underline (except when escaped)
111   na = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\\\*])*"  # not asterisk (except when escaped)
112   ieu =  "_([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])_"                 # inner <em> (underline)
113   isu = "__([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])__"                # inner <strong> (underline)
114   iea =    "\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*"     # inner <em> (asterisk)
115   isa = "\\*\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*\\*"  # inner <strong> (asterisk)
116
117   if ( line ~ /^$/ ) {  # Recursion End
118     return "";
119
120   # omit processing of escaped characters
121   } else if ( line ~ /^\\./) {
122     return HTML(substr(line, 2, 1)) inline( substr(line, 3) );
123
124   # hard brakes
125   } else if ( match(line, /^  \n/) ) {
126     return "<br>\n" inline( substr(line, RLENGTH + 1) );
127
128   #  ``code spans``
129   } else if ( match( line, /^`+/) ) {
130     len = RLENGTH
131     guard = substr( line, 1, len )
132     if ( match(line, guard ".*" guard) ) {
133       code = substr( line, len + 1, match( substr(line, len + 1), guard ) - 1)
134       len = 2 * length(guard) + length(code)
135       #  strip single surrounding white spaces
136       code = gensub( /^ | $/, "", "g" , code)
137       #  escape HTML within code span
138       gsub( /&/, "\\&amp;", code ); gsub( /</, "\\&lt;", code ); gsub( />/, "\\&gt;", code );
139       return "<code>" code "</code>" inline( substr( line, len + 1 ) )
140     }
141
142   # Wiki style links
143   } else if ( match( line, /^\[\[([^]|]+)(\|[^]]+)?\]\]/) ) {
144     len = RLENGTH;
145     href = gensub(/^\[\[([^]|]+)(\|([^]]+))?\]\]/, "\\1", 1, substr(line, 1, len) );
146     text = gensub(/^\[\[([^]|]+)(\|([^]]+))?\]\]/, "\\3", 1, substr(line, 1, len) );
147     if ( ! text ) text = href;
148     return "<a href=\"" URL(href) "\">" HTML(text) "</a>" inline( substr( line, len + 1) );
149
150   #  quick links ("automatic links" in md doc)
151   } else if ( match( line, /^<[a-zA-Z]+:\/\/([-\.[:alnum:]]+)(:[0-9]*)?(\/[^>]*)?>/ ) ) {
152     len = RLENGTH;
153     href = URL( substr( line, 2, len - 2) );
154     return "<a href=\"" href "\">" href "</a>" inline( substr( line, len + 1) );
155
156   # quick link email
157   } else if ( match( line, /^<[a-zA-Z0-9.!#$%&'\''*+\/=?^_`{|}~-]+@[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*>/ ) ) {
158     len = RLENGTH;
159     href = URL( substr( line, 2, len - 2) );
160     return "<a href=\"mailto:" href "\">" href "</a>" inline( substr( line, len + 1) );
161
162   # inline links
163   #                                 ,_______________________Image____________________________,
164   } else if ( match(line, /^\[([^]]+|!\[[^]]*\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/) ) {
165     len = RLENGTH;
166     text  = gensub(/^\[([^]]+|!\[[^]]*\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, \
167                    "\\1", 1, substr(line, 1, len) );
168     href  = gensub(/^\[([^]]+|!\[[^]]*\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, \
169                    "\\4", 1, substr(line, 1, len) );
170     title = gensub(/^\[([^]]+|!\[[^]]*\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, \
171                    "\\6", 1, substr(line, 1, len) );
172     if ( title ) {
173       return "<a href=\"" URL(href) "\" title=\"" HTML(title) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
174     } else {
175       return "<a href=\"" URL(href) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
176     }
177
178   # reference style links
179   } else if ( match(line, /^\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
180     len = RLENGTH;
181     text = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, substr(line, 1, len) );
182       id = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, substr(line, 1, len) );
183     if ( ! id ) id = text;
184     if ( rl_href[id] && rl_title[id] ) {
185       return "<a href=\"" URL(rl_href[id]) "\" title=\"" HTML(rl_title[id]) "\">" inline(text) "</a>" inline( substr( line, len + 1) );
186     } else if ( rl_href[id] ) {
187       return "<a href=\"" URL(rl_href[id]) "\">" inline(text) "</a>" inline( substr( line, len + 1) );
188     } else {
189       return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
190     }
191
192   # inline images
193   } else if ( match(line, /^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/) ) {
194     len = RLENGTH;
195     text   = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\1", "g", substr(line, 1, len) );
196     href   = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\2", "g", substr(line, 1, len) );
197     title  = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\4", "g", substr(line, 1, len) );
198     attrib = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\6", "g", substr(line, 1, len) );
199     if ( title && attrib ) {
200       return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\" class=\"" HTML(attrib) "\">" \
201              inline( substr( line, len + 1) );
202     } else if ( title ) {
203       return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\">" \
204              inline( substr( line, len + 1) );
205     } else if ( attrib ) {
206       return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
207              inline( substr( line, len + 1) );
208     } else {
209       return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
210              inline( substr( line, len + 1) );
211     }
212
213   # reference style images
214   } else if ( match(line, /^!\[([^]]*)\] ?\[([^]]*)\]/ ) ) {
215     len = RLENGTH;
216     text = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\].*/, "\\1", 1, substr(line, 1, len) );
217       id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\].*/, "\\2", 1, substr(line, 1, len) );
218     if ( ! id ) id = text;
219     if ( rl_href[id] && rl_title[id] ) {
220       return "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\" title=\"" HTML(rl_title[id]) "\">" \
221              inline( substr( line, len + 1) );
222     } else if ( rl_href[id] ) {
223       return "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
224              inline( substr( line, len + 1) );
225     } else {
226       return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
227     }
228
229   #  ~~strikeout~~ (pandoc)
230   } else if ( match(line, /^~~([[:graph:]]|[[:graph:]]([^~]|~[^~])*[[:graph:]])~~/) ) {
231     len = RLENGTH;
232     return "<del>" inline( substr( line, 3, len - 4 ) ) "</del>" inline( substr( line, len + 1 ) );
233
234   #  ^superscript^ (pandoc)
235   } else if ( match(line, /^\^([^[:space:]^]|\\[ ^])+\^/) ) {
236     len = RLENGTH;
237     return "<sup>" inline( substr( line, 2, len - 2 ) ) "</sup>" inline( substr( line, len + 1 ) );
238
239   #  ~subscript~ (pandoc)
240   } else if ( match(line, /^~([^[:space:]~]|\\[ ~])+~/) ) {
241     len = RLENGTH;
242     return "<sub>" inline( substr( line, 2, len - 2 ) ) "</sub>" inline( substr( line, len + 1 ) );
243
244   # ignore embedded underscores (pandoc, php md)
245   } else if ( match(line, "^[[:alnum:]](__|_)") ) {
246     return HTML(substr( line, 1, RLENGTH)) inline( substr(line, RLENGTH + 1) );
247
248   #  __strong__$
249   } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__$") ) {
250     len = RLENGTH;
251     return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
252
253   #  __strong__
254   } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__[[:space:][:punct:]]") ) {
255     len = RLENGTH;
256     return "<strong>" inline( substr( line, 3, len - 5 ) ) "</strong>" inline( substr( line, len) );
257
258   #  **strong**
259   } else if ( match(line, "^\\*\\*(([^\\*[:space:]]|" iea ")|([^\\*[:space:]]|" iea ")(" na "|" iea ")*([^\\*[:space:]]|" iea "))\\*\\*") ) {
260     len = RLENGTH;
261     return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
262
263   #  _em_$
264   } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_$") ) {
265     len = RLENGTH;
266     return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
267
268   #  _em_
269   } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_[[:space:][:punct:]]") ) {
270     len = RLENGTH;
271     return "<em>" inline( substr( line, 2, len - 3 ) ) "</em>" inline( substr( line, len ) );
272
273   #  *em*
274   } else if ( match(line, "^\\*(([^\\*[:space:]]|" isa ")|([^\\*[:space:]]|" isa ")(" na "|" isa ")*([^\\*[:space:]]|" isa "))\\*") ) {
275     len = RLENGTH;
276     return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
277
278   # Macros
279   } else if ( AllowMacros && match( line, /^<<([^>]|>[^>])+>>/) ) {
280     len = RLENGTH;
281     return macro( substr( line, 3, len - 4 ) ) inline(substr(line, len + 1));
282
283   # Verbatim inline HTML
284   } 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:]]*\/?>)/) ) {
285     len = RLENGTH;
286     return substr( line, 1, len) inline(substr(line, len + 1));
287
288   # Literal HTML entities
289   } else if ( match( line, /^&([a-zA-Z]{2,32}|#[0-9]{1,7}|#[xX][0-9a-fA-F]{1,6});/) ) {
290     len = RLENGTH;
291     return substr( line, 1, len ) inline(substr(line, len + 1));
292
293   # Arrows
294   } else if ( line ~ /^-->( |$)/) {  # ignore multidash-arrow
295     return "--&gt;" inline( substr(line, 4) );
296   } else if ( line ~ /^<-( |$)/) {
297     return "&larr;" inline( substr(line, 3) );
298   } else if ( line ~ /^->( |$)/) {
299     return "&rarr;" inline( substr(line, 3) );
300
301   # Escape lone HTML character
302   } else if ( match( line, /^[&<>"']/) ) {
303     return HTML(substr(line, 1, 1)) inline(substr(line, 2));
304
305   #  continue walk over string
306   } else {
307     return substr(line, 1, 1) inline( substr(line, 2) );
308   }
309 }
310
311 function headline( hlvl, htxt, attrib, LOCAL, sec, n, HL) {
312   split( gensub( /^(.* )?([0-9]+( [0-9]+){5})$/, "\\2" ,"1", hstack),  HL);
313
314   for ( n = hlvl; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
315   HL[hlvl]++; for ( n = hlvl + 1; n <= 6; n++) { HL[n] = 0;}
316   hid = ""; for ( n = 2; n <= blvl; n++) { hid = hid BL[n] "/"; }
317   hid = hid HL[1]; for ( n = 2; n <= hlvl; n++) { hid = hid "." HL[n] ; }
318   hid = hid ":" URL(htxt, 1);
319
320   hstack = gensub( /^(.* )?([0-9]+( [0-9]+){5})$/, "\\1" ,"1", hstack) \
321            HL[1] " " HL[2] " " HL[3] " " HL[4] " " HL[5] " " HL[6];
322
323   return sec "<section class=\"" (attrib ? "h" hlvl " " attrib : "h" hlvl)  "\" id=\"" hid "\">" \
324          "<h" hlvl (attrib ? " class=\"" attrib "\"" : "") ">" inline( htxt ) \
325          "<a class=\"anchor\" href=\"#" hid "\"></a>" \
326          "</h" hlvl ">\n\n";
327 }
328
329 # Nested Block, resets heading counters
330 function _nblock( block, LOCAL, hlsav, sec, n ) {
331   hlsav = hstack;
332   hstack = hstack " 0 0 0 0 0 0";
333
334   # Block Level
335   blvl++; BL[blvl]++;
336   for ( n = blvl + 1; n in BL; n++) { delete BL[n]; }
337
338   block = _block( block );
339   split( gensub( /^(.* )?([0-9]+( [0-9]+){5})$/, "\\2" ,"1", hstack),  HL);
340   sec = ""; for ( n = 1; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
341   hstack = hlsav; blvl--;
342   return block sec;
343 }
344
345 function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, attrib ) {
346   gsub( /^\n+|\n+$/, "", block );
347
348   if ( block == "" ) {
349     return "";
350
351   # HTML #2 #3 #4 $5
352   } else if ( AllowHTML && match( block, /(^|\n) ? ? ?(<!--([^-]|-[^-]|--[^>])*(-->|$)|<\?([^\?]|\?[^>])*(\?>|$)|<![A-Z][^>]*(>|$)|<!\[CDATA\[([^\]]|\][^\]]|\]\][^>])*(\]\]>|$))/) ) {
353     len = RLENGTH; st = RSTART;
354     return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
355
356   # HTML #6
357   } 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|$)/) ) {
358     len = RLENGTH; st = RSTART;
359     return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
360
361   # HTML #1
362   } else if ( AllowHTML && match( tolower(block), /(^|\n) ? ? ?<(script|pre|style)([[:space:]\n>]).*(<\/script>|<\/pre>|<\/style>|$)/) ) {
363     len = RLENGTH; st = RSTART;
364     match( tolower(substr(block, st, len)), /(<\/script>|<\/pre>|<\/style>)/);
365     len = RSTART + RLENGTH;
366     return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
367
368   # HTML #7
369   } 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|$)/) ) {
370     len = RLENGTH; st = RSTART;
371     return substr(block, st, len) _block(substr(block, st + len));
372
373   # Metadata (custom, block starting with %something)
374   # Metadata is ignored but can be interpreted externally
375   } else if ( match(block, /^%[a-zA-Z]+([[:space:]][^\n]*)?(\n|$)(%[a-zA-Z]+([[:space:]][^\n]*)?(\n|$)|%([[:space:]][^\n]*)?(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
376     len = RLENGTH; st = RSTART;
377     return  _block( substr( block, len + 1) );
378  
379   # Blockquote (leading >)
380   } else if ( match( block, /^> /) ) {
381     match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/);
382     len = RLENGTH; st = RSTART;
383     return "<blockquote>" gensub( /^\n|\n$/, "", "g",  _nblock( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) ) "</blockquote>\n\n" \
384            _block( substr(block, st + len) );
385
386   # Pipe Tables (pandoc / php md / gfm )
387   } else if ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?)\n" \
388                            "((\\|)?:?(-+:?[\\|+])+:?-+:?(\\|)?)\n" \
389                            "((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ) {
390     len = RLENGTH; st = RSTART;
391     #initialize empty arrays
392     split("", talign); split("", tarray);
393     cols = 0; cnt=0; ttext = "";
394
395     # table header and alignment
396     split( gensub( /(^\||\|$)/, "", "g", \
397            gensub( /(^|[^\\])\\\|/, "\\1\\&#x7C;", "g", \
398            substr(block, 1, match(block, /(\n|$)/)) \
399     )), tarray, /\|/);
400     block = substr(block, match(block, /(\n|$)/) + 1 );
401     cols = split( \
402            gensub( /(^\||\|$)/, "", "g", \
403            substr(block, 1, match(block, /(\n|$)/)) \
404     ), talign, /[+\|]/);
405     block = substr(block, match(block, /(\n|$)/) + 1 );
406
407     for( cnt = 1; cnt < cols; cnt++ ) {
408            if (match(talign[cnt], /:-+:/)) talign[cnt]="center";
409       else if (match(talign[cnt],  /-+:/)) talign[cnt]="right";
410       else if (match(talign[cnt],  /:-+/)) talign[cnt]="left";
411       else talign[cnt]="";
412     }
413
414     ttext = "<thead>\n<tr>"
415     for (cnt = 1; cnt < cols; cnt++)
416       ttext = ttext "<th align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</th>"
417     ttext = ttext "</tr>\n</thead><tbody>\n"
418
419     while ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ){
420       split( gensub( /(^\||\|$)/, "", "g", \
421              gensub( /(^|[^\\])\\\|/, "\\1\\&#x7C;", "g", \
422              substr(block, 1, match(block, /(\n|$)/)) \
423       )), tarray, /\|/);
424       block = substr(block, match(block, /(\n|$)/) + 1 );
425
426       ttext = ttext "<tr>"
427       for (cnt = 1; cnt < cols; cnt++)
428         ttext = ttext "<td align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</td>"
429       ttext = ttext "</tr>\n"
430     }
431     return "<table>" ttext "</tbody></table>\n" _block(block);
432
433   # Grid Tables (pandoc)
434   # (with, and without header)
435   } else if ( match( block, "^\\+(-+\\+)+\n" \
436                             "(\\|([^\n]+\\|)+\n)+" \
437                             "(\\+(:?=+:?\\+)+)\n" \
438                            "((\\|([^\n]+\\|)+\n)+" \
439                              "\\+(-+\\+)+(\n|$))+" \
440                    ) || \
441               match( block, "^()()()" \
442                             "(\\+(:?-+:?\\+)+)\n" \
443                            "((\\|([^\n]+\\|)+\n)+" \
444                              "\\+(-+\\+)+(\n|$))+" \
445   ) ) {
446     len = RLENGTH; st = RSTART;
447     #initialize empty arrays
448     split("", talign); split("", tarray); split("", tread);
449     cols = 0; cnt=0; ttext = "";
450
451     # Column Count
452     cols = split(   gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block), tread, /\+/) - 2;
453     # debug(" Cols: " gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block ));
454
455     # table alignment
456     split( gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block ), talign, /\+/ );
457     # debug("Align: " gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block ));
458
459     for (cnt = 1; cnt <= cols; cnt++) {
460            if (match(talign[cnt], /:(-+|=+):/)) talign[cnt]="center";
461       else if (match(talign[cnt],  /(-+|=+):/)) talign[cnt]="right";
462       else if (match(talign[cnt], /:(-+|=+)/ )) talign[cnt]="left";
463       else talign[cnt]="";
464     }
465
466     if ( match(block, "^\\+(-+\\+)+\n" \
467                       "(\\|([^\n]+\\|)+\n)+" \
468                        "\\+(:?=+:?\\+)+\n" \
469                      "((\\|([^\n]+\\|)+\n)+" \
470                        "\\+(-+\\+)+(\n|$))+" \
471     ) ) {
472       # table header
473       block = substr(block, match(block, /(\n|$)/) + 1 );
474       while ( match(block, "^\\|([^\n]+\\|)+\n") ) {
475         split( gensub( /(^\||\|$)/, "", "g", \
476                  gensub( /(^|[^\\])\\\|/, "\\1\\&#x7C;", "g", \
477                    substr(block, 1, match(block, /(\n|$)/)) \
478         )), tread, /\|/);
479         block = substr(block, match(block, /(\n|$)/) + 1 );
480         for (cnt = 1; cnt <= cols; cnt++)
481           tarray[cnt] = tarray[cnt] "\n" tread[cnt];
482       }
483
484       ttext = "<thead>\n<tr>"
485       for (cnt = 1; cnt <= cols; cnt++)
486         ttext = ttext "<th align=\"" talign[cnt] "\">" _nblock(tarray[cnt]) "</th>"
487       ttext = ttext "</tr>\n</thead>"
488     }
489
490     # table body
491     block = substr(block, match(block, /(\n|$)/) + 1 );
492     ttext = ttext "<tbody>\n"
493
494     while ( match(block, /^((\|([^\n]+\|)+\n)+\+(-+\+)+(\n|$))+/ ) ){
495       split("", tarray);
496       while ( match(block, /^\|([^\n]+\|)+\n/) ) {
497         split( gensub( /(^\||\|$)/, "", "g", \
498                gensub( /(^|[^\\])\\\|/, "\\1\\&#x7C;", "g", \
499                substr(block, 1, match(block, /(\n|$)/)) \
500         )), tread, /\|/);
501         block = substr(block, match(block, /(\n|$)/) + 1 );
502         for (cnt = 1; cnt <= cols; cnt++)
503           tarray[cnt] = tarray[cnt] "\n" tread[cnt];
504       }
505       block = substr(block, match(block, /(\n|$)/) + 1 );
506
507       ttext = ttext "<tr>"
508       for (cnt = 1; cnt <= cols; cnt++)
509         ttext = ttext "<td align=\"" talign[cnt] "\">" _nblock(tarray[cnt]) "</td>"
510       ttext = ttext "</tr>\n"
511     }
512     return "<table>" ttext "</tbody></table>\n" _nblock(block);
513
514   # Line Blocks (pandoc)
515   } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
516     len = RLENGTH; st = RSTART;
517     code = substr(block, 1, len);
518     gsub(/\n[[:space:]]+/, " ", code);
519     gsub(/\n\| /, "\n", code);
520     gsub(/^\| |\n$/, "", code);
521     return "<div class=\"line-block\">" gensub(/\n/, "<br>\n", "g", inline( code )) "</div>\n" \
522            _block( substr( block, len + 1) );
523
524   # Indented Code Block
525   } else if ( match(block, /^(    |\t)( *\t*[^ \t\n]+ *\t*)+(\n|$)((    |\t)[^\n]+(\n|$)|[ \t]*(\n|$))*/) ) {
526     len = RLENGTH; st = RSTART;
527     code = substr(block, 1, len);
528     gsub(/(^|\n)(    |\t)/, "\n", code);
529     gsub(/^\n|\n+$/, "", code);
530     return "<pre><code>" HTML( code ) "</code></pre>\n" \
531            _block( substr( block, len + 1 ) );
532
533   # Fenced Divs (pandoc, custom)
534   } else if ( match( block, /^(:::+)/ ) ) {
535     guard = substr( block, 1, RLENGTH );
536     code = gensub(/^[^\n]+\n/, "", 1, block);
537     attrib = gensub(/^:::+[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\1", 1, block);
538     gsub(/[^a-zA-Z0-9_-]+/, " ", attrib);
539     gsub(/(^ | $)/, "", attrib);
540     if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
541       len = RLENGTH; st = RSTART;
542       return "<div class=\"" attrib "\">" _nblock( substr(code, 1, st - 1) ) "</div>\n" \
543              _block( substr( code, st + len ) );
544     } else {
545       match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
546       len = RLENGTH; st = RSTART;
547       return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
548              _block( substr(block, st + len) );
549     }
550
551   # Fenced Code Block (pandoc)
552   } else if ( match( block, /^(~~~+|```+)/ ) ) {
553     guard = substr( block, 1, RLENGTH );
554     code = gensub(/^[^\n]+\n/, "", 1, block);
555     attrib = gensub(/^(~~~+|```+)[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\2", 1, block);
556     gsub(/[^a-zA-Z0-9_-]+/, " ", attrib);
557     gsub(/(^ | $)/, "", attrib);
558     if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
559       len = RLENGTH; st = RSTART;
560       return "<pre><code class=\"" attrib "\">" HTML( substr(code, 1, st - 1) ) "</code></pre>\n" \
561              _block( substr( code, st + len ) );
562     } else {
563       match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
564       len = RLENGTH; st = RSTART;
565       return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
566              _block( substr(block, st + len) );
567     }
568
569   # Unordered list
570   } else if ( match( block, "^ ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
571                             "(([ \t]*\n)* ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
572                             "|([ \t]*\n)*( ? ? ?\t|  +)[^\n]+(\n|$)" \
573                             "|[^\n]+(\n|$))*" ) ) {
574   list = substr( block, 1, RLENGTH);
575   block = substr( block, RLENGTH + 1);
576   indent = length( gensub(/[-+*][ \t]+[^\n]+.*$/, "", 1, list) );
577
578   gsub("(^|\n) {0," indent "}", "\n", list);
579   return "\n<ul>\n" _list( substr(list, 2) ) "</ul>\n" _block( block );
580
581   # Ordered list
582   } else if ( match( block, "^ ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
583                             "(([ \t]*\n)* ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
584                             "|([ \t]*\n)*( ? ? ?\t|  +)[^\n]+(\n|$)" \
585                             "|[^\n]+(\n|$))*" ) ) {
586   list = substr( block, 1, RLENGTH);
587   block = substr( block, RLENGTH + 1);
588   indent = length( gensub(/([0-9]+|#)\.[ \t]+[^\n]+.*$/, "", 1, list) );
589
590   gsub("(^|\n) {0," indent "}", "\n", list);
591   return "\n<ol>\n" _list( substr(list, 2) ) "</ol>\n" _block( block );
592
593   # First Order Heading H1 + Attrib
594   } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n|$)/ ) ) {
595     len = RLENGTH;
596     text   = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "\\1", 1, block)
597     attrib = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "\\3", 1, block)
598     gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
599
600     return headline(1, text, attrib) \
601            _block( substr( block, len + 1 ) );
602
603   # First Order Heading H1
604   } else if ( match( block, /^([^\n]+)\n===+(\n|$)/ ) ) {
605     len = RLENGTH;
606     text   = gensub(/^([^\n]+)\n===+(\n.*)?$/, "\\1", 1, block)
607
608     return headline(1, text, 0) \
609            _block( substr( block, len + 1 ) );
610
611   # Second Order Heading H2 + Attrib
612   } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n|$)/ ) ) {
613     len = RLENGTH;
614     text   = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "\\1", 1, block)
615     attrib = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "\\3", 1, block)
616     gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
617
618     return headline(2, text, attrib) \
619            _block( substr( block, len + 1) );
620
621   # Second Order Heading H2
622   } else if ( match( block, /^([^\n]+)\n---+(\n|$)/ ) ) {
623     len = RLENGTH;
624     text   = gensub(/^([^\n]+)\n---+(\n.*)?$/, "\\1", 1, block)
625
626     return headline(2, text, 0) \
627            _block( substr( block, len + 1) );
628
629   # Nth Order Heading H1 H2 H3 H4 H5 H6 + Attrib
630   } else if ( match( block, /^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n|$)/ ) ) {
631     len = RLENGTH;
632     n      = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\1", 1, block);
633     text   = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\2", 1, block);
634     attrib = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\5", 1, block);
635     gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
636
637     return headline( length(n), text, attrib ) \
638            _block( substr( block, len + 1) );
639
640   # Nth Order Heading H1 H2 H3 H4 H5 H6
641   } else if ( match( block, /^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n|$)/ ) ) {
642     len = RLENGTH;
643     n      = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n.*)?$/, "\\1", 1, block);
644     text   = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n.*)?$/, "\\2", 1, block);
645
646     return headline( length(n), text, 0 ) \
647            _block( substr( block, len + 1) );
648
649   # block images (wrapped in <figure>)
650   } else if ( match(block, /^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n|$)/) ) {
651     len = RLENGTH;
652     text   = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\1", "g", block);
653     href   = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\2", "g", block);
654     title  = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\4", "g", block);
655     attrib = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\6", "g", block);
656     if ( title && attrib ) {
657       return "<figure data-src=\"" URL(href, 1) "\" class=\"" HTML(attrib) "\">" \
658                "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
659                "<figcaption>" inline(title) "</figcaption>" \
660              "</figure>\n\n" \
661              _block( substr( block, len + 1) );
662     } else if ( title ) {
663       return "<figure data-src=\"" URL(href, 1) "\">" \
664                "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
665                "<figcaption>" inline(title) "</figcaption>" \
666              "</figure>\n\n" \
667              _block( substr( block, len + 1) );
668     } else if ( attrib ) {
669       return "<figure data-src=\"" URL(href, 1) "\" class=\"" HTML(attrib) "\">" \
670                "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
671              "</figure>\n\n" \
672              _block( substr( block, len + 1) );
673     } else {
674       return "<figure data-src=\"" URL(href, 1) "\">" \
675                "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
676              "</figure>\n\n" \
677              _block( substr( block, len + 1) );
678     }
679
680   # reference style images (block)
681   } else if ( match(line, /^!\[([^]]*)\] ?\[([^]]*)\](\n|$)/ ) ) {
682     len = RLENGTH;
683     text = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\1", 1, block);
684       id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\2", 1, block);
685     if ( ! id ) id = text;
686     if ( rl_href[id] && rl_title[id] ) {
687       return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
688                "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
689                "<figcaption>" inline(rl_title[id]) "</figcaption>" \
690              "</figure>\n\n" \
691              _block( substr( block, len + 1) );
692     } else if ( rl_href[id] ) {
693       return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
694                "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
695              "</figure>\n\n" \
696              _block( substr( block, len + 1) );
697     } else {
698       return "<p>" HTML(substr(block, 1, len)) "</p>\n" _block( substr(block, len + 1) );
699     }
700
701   # Macros (standalone <<macro>> calls handled as block, so they are not wrapped in paragraph)
702   } else if ( AllowMacros && match( block, /^<<(([^>]|>[^>])+)>>(\n|$)/) ) {
703     len = RLENGTH;
704     text = gensub(/^<<(([^>]|>[^>])+)>>(\n.*)?$/, "\\1", 1, block);
705     return macro(text) _block(substr(block, len + 1) );
706
707   # Split paragraphs
708   } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) {
709     len = RLENGTH; st = RSTART;
710     return _block( substr(block, 1, st - 1) ) "\n" \
711            _block( substr(block, st + len) );
712
713   # Horizontal rule
714   } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
715     len = RLENGTH; st = RSTART;
716     return _block(substr(block, 1, st - 1)) "<hr>\n" _block(substr(block, st + len));
717
718   # Plain paragraph
719   } else {
720     return "<p>" inline(block) "</p>\n";
721   }
722 }
723
724 function _list( block, last, LOCAL, p) {
725   if ( ! length(block) ) return "";
726   gsub(/^([-+*]|[0-9]+\.|#\.)(  ? ? ?|\t)/, "", block)
727
728   # slice next list item from input
729   if ( match( block, /\n([-+*]|[0-9]+\.|#\.)[ \t]+[^\n]+/) ) {
730     p = substr( block, 1, RSTART);
731     block = substr( block, RSTART + 1);
732   } else {
733     p = block; block = "";
734   }
735   sub( /\n +([-+*]|[0-9]+\.|#\.)/, "\n&", p );
736
737   # if this should be a paragraph item
738   # either previous item (last) or current item (p) contains blank lines
739   if (match(last, /\n[[:space:]]*\n/) || match(p, /\n[[:space:]]*\n/) ) {
740     last = p; p = _nblock(p);
741   } else {
742     last = p; p = _nblock(p);
743     sub( /^<p>/, "", p );
744     sub( /<\/p>\n/, "", p );
745   }
746   sub( /\n$/, "", p );
747
748   # Task List (pandoc, custom)
749          if ( p ~ /^\[ \].*/ )       { return "<li class=\"task pending\"><input type=checkbox disabled>" \
750                                               substr(p, 4) "</li>\n" _list( block, last );
751   } else if ( p ~ /^\[-\].*/ )       { return "<li class=\"task negative\"><input type=checkbox disabled>" \
752                                               substr(p, 4) "</li>\n" _list( block, last );
753   } else if ( p ~ /^\[\?\].*/ )      { return "<li class=\"task unsure\"><input type=checkbox disabled>" \
754                                               substr(p, 4) "</li>\n" _list( block, last );
755   } else if ( p ~ /^\[\/\].*/ )      { return "<li class=\"task partial\"><input type=checkbox disabled>" \
756                                               substr(p, 4) "</li>\n" _list( block, last );
757   } else if ( p ~ /^\[[xX]\].*/ )    { return "<li class=\"task done\"><input type=checkbox disabled checked>" \
758                                             substr(p, 4) "</li>\n" _list( block, last );
759   } else if ( p ~ /^<p>\[ \].*/ )    { return "<li class=\"task pending\"><p><input type=checkbox disabled>" \
760                                               substr(p, 7) "</li>\n" _list( block, last );
761   } else if ( p ~ /^<p>\[-\].*/ )    { return "<li class=\"task negative\"><p><input type=checkbox disabled>" \
762                                               substr(p, 7) "</li>\n" _list( block, last );
763   } else if ( p ~ /^<p>\[\?\].*/ )   { return "<li class=\"task unsure\"><p><input type=checkbox disabled>" \
764                                               substr(p, 7) "</li>\n" _list( block, last );
765   } else if ( p ~ /^<p>\[\/\].*/ )   { return "<li class=\"task partial\"><p><input type=checkbox disabled>" \
766                                               substr(p, 7) "</li>\n" _list( block, last );
767   } else if ( p ~ /^<p>\[[xX]\].*/ ) { return "<li class=\"task done\"><p><input type=checkbox disabled checked>" \
768                                               substr(p, 7) "</li>\n" _list( block, last );
769   } else { return "<li>" p "</li>\n" _list( block, last ); }
770 }
771
772 BEGIN {
773   # Global Vars
774   file = ""; rl_href[""] = ""; rl_title[""] = "";
775   if (ENVIRON["MD_HTML"] == "true") { AllowHTML = "true"; }
776   HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
777   # hls = "0 0 0 0 0 0";
778
779   # Buffering of full file ist necessary, e.g. to find reference links
780   while (getline) { file = file $0 "\n"; }
781   # Clean up MS-DOS line breaks
782   gsub(/\r\n/, "\n", file);
783
784   # Fill array of reference links
785   f = file; rl_id;
786   re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
787   # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
788   while ( match(f, re_reflink ) ) {
789     rl_id           = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
790     rl_href[rl_id]  = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
791     rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
792     f = substr(f, RSTART + RLENGTH);
793     rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
794     if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
795   }
796   # Clear reflinks from File
797   while( gsub(re_reflink, "\n", file ) );
798   # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
799
800   # Run Block Processing -> The Actual Markdown!
801   printf "%s", _nblock( file );
802 }