]> git.plutz.net Git - cgilite/blob - markdown.awk
bugfix: endless loop in _startlist because of lacking indention removal
[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> terminates 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, text, 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       gsub( /^ | $/, "", 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   match(hstack, /([0-9]+( [0-9]+){5})$/); split( substr(hstack, RSTART),  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   sub(/([0-9]+( [0-9]+){5})$/, "", hstack);
323   hstack = hstack 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   match(hstack, /([0-9]+( [0-9]+){5})$/); split( substr(hstack, RSTART),  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     text = substr(block, 1, st - 1); gsub( /(^|\n)> /, "\n", text );
386     text = _nblock( text ); gsub( /^\n|\n$/, "", text )
387     return "<blockquote>" text "</blockquote>\n\n" _block( substr(block, st + len) );
388
389   # Pipe Tables (pandoc / php md / gfm )
390   } else if ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?)\n" \
391                            "((\\|)?:?(-+:?[\\|+])+:?-+:?(\\|)?)\n" \
392                            "((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ) {
393     len = RLENGTH; st = RSTART;
394     #initialize empty arrays
395     split("", talign); split("", tarray);
396     cols = 0; cnt=0; ttext = "";
397
398     # table header and alignment
399     split( gensub( /(^\||\|$)/, "", "g", \
400            gensub( /(^|[^\\])\\\|/, "\\1\\&#x7C;", "g", \
401            substr(block, 1, match(block, /(\n|$)/)) \
402     )), tarray, /\|/);
403     block = substr(block, match(block, /(\n|$)/) + 1 );
404     cols = split( \
405            gensub( /(^\||\|$)/, "", "g", \
406            substr(block, 1, match(block, /(\n|$)/)) \
407     ), talign, /[+\|]/);
408     block = substr(block, match(block, /(\n|$)/) + 1 );
409
410     for( cnt = 1; cnt < cols; cnt++ ) {
411            if (match(talign[cnt], /:-+:/)) talign[cnt]="center";
412       else if (match(talign[cnt],  /-+:/)) talign[cnt]="right";
413       else if (match(talign[cnt],  /:-+/)) talign[cnt]="left";
414       else talign[cnt]="";
415     }
416
417     ttext = "<thead>\n<tr>"
418     for (cnt = 1; cnt < cols; cnt++)
419       ttext = ttext "<th align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</th>"
420     ttext = ttext "</tr>\n</thead><tbody>\n"
421
422     while ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ){
423       split( gensub( /(^\||\|$)/, "", "g", \
424              gensub( /(^|[^\\])\\\|/, "\\1\\&#x7C;", "g", \
425              substr(block, 1, match(block, /(\n|$)/)) \
426       )), tarray, /\|/);
427       block = substr(block, match(block, /(\n|$)/) + 1 );
428
429       ttext = ttext "<tr>"
430       for (cnt = 1; cnt < cols; cnt++)
431         ttext = ttext "<td align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</td>"
432       ttext = ttext "</tr>\n"
433     }
434     return "<table>" ttext "</tbody></table>\n" _block(block);
435
436   # Grid Tables (pandoc)
437   # (with, and without header)
438   } else if ( match( block, "^\\+(-+\\+)+\n" \
439                             "(\\|([^\n]+\\|)+\n)+" \
440                             "(\\+(:?=+:?\\+)+)\n" \
441                            "((\\|([^\n]+\\|)+\n)+" \
442                              "\\+(-+\\+)+(\n|$))+" \
443                    ) || \
444               match( block, "^()()()" \
445                             "(\\+(:?-+:?\\+)+)\n" \
446                            "((\\|([^\n]+\\|)+\n)+" \
447                              "\\+(-+\\+)+(\n|$))+" \
448   ) ) {
449     len = RLENGTH; st = RSTART;
450     #initialize empty arrays
451     split("", talign); split("", tarray); split("", tread);
452     cols = 0; cnt=0; ttext = "";
453
454     # Column Count
455     cols = split(   gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block), tread, /\+/) - 2;
456     # debug(" Cols: " gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block ));
457
458     # table alignment
459     split( gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block ), talign, /\+/ );
460     # debug("Align: " gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block ));
461
462     for (cnt = 1; cnt <= cols; cnt++) {
463            if (match(talign[cnt], /:(-+|=+):/)) talign[cnt]="center";
464       else if (match(talign[cnt],  /(-+|=+):/)) talign[cnt]="right";
465       else if (match(talign[cnt], /:(-+|=+)/ )) talign[cnt]="left";
466       else talign[cnt]="";
467     }
468
469     if ( match(block, "^\\+(-+\\+)+\n" \
470                       "(\\|([^\n]+\\|)+\n)+" \
471                        "\\+(:?=+:?\\+)+\n" \
472                      "((\\|([^\n]+\\|)+\n)+" \
473                        "\\+(-+\\+)+(\n|$))+" \
474     ) ) {
475       # table header
476       block = substr(block, match(block, /(\n|$)/) + 1 );
477       while ( match(block, "^\\|([^\n]+\\|)+\n") ) {
478         split( gensub( /(^\||\|$)/, "", "g", \
479                  gensub( /(^|[^\\])\\\|/, "\\1\\&#x7C;", "g", \
480                    substr(block, 1, match(block, /(\n|$)/)) \
481         )), tread, /\|/);
482         block = substr(block, match(block, /(\n|$)/) + 1 );
483         for (cnt = 1; cnt <= cols; cnt++)
484           tarray[cnt] = tarray[cnt] "\n" tread[cnt];
485       }
486
487       ttext = "<thead>\n<tr>"
488       for (cnt = 1; cnt <= cols; cnt++)
489         ttext = ttext "<th align=\"" talign[cnt] "\">" _nblock(tarray[cnt]) "</th>"
490       ttext = ttext "</tr>\n</thead>"
491     }
492
493     # table body
494     block = substr(block, match(block, /(\n|$)/) + 1 );
495     ttext = ttext "<tbody>\n"
496
497     while ( match(block, /^((\|([^\n]+\|)+\n)+\+(-+\+)+(\n|$))+/ ) ){
498       split("", tarray);
499       while ( match(block, /^\|([^\n]+\|)+\n/) ) {
500         split( gensub( /(^\||\|$)/, "", "g", \
501                gensub( /(^|[^\\])\\\|/, "\\1\\&#x7C;", "g", \
502                substr(block, 1, match(block, /(\n|$)/)) \
503         )), tread, /\|/);
504         block = substr(block, match(block, /(\n|$)/) + 1 );
505         for (cnt = 1; cnt <= cols; cnt++)
506           tarray[cnt] = tarray[cnt] "\n" tread[cnt];
507       }
508       block = substr(block, match(block, /(\n|$)/) + 1 );
509
510       ttext = ttext "<tr>"
511       for (cnt = 1; cnt <= cols; cnt++)
512         ttext = ttext "<td align=\"" talign[cnt] "\">" _nblock(tarray[cnt]) "</td>"
513       ttext = ttext "</tr>\n"
514     }
515     return "<table>" ttext "</tbody></table>\n" _nblock(block);
516
517   # Line Blocks (pandoc)
518   } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
519     len = RLENGTH; st = RSTART;
520
521     text = substr(block, 1, len); gsub(/\n[[:space:]]+/, " ", text);
522     gsub(/\n\| /, "\n", text); gsub(/^\| |\n$/, "", text);
523     text = inline(text); gsub(/\n/, "<br>\n", text);
524
525     return "<div class=\"line-block\">" text "</div>\n" _block( substr( block, len + 1) );
526
527   # Indented Code Block
528   } else if ( match(block, /^(    |\t)( *\t*[^ \t\n]+ *\t*)+(\n|$)((    |\t)[^\n]+(\n|$)|[ \t]*(\n|$))*/) ) {
529     len = RLENGTH; st = RSTART;
530     code = substr(block, 1, len);
531     gsub(/(^|\n)(    |\t)/, "\n", code);
532     gsub(/^\n|\n+$/, "", code);
533     return "<pre><code>" HTML( code ) "</code></pre>\n" \
534            _block( substr( block, len + 1 ) );
535
536   # Fenced Divs (pandoc, custom)
537   } else if ( match( block, /^(:::+)/ ) ) {
538     guard = substr( block, 1, RLENGTH );
539     code = block; sub(/^[^\n]+\n/, "", code);
540     attrib = gensub(/^:::+[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\1", 1, block);
541     gsub(/[^a-zA-Z0-9_-]+/, " ", attrib);
542     gsub(/(^ | $)/, "", attrib);
543     if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
544       len = RLENGTH; st = RSTART;
545       return "<div class=\"" attrib "\">" _nblock( substr(code, 1, st - 1) ) "</div>\n" \
546              _block( substr( code, st + len ) );
547     } else {
548       match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
549       len = RLENGTH; st = RSTART;
550       return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
551              _block( substr(block, st + len) );
552     }
553
554   # Fenced Code Block (pandoc)
555   } else if ( match( block, /^(~~~+|```+)/ ) ) {
556     guard = substr( block, 1, RLENGTH );
557     code = gensub(/^[^\n]+\n/, "", 1, block);
558     attrib = gensub(/^(~~~+|```+)[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\2", 1, block);
559     gsub(/[^a-zA-Z0-9_-]+/, " ", attrib);
560     gsub(/(^ | $)/, "", attrib);
561     if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
562       len = RLENGTH; st = RSTART;
563       return "<pre><code class=\"" attrib "\">" HTML( substr(code, 1, st - 1) ) "</code></pre>\n" \
564              _block( substr( code, st + len ) );
565     } else {
566       match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
567       len = RLENGTH; st = RSTART;
568       return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
569              _block( substr(block, st + len) );
570     }
571
572   # First Order Heading H1 + Attrib
573   } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n|$)/ ) ) {
574     len = RLENGTH; text = attrib = block;
575     sub(/([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "", text);
576     sub(/\}\n===+(\n.*)?$/, "", attrib); sub(/^([^\n]+)[ \t]*\{/, "", attrib);
577     gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
578
579     return headline(1, text, attrib) _block( substr( block, len + 1 ) );
580
581   # First Order Heading H1
582   } else if ( match( block, /^([^\n]+)\n===+(\n|$)/ ) ) {
583     len = RLENGTH; text = substr(block, 1, len);
584     sub(/\n===+(\n.*)?$/, "", text);
585
586     return headline(1, text, 0) _block( substr( block, len + 1 ) );
587
588   # Second Order Heading H2 + Attrib
589   } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n|$)/ ) ) {
590     len = RLENGTH; text = attrib = block;
591     sub(/([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "", text);
592     sub(/\}\n---+(\n.*)?$/, "", attrib); sub(/^([^\n]+)[ \t]*\{/, "", attrib);
593     gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
594
595     return headline(2, text, attrib) _block( substr( block, len + 1) );
596
597   # Second Order Heading H2
598   } else if ( match( block, /^([^\n]+)\n---+(\n|$)/ ) ) {
599     len = RLENGTH; text = substr(block, 1, len);
600     sub(/\n---+(\n.*)?$/, "", text);
601
602     return headline(2, text, 0) _block( substr( block, len + 1) );
603
604   # Nth Order Heading H1 H2 H3 H4 H5 H6 + Attrib
605   } else if ( match( block, /^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n|$)/ ) ) {
606     len = RLENGTH; text = attrib = substr(block, 1, len);
607     match(block, /^#{1,6}/); n = RLENGTH;
608
609     sub(/^(#{1,6})[ \t]*/, "", text);   sub(/[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "", text);
610     sub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*[ \t]*\{/, "", attrib);
611     sub(/\})(\n.*)?$/, "", attrib);
612     gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
613
614     return headline( n, text, attrib ) _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; text = substr(block, 1, len);
619     match(block, /^#{1,6}/); n = RLENGTH;
620     sub(/^(#{1,6})[ \t]*/, "", text); sub(/[ \t]*#*(\n.*)?$/, "", text);
621
622     return headline( n, text, 0 ) _block( substr( block, len + 1) );
623
624   # block images (wrapped in <figure>)
625   } else if ( match(block, /^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n|$)/) ) {
626     len = RLENGTH;
627     text   = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\1", "g", block);
628     href   = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\2", "g", block);
629     title  = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\4", "g", block);
630     attrib = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\6", "g", block);
631     if ( title && attrib ) {
632       return "<figure data-src=\"" URL(href, 1) "\" class=\"" HTML(attrib) "\">" \
633                "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
634                "<figcaption>" inline(title) "</figcaption>" \
635              "</figure>\n\n" \
636              _block( substr( block, len + 1) );
637     } else if ( title ) {
638       return "<figure data-src=\"" URL(href, 1) "\">" \
639                "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
640                "<figcaption>" inline(title) "</figcaption>" \
641              "</figure>\n\n" \
642              _block( substr( block, len + 1) );
643     } else if ( attrib ) {
644       return "<figure data-src=\"" URL(href, 1) "\" class=\"" HTML(attrib) "\">" \
645                "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
646              "</figure>\n\n" \
647              _block( substr( block, len + 1) );
648     } else {
649       return "<figure data-src=\"" URL(href, 1) "\">" \
650                "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
651              "</figure>\n\n" \
652              _block( substr( block, len + 1) );
653     }
654
655   # reference style images (block)
656   } else if ( match(line, /^!\[([^]]*)\] ?\[([^]]*)\](\n|$)/ ) ) {
657     len = RLENGTH;
658     text = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\1", 1, block);
659       id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\2", 1, block);
660     if ( ! id ) id = text;
661     if ( rl_href[id] && rl_title[id] ) {
662       return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
663                "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
664                "<figcaption>" inline(rl_title[id]) "</figcaption>" \
665              "</figure>\n\n" \
666              _block( substr( block, len + 1) );
667     } else if ( rl_href[id] ) {
668       return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
669                "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
670              "</figure>\n\n" \
671              _block( substr( block, len + 1) );
672     } else {
673       return "<p>" HTML(substr(block, 1, len)) "</p>\n" _block( substr(block, len + 1) );
674     }
675
676   # Macros (standalone <<macro>> calls handled as block, so they are not wrapped in paragraph)
677   } else if ( AllowMacros && match( block, /^<<(([^>]|>[^>])+)>>(\n|$)/) ) {
678     len = RLENGTH;
679     text = gensub(/^<<(([^>]|>[^>])+)>>(\n.*)?$/, "\\1", 1, block);
680     return macro(text) _block(substr(block, len + 1) );
681
682   # Definition list
683   } else if (match( block, "^(([ \t]*\n)*[^:\n \t][^\n]+\n" \
684                            "([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
685                           "(([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
686                            "|[^:\n \t][^\n]+(\n|$)" \
687                            "|( ? ? ?\t|  +)[^\n]+(\n|$)" \
688                            "|([ \t]*\n)+( ? ? ?\t|  +)[^\n]+(\n|$))*)+" \
689   )) {
690     list = substr( block, 1, RLENGTH); block = substr( block, RLENGTH + 1);
691     return "\n<dl>\n" _dlist( list ) "</dl>\n" _block( block );
692
693   # Unordered list types
694   } else if ( text = _startlist( block, "ul", "-",   "([+*]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) {
695     return text;
696   } else if ( text = _startlist( block, "ul", "\\+", "([-*]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) {
697     return text;
698   } else if ( text = _startlist( block, "ul", "\\*", "([-+]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) {
699     return text;
700
701   # Ordered list types
702   } else if ( text = _startlist( block, "ol", "[0-9]+\\.", "([-+*]|#\\.|[0-9]+\\)|#\\))") ) {
703     return text;
704   } else if ( text = _startlist( block, "ol", "[0-9]+\\)", "([-+*]|[0-9]+\\.|#\\.|#\\))") ) {
705     return text;
706   } else if ( text = _startlist( block, "ol", "#\\.", "([-+*]|[0-9]+\\.|[0-9]+\\)|#\\))") ) {
707     return text;
708   } else if ( text = _startlist( block, "ol", "#\\)", "([-+*]|[0-9]+\\.|#\\.|[0-9]+\\))") ) {
709     return text;
710
711   # Split paragraphs
712   } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) {
713     len = RLENGTH; st = RSTART;
714     return _block( substr(block, 1, st - 1) ) "\n" \
715            _block( substr(block, st + len) );
716
717   # Horizontal rule
718   } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
719     len = RLENGTH; st = RSTART;
720     return _block(substr(block, 1, st - 1)) "<hr>\n" _block(substr(block, st + len));
721
722   # Plain paragraph
723   } else {
724     return "<p>" inline(block) "</p>\n";
725   }
726 }
727
728 function _startlist(block, type, mark, exclude, LOCAL, st, len, list, indent, text) {
729   if (match( block, "(^|\n) ? ? ?" mark "[ \t][^\n]+(\n|$)" \
730          "(([ \t]*\n)* ? ? ?" mark "[ \t][^\n]+(\n|$)" \
731          "|([ \t]*\n)*( ? ? ?\t|  +)[^\n]+(\n|$)" \
732          "|[^\n \t][^\n]+(\n|$))*" ) ) {
733     st = RSTART; len = RLENGTH; list = substr( block, RSTART, RLENGTH);
734
735     sub("^\n", "", list); match(list, "^ ? ? ?"); indent = RLENGTH;
736     gsub( "(^|\n) {0," indent "}", "\n", list); sub("^\n", "", list);
737
738     text = substr(block, 1, st - 1); block = substr(block, st + len);
739     if (match( list, "\n" exclude "[ \t]" )) {
740       block = substr(list, RSTART + 1) block;
741       list = substr(list, 1, RSTART);
742     }
743
744     return _block( text ) "<" type ">\n" _list( list, mark ) "</" type ">\n" _block( block );
745   } else return 0;
746 }
747
748 function _list (block, mark, p, LOCAL, len, st, text, indent, task) {
749   if ( match(block, "^([ \t]*\n)*$")) return;
750
751   match(block, "^" mark "[ \t]"); indent = RLENGTH;
752   sub("^" mark "[ \t]", "", block);
753
754   if (match(block, /\n[ \t]*\n/)) p = 1;
755
756   match( block, "\n" mark "[ \t][^\n]+(\n|$)" );
757   st = (RLENGTH == -1) ? length(block) + 1 : RSTART;
758   text = substr(block, 1, st); block = substr(block, st + 1);
759
760   gsub("\n {0," indent "}", "\n", text);
761
762   task = match( text, /^\[ \]/   ) ? "<li class=\"task pending\"><input type=checkbox disabled>"      : \
763          match( text, /^\[-\]/   ) ? "<li class=\"task negative\"><input type=checkbox disabled>"     : \
764          match( text, /^\[\/\]/  ) ? "<li class=\"task partial\"><input type=checkbox disabled>"      : \
765          match( text, /^\[\?\]/  ) ? "<li class=\"task unsure\"><input type=checkbox disabled>"       : \
766          match( text, /^\[[xX]\]/) ? "<li class=\"task done\"><input type=checkbox disabled checked>" : "<li>";
767   sub(/^\[[-? \/xX]\]/, "", text);
768
769   text = _nblock( text );
770   if ( ! p && match( text, "^<p>(</p[^>]|</[^p]|<[^/]|[^<])*</p>\n$" ))
771      gsub( "(^<p>|</p>\n$)", "", text);
772
773   return task text "</li>\n" _list(block, mark, p);
774 }
775
776 function _dlist (block, LOCAL, len, st, text, indent, p) {
777   if (match( block, "^([ \t]*\n)*[^:\n \t][^\n]+\n" )) {
778     len = RLENGTH; text = substr(block, 1, len);
779     gsub( "(^\n*|\n*$)", "", text );
780     return "<dt>" inline( text ) "</dt>\n" _dlist( substr(block, len + 1) );
781   } else if (match( block, "^([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
782                          "([^:\n \t][^\n]+(\n|$)" \
783                          "|( ? ? ?\t|  +)[^\n]+(\n|$)" \
784                          "|([ \t]*\n)+( ? ? ?\t|  +)[^\n]+(\n|$))*" \
785   )) {
786     len = RLENGTH; text = substr(block, 1, len);
787     sub( "^([ \t]*\n)*", "", text);
788     match(text, "^ ? ? ?:(\t| +)"); indent = RLENGTH;
789     sub( "^ ? ? ?:(\t| +)", "", text);
790     gsub( "(^|\n) {0," indent "}", "\n", text );
791
792     text = _nblock(text);
793     if (match( text, "^<p>(</p[^>]|</[^p]|<[^/]|[^<])*</p>\n$" ))
794        gsub( "(^<p>|</p>\n$)", "", text);
795
796     return "<dd>" text "</dd>\n" _dlist( substr(block, len + 1) );
797   }
798 }
799
800 BEGIN {
801   # Global Vars
802   file = ""; rl_href[""] = ""; rl_title[""] = "";
803   if (ENVIRON["MD_HTML"] == "true") { AllowHTML = "true"; }
804   HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
805   # hls = "0 0 0 0 0 0";
806
807   # Buffering of full file ist necessary, e.g. to find reference links
808   while (getline) { file = file $0 "\n"; }
809   # Clean up MS-DOS line breaks
810   gsub(/\r\n/, "\n", file);
811
812   # Fill array of reference links
813   f = file; rl_id;
814   re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
815   # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
816   while ( match(f, re_reflink ) ) {
817     rl_id           = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
818     rl_href[rl_id]  = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
819     rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
820     f = substr(f, RSTART + RLENGTH);
821     rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
822     if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
823   }
824   # Clear reflinks from File
825   while( gsub(re_reflink, "\n", file ) );
826   # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
827
828   # Run Block Processing -> The Actual Markdown!
829   printf "%s", _nblock( file );
830 }