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