]> git.plutz.net Git - cgilite/blob - markdown.awk
fix: block nesting in lists
[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 # - [x] 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";
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, text, title, attrib, href, guard, code, indent, list ) {
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     text = substr(block, 1, len);
518     gsub(/\n[[:space:]]+/, " ", text);
519     gsub(/\n\| /, "\n", text);
520     gsub(/^\| |\n$/, "", text);
521     return "<div class=\"line-block\">" gensub(/\n/, "<br>\n", "g", inline( text )) "</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 = block; sub(/^[^\n]+\n/, "", code);
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   # First Order Heading H1 + Attrib
570   } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n|$)/ ) ) {
571     len = RLENGTH;
572     text   = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "\\1", 1, block)
573     attrib = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "\\3", 1, block)
574     gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
575
576     return headline(1, text, attrib) \
577            _block( substr( block, len + 1 ) );
578
579   # First Order Heading H1
580   } else if ( match( block, /^([^\n]+)\n===+(\n|$)/ ) ) {
581     len = RLENGTH;
582     text   = gensub(/^([^\n]+)\n===+(\n.*)?$/, "\\1", 1, block)
583
584     return headline(1, text, 0) \
585            _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) \
595            _block( substr( block, len + 1) );
596
597   # Second Order Heading H2
598   } else if ( match( block, /^([^\n]+)\n---+(\n|$)/ ) ) {
599     len = RLENGTH;
600     text   = gensub(/^([^\n]+)\n---+(\n.*)?$/, "\\1", 1, block)
601
602     return headline(2, text, 0) \
603            _block( substr( block, len + 1) );
604
605   # Nth Order Heading H1 H2 H3 H4 H5 H6 + Attrib
606   } else if ( match( block, /^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n|$)/ ) ) {
607     len = RLENGTH;
608     n      = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\1", 1, block);
609     text   = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\2", 1, block);
610     attrib = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\5", 1, block);
611     gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
612
613     return headline( length(n), text, attrib ) \
614            _block( substr( block, len + 1) );
615
616   # Nth Order Heading H1 H2 H3 H4 H5 H6
617   } else if ( match( block, /^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n|$)/ ) ) {
618     len = RLENGTH;
619     n      = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n.*)?$/, "\\1", 1, block);
620     text   = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n.*)?$/, "\\2", 1, block);
621
622     return headline( length(n), text, 0 ) \
623            _block( substr( block, len + 1) );
624
625   # block images (wrapped in <figure>)
626   } else if ( match(block, /^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n|$)/) ) {
627     len = RLENGTH;
628     text   = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\1", "g", block);
629     href   = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\2", "g", block);
630     title  = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\4", "g", block);
631     attrib = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\6", "g", block);
632     if ( title && attrib ) {
633       return "<figure data-src=\"" URL(href, 1) "\" class=\"" HTML(attrib) "\">" \
634                "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
635                "<figcaption>" inline(title) "</figcaption>" \
636              "</figure>\n\n" \
637              _block( substr( block, len + 1) );
638     } else if ( title ) {
639       return "<figure data-src=\"" URL(href, 1) "\">" \
640                "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
641                "<figcaption>" inline(title) "</figcaption>" \
642              "</figure>\n\n" \
643              _block( substr( block, len + 1) );
644     } else if ( attrib ) {
645       return "<figure data-src=\"" URL(href, 1) "\" class=\"" HTML(attrib) "\">" \
646                "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
647              "</figure>\n\n" \
648              _block( substr( block, len + 1) );
649     } else {
650       return "<figure data-src=\"" URL(href, 1) "\">" \
651                "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
652              "</figure>\n\n" \
653              _block( substr( block, len + 1) );
654     }
655
656   # reference style images (block)
657   } else if ( match(line, /^!\[([^]]*)\] ?\[([^]]*)\](\n|$)/ ) ) {
658     len = RLENGTH;
659     text = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\1", 1, block);
660       id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\2", 1, block);
661     if ( ! id ) id = text;
662     if ( rl_href[id] && rl_title[id] ) {
663       return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
664                "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
665                "<figcaption>" inline(rl_title[id]) "</figcaption>" \
666              "</figure>\n\n" \
667              _block( substr( block, len + 1) );
668     } else if ( rl_href[id] ) {
669       return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
670                "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
671              "</figure>\n\n" \
672              _block( substr( block, len + 1) );
673     } else {
674       return "<p>" HTML(substr(block, 1, len)) "</p>\n" _block( substr(block, len + 1) );
675     }
676
677   # Macros (standalone <<macro>> calls handled as block, so they are not wrapped in paragraph)
678   } else if ( AllowMacros && match( block, /^<<(([^>]|>[^>])+)>>(\n|$)/) ) {
679     len = RLENGTH;
680     text = gensub(/^<<(([^>]|>[^>])+)>>(\n.*)?$/, "\\1", 1, block);
681     return macro(text) _block(substr(block, len + 1) );
682
683   # Horizontal rule
684   } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
685     len = RLENGTH; st = RSTART;
686     return _block(substr(block, 1, st - 1)) "<hr>\n" _block(substr(block, st + len));
687
688   # Definition list
689   } else if (match( block, "^(([ \t]*\n)*[^:\n \t][^\n]+\n" \
690                            "([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
691                           "(([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
692                            "|[^:\n \t][^\n]+(\n|$)" \
693                            "|( ? ? ?\t|  +)[^\n]+(\n|$)" \
694                            "|([ \t]*\n)+( ? ? ?\t|  +)[^\n]+(\n|$))*)+" \
695   )) {
696     list = substr( block, 1, RLENGTH); block = substr( block, RLENGTH + 1);
697     return "\n<dl>\n" _dlist( list ) "</dl>\n" _block( block );
698
699   # Unordered list
700   } else if ( match( block, "(^|\n) ? ? ?[-+*][ \t][^\n]+(\n|$)" \
701                  "(([ \t]*\n)* ? ? ?[-+*][ \t][^\n]+(\n|$)" \
702                  "|([ \t]*\n)*( ? ? ?\t|  +)[^\n]+(\n|$)" \
703                  "|[^\n \t][^\n]+(\n|$))*" ) ) {
704     st = RSTART; len = RLENGTH; list = substr( block, RSTART, RLENGTH);
705     sub("^\n", "", list); match(list, "^ ? ? ?[-+*]"); indent = RLENGTH;
706     gsub( "(^|\n) {0," indent - 1 "}", "\n", list); sub("^\n", "", list);
707
708     text = substr(block, 1, st - 1); block = substr(block, st + len);
709     if (match( list, "\n([0-9]+\\.|#\\.)[ \t]" )) {
710       block = substr(list, RSTART + 1) block;
711       list = substr(list, 1, RSTART);
712     }
713
714     return _block( text ) "<ul>\n" _list( list, "[-+*]" ) "</ul>\n" _block( block );
715
716   # Ordered list
717   } else if ( match( block, "(^|\n) ? ? ?([0-9]+\\.|#\\.)[ \t][^\n]+(\n|$)" \
718                  "(([ \t]*\n)* ? ? ?([0-9]+\\.|#\\.)[ \t][^\n]+(\n|$)" \
719                  "|([ \t]*\n)*( ? ? ?\t|  +)[^\n]+(\n|$)" \
720                  "|[^\n \t][^\n]+(\n|$))*" ) ) {
721     st = RSTART; len = RLENGTH; list = substr( block, RSTART, RLENGTH);
722     sub("^\n", "", list); match(list, "^ ? ? ?[0-9#]"); indent = RLENGTH;
723     gsub( "(^|\n) {0," indent - 1 "}", "\n", list); sub("^\n", "", list);
724
725     text = substr(block, 1, st - 1); block = substr(block, st + len);
726     if (match( list, "\n[-+*][ \t]" )) {
727       block = substr(list, RSTART + 1) block;
728       list = substr(list, 1, RSTART);
729     }
730
731     return _block( text ) "<ol>\n" _list( list, "([0-9]+\\.|#\\.)" ) "</ol>\n" _block( block );
732
733   # Split paragraphs
734   } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) {
735     len = RLENGTH; st = RSTART;
736     return _block( substr(block, 1, st - 1) ) "\n" \
737            _block( substr(block, st + len) );
738
739   # Plain paragraph
740   } else {
741     return "<p>" inline(block) "</p>\n";
742   }
743 }
744
745 function _list (block, mark, LOCAL, len, st, text, indent, task) {
746   if ( match(block, "^([ \t]*\n)*$")) return;
747   match(block, "^" mark "[ \t]"); indent = RLENGTH;
748   sub("^" mark "[ \t]", "", block);
749
750   match( block, "\n" mark "[ \t][^\n]+(\n|$)" \
751       "(([ \t]*\n)* ? ? ?" mark "[ \t][^\n]+(\n|$)" \
752       "|([ \t]*\n)*( ? ? ?\t|  +)[^\n]+(\n|$)" \
753       "|[^\n \t][^\n]+(\n|$))*");
754   (RLENGTH == -1) ? st = length(block) + 1 : st = RSTART;
755   text = substr(block, 1, st); block = substr(block, st + 1);
756
757   gsub("\n {0," indent "}", "\n", text);
758
759   task = match( text, /^\[ \]/   ) ? "<li class=\"task pending\"><input type=checkbox disabled>"      : \
760          match( text, /^\[-\]/   ) ? "<li class=\"task negative\"><input type=checkbox disabled>"     : \
761          match( text, /^\[\/\]/  ) ? "<li class=\"task partial\"><input type=checkbox disabled>"      : \
762          match( text, /^\[\?\]/  ) ? "<li class=\"task unsure\"><input type=checkbox disabled>"       : \
763          match( text, /^\[[xX]\]/) ? "<li class=\"task done\"><input type=checkbox disabled checked>" : "<li>";
764   sub(/^\[[-? /xX]\]/, "", text);
765
766   text = _nblock( text );
767   if (match( text, "^<p>(</p[^>]|</[^p]|<[^/]|[^<])*</p>\n$" ))
768      gsub( "(^<p>|</p>\n$)", "", text);
769
770   return task text "</li>\n" _list(block, mark);
771 }
772
773 function _dlist (block, LOCAL, len, st, text, indent, p) {
774   if (match( block, "^([ \t]*\n)*[^:\n \t][^\n]+\n" )) {
775     len = RLENGTH; text = substr(block, 1, len);
776     gsub( "(^\n*|\n*$)", "", text );
777     return "<dt>" inline( text ) "</dt>\n" _dlist( substr(block, len + 1) );
778   } else if (match( block, "^([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
779                          "([^:\n \t][^\n]+(\n|$)" \
780                          "|( ? ? ?\t|  +)[^\n]+(\n|$)" \
781                          "|([ \t]*\n)+( ? ? ?\t|  +)[^\n]+(\n|$))*" \
782   )) {
783     len = RLENGTH; text = substr(block, 1, len);
784     sub( "^([ \t]*\n)*", "", text);
785     match(text, "^ ? ? ?:(\t| +)"); indent = RLENGTH;
786     sub( "^ ? ? ?:(\t| +)", "", text);
787     gsub( "(^|\n) {0," indent "}", "\n", text );
788
789     text = _nblock(text);
790     if (match( text, "^<p>(</p[^>]|</[^p]|<[^/]|[^<])*</p>\n$" ))
791        gsub( "(^<p>|</p>\n$)", "", text);
792
793     return "<dd>" text "</dd>\n" _dlist( substr(block, len + 1) );
794   }
795 }
796
797 BEGIN {
798   # Global Vars
799   file = ""; rl_href[""] = ""; rl_title[""] = "";
800   if (ENVIRON["MD_HTML"] == "true") { AllowHTML = "true"; }
801   HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
802   # hls = "0 0 0 0 0 0";
803
804   # Buffering of full file ist necessary, e.g. to find reference links
805   while (getline) { file = file $0 "\n"; }
806   # Clean up MS-DOS line breaks
807   gsub(/\r\n/, "\n", file);
808
809   # Fill array of reference links
810   f = file; rl_id;
811   re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
812   # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
813   while ( match(f, re_reflink ) ) {
814     rl_id           = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
815     rl_href[rl_id]  = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
816     rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
817     f = substr(f, RSTART + RLENGTH);
818     rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
819     if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
820   }
821   # Clear reflinks from File
822   while( gsub(re_reflink, "\n", file ) );
823   # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
824
825   # Run Block Processing -> The Actual Markdown!
826   printf "%s", _nblock( file );
827 }