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
8 # Supported Features / TODO:
9 # ==========================
10 # [x] done [ ] todo [-] not planned ? unsure
12 # Basic Markdown - Block elements:
13 # -------------------------------
15 # - [x] Double space line breaks
16 # - [x] Proper block element nesting
18 # - [x] ATX-Style Headings
20 # - [x] Lists (ordered, unordered)
21 # - [x] Code blocks (using indention)
22 # - [x] Horizontal rules
23 # - [x] Verbatim HTML block (disabled by default)
25 # Basic Markdown - Inline elements:
26 # ---------------------------------
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)
37 # NOTE: Set the environment variable MD_HTML=true to enable verbatim HTML
39 # Extensions - Block elements:
40 # ----------------------------
41 # - ? Heading identifiers (php md, pandoc)
42 # - [x] Automatic heading identifiers (custom)
43 # - [x] Fenced code blocks (php md, pandoc)
44 # - [x] Fenced code attributes
46 # - ? Simple table (pandoc)
47 # - ? Multiline table (pandoc)
48 # - [x] Grid table (pandoc)
49 # - [x] Pipe table (php md pandoc)
50 # - [x] Line blocks (pandoc)
51 # - [x] Task lists (pandoc, custom)
52 # - [ ] Definition lists (php md, pandoc)
53 # - [-] Numbered example lists (pandoc)
54 # - [-] Metadata blocks (pandoc)
55 # - [x] Metadata blocks (custom)
56 # - [x] Fenced Divs (pandoc)
58 # Extensions - Inline elements:
59 # ----------------------------
60 # - [x] Ignore embedded_underscores (php md, pandoc)
61 # - [x] ~~strikeout~~ (pandoc)
62 # - [x] ^Superscript^ ~Subscript~ (pandoc)
63 # - [-] Bracketed spans (pandoc)
64 # - [-] Inline attributes (pandoc)
65 # - [x] Image attributes (custom, pandoc inspired, inline only)
66 # - [x] Wiki style links [[PageName]] / [[PageName|Link Text]]
67 # - [-] TEX-Math (pandoc)
68 # - ? Footnotes (php md)
69 # - ? Abbreviations (php md)
70 # - ? "Curly quotes" (smartypants)
71 # - [ ] em-dashes (--) (smartypants old)
72 # - ? ... three-dot ellipsis (smartypants)
73 # - [-] en-dash (smartypants)
74 # - [ ] Automatic em-dash / en-dash
75 # - [ ] Automatic -> Arrows <-
77 function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; }
79 function HTML ( text ) {
80 gsub( /&/, "\\&", text );
81 gsub( /</, "\\<", text );
82 gsub( />/, "\\>", text );
83 gsub( /"/, "\\"", text );
84 gsub( /'/, "\\'", text );
85 gsub( /\\/, "\\\", text );
89 function URL ( text ) {
90 gsub( /&/, "%26", text );
91 gsub( /"/, "%22", text );
92 gsub( /'/, "%27", text );
93 gsub( /\?/, "%3F", text );
94 gsub( /#/, "%23", text );
95 gsub( /\[/, "%5B", text );
96 gsub( /\]/, "%5D", text );
97 gsub( / /, "%20", text );
98 gsub( / /, "%09", text );
99 gsub( /\\/, "%5C", text );
103 function inline( line, LOCAL, len, code, href, guard ) {
104 nu = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\_]|_[[:alnum:]])*" # not underline (except when escaped)
105 na = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\\\*])*" # not asterisk (except when escaped)
106 ieu = "_([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])_" # inner <em> (underline)
107 isu = "__([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])__" # inner <strong> (underline)
108 iea = "\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*" # inner <em> (asterisk)
109 isa = "\\*\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*\\*" # inner <strong> (asterisk)
111 if ( line ~ /^$/ ) { # Recursion End
114 # omit processing of escaped characters
115 } else if ( line ~ /^\\[]\\`\*_\{\}\(\)#\+-\.![]/) {
116 return substr(line, 2, 1) inline( substr(line, 3) );
119 } else if ( match(line, /^ \n/) ) {
120 return "<br>\n" inline( substr(line, RLENGTH + 1) );
123 } else if ( match( line, /^`+/) ) {
125 guard = substr( line, 1, len )
126 if ( match(line, guard ".*" guard) ) {
127 code = substr( line, len + 1, match( substr(line, len + 1), guard ) - 1)
128 len = 2 * length(guard) + length(code)
129 # strip single surrounding white spaces
130 code = gensub( / (.*) /, "\\1", "1" , code)
131 # escape HTML within code span
132 gsub( /&/, "\\&", code ); gsub( /</, "\\<", code ); gsub( />/, "\\>", code );
133 return "<code>" code "</code>" inline( substr( line, len + 1 ) )
137 } else if ( match( line, /^\[\[([^\]\|]+)(\|([^\]]+))?\]\]/) ) {
139 href = gensub(/^\[\[([^\]\|]+)(\|([^\]]+))?\]\]/, "\\1", 1, substr(line, 1, len) );
140 text = gensub(/^\[\[([^\]\|]+)(\|([^\]]+))?\]\]/, "\\3", 1, substr(line, 1, len) );
141 if ( ! text ) text = href;
142 return "<a href=\"" URL(href) "\">" HTML(text) "</a>" inline( substr( line, len + 1) );
144 # quick links ("automatic links" in md doc)
145 } else if ( match( line, /^<[a-zA-Z]+:\/\/([-\.[:alnum:]]+)(:[0-9]*)?(\/[^>]*)?>/ ) ) {
147 href = URL( substr( line, 2, len - 2) );
148 return "<a href=\"" href "\">" href "</a>" inline( substr( line, len + 1) );
151 } 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])?)*>/ ) ) {
153 href = URL( substr( line, 2, len - 2) );
154 return "<a href=\"mailto:" href "\">" href "</a>" inline( substr( line, len + 1) );
157 } else if ( match(line, /^\[([^]]+)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/) ) {
159 text = gensub(/^\[([^]]+)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, "\\1", 1, substr(line, 1, len) );
160 href = gensub(/^\[([^]]+)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, "\\2", 1, substr(line, 1, len) );
161 title = gensub(/^\[([^]]+)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, "\\4", 1, substr(line, 1, len) );
163 return "<a href=\"" URL(href) "\" title=\"" HTML(title) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
165 return "<a href=\"" URL(href) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
168 # reference style links
169 } else if ( match(line, /^\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
171 text = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, substr(line, 1, len) );
172 id = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, substr(line, 1, len) );
173 if ( ! id ) id = text;
174 if ( rl_href[id] && rl_title[id] ) {
175 return "<a href=\"" URL(rl_href[id]) "\" title=\"" HTML(rl_title[id]) "\">" inline(text) "</a>" inline( substr( line, len + 1) );
176 } else if ( rl_href[id] ) {
177 return "<a href=\"" URL(rl_href[id]) "\">" inline(text) "</a>" inline( substr( line, len + 1) );
179 return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
183 } else if ( match(line, /^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/) ) {
185 text = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\1", "g", substr(line, 1, len) );
186 href = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\2", "g", substr(line, 1, len) );
187 title = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\4", "g", substr(line, 1, len) );
188 attrib = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\6", "g", substr(line, 1, len) );
189 if ( title && attrib ) {
190 return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\" class=\"" HTML(attrib) "\">" \
191 inline( substr( line, len + 1) );
192 } else if ( title ) {
193 return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\">" \
194 inline( substr( line, len + 1) );
195 } else if ( attrib ) {
196 return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
197 inline( substr( line, len + 1) );
199 return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\">" \
200 inline( substr( line, len + 1) );
203 # reference style images
204 } else if ( match(line, /^!\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
206 text = gensub(/^!\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, substr(line, 1, len) );
207 id = gensub(/^!\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, substr(line, 1, len) );
208 if ( ! id ) id = text;
209 if ( rl_href[id] && rl_title[id] ) {
210 return "<img src=\"" URL(rl_href[id]) "\" alt=\"" HTML(text) "\" title=\"" HTML(rl_title[id]) "\">" \
211 inline( substr( line, len + 1) );
212 } else if ( rl_href[id] ) {
213 return "<img src=\"" URL(rl_href[id]) "\" alt=\"" HTML(text) "\">" \
214 inline( substr( line, len + 1) );
216 return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
219 # ~~strikeout~~ (pandoc)
220 } else if ( match(line, /^~~([[:graph:]]|[[:graph:]]([^~]|~[^~])*[[:graph:]])~~/) ) {
222 return "<del>" inline( substr( line, 3, len - 4 ) ) "</del>" inline( substr( line, len + 1 ) );
224 # ^superscript^ (pandoc)
225 } else if ( match(line, /^\^([^[:space:]^]|\\[ ^])+\^/) ) {
227 return "<sup>" inline( substr( line, 2, len - 2 ) ) "</sup>" inline( substr( line, len + 1 ) );
229 # ~subscript~ (pandoc)
230 } else if ( match(line, /^~([^[:space:]~]|\\[ ~])+~/) ) {
232 return "<sub>" inline( substr( line, 2, len - 2 ) ) "</sub>" inline( substr( line, len + 1 ) );
234 # ignore embedded underscores (pandoc, php md)
235 } else if ( match(line, "^[[:alnum:]](__|_)") ) {
236 return HTML(substr( line, 1, RLENGTH)) inline( substr(line, RLENGTH + 1) );
239 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__$") ) {
241 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
244 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__[[:space:][:punct:]]") ) {
246 return "<strong>" inline( substr( line, 3, len - 5 ) ) "</strong>" inline( substr( line, len) );
249 } else if ( match(line, "^\\*\\*(([^\\*[:space:]]|" iea ")|([^\\*[:space:]]|" iea ")(" na "|" iea ")*([^\\*[:space:]]|" iea "))\\*\\*") ) {
251 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
254 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_$") ) {
256 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
259 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_[[:space:][:punct:]]") ) {
261 return "<em>" inline( substr( line, 2, len - 3 ) ) "</em>" inline( substr( line, len ) );
264 } else if ( match(line, "^\\*(([^\\*[:space:]]|" isa ")|([^\\*[:space:]]|" isa ")(" na "|" isa ")*([^\\*[:space:]]|" isa "))\\*") ) {
266 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
269 } else if ( AllowMacros && match( line, /^<<([^>]|>[^>])+>>/) ) {
271 return macro( substr( line, 3, len - 4 ) ) inline(substr(line, len + 1));
273 # Verbatim inline HTML
274 } 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:]]*\/?>)/) ) {
276 return substr( line, 1, len) inline(substr(line, len + 1));
278 # Literal HTML entities
279 } else if ( match( line, /^&([a-zA-Z]{2,32}|#[0-9]{1,7}|#[xX][0-9a-fA-F]{1,6});/) ) {
281 return substr( line, 1, len ) inline(substr(line, len + 1));
283 # Escape lone HTML character
284 } else if ( match( line, /^[&<>"']/) ) {
285 return HTML(substr(line, 1, 1)) inline(substr(line, 2));
287 # continue walk over string
289 return substr(line, 1, 1) inline( substr(line, 2) );
293 function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent, attrib ) {
294 gsub( /^\n+|\n+$/, "", block );
300 } else if ( AllowHTML && match( block, /(^|\n) ? ? ?(<!--([^-]|-[^-]|--[^>])*(-->|$)|<\?([^\?]|\?[^>])*(\?>|$)|<![A-Z][^>]*(>|$)|<!\[CDATA\[([^\]]|\][^\]]|\]\][^>])*(\]\]>|$))/) ) {
301 len = RLENGTH; st = RSTART;
302 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
305 } 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|$)/) ) {
306 len = RLENGTH; st = RSTART;
307 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
310 } else if ( AllowHTML && match( tolower(block), /(^|\n) ? ? ?<(script|pre|style)([[:space:]\n>]).*(<\/script>|<\/pre>|<\/style>|$)/) ) {
311 len = RLENGTH; st = RSTART;
312 match( tolower(substr(block, st, len)), /(<\/script>|<\/pre>|<\/style>)/);
313 len = RSTART + RLENGTH;
314 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
317 } 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|$)/) ) {
318 len = RLENGTH; st = RSTART;
319 return substr(block, st, len) _block(substr(block, st + len));
321 # Metadata (custom, block starting with %something)
322 # Metadata is ignored but can be interpreted externally
323 } else if ( match(block, /^%[a-zA-Z]+([[:space:]][^\n]*)?(\n|$)(%[a-zA-Z]+([[:space:]][^\n]*)?(\n|$)|%([[:space:]][^\n]*)?(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
324 len = RLENGTH; st = RSTART;
325 return _block( substr( block, len + 1) );
327 # Blockquote (leading >)
328 } else if ( match( block, /^> /) ) {
329 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/);
330 len = RLENGTH; st = RSTART;
331 return "<blockquote>\n" _block( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) "</blockquote>\n\n" \
332 _block( substr(block, st + len) );
334 # Pipe Tables (pandoc / php md / gfm )
335 } else if ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?)\n" \
336 "((\\|)?:?(-+:?[\\|+])+:?-+:?(\\|)?)\n" \
337 "((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ) {
338 len = RLENGTH; st = RSTART;
339 #initialize empty arrays
340 split("", talign); split("", tarray);
341 cols = 0; cnt=0; ttext = "";
343 # table header and alignment
344 split( gensub( /(^\||\|$)/, "", "g", \
345 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
346 substr(block, 1, match(block, /(\n|$)/)) \
348 block = substr(block, match(block, /(\n|$)/) + 1 );
350 gensub( /(^\||\|$)/, "", "g", \
351 substr(block, 1, match(block, /(\n|$)/)) \
353 block = substr(block, match(block, /(\n|$)/) + 1 );
355 for( cnt = 1; cnt < cols; cnt++ ) {
356 if (match(talign[cnt], /:-+:/)) talign[cnt]="center";
357 else if (match(talign[cnt], /-+:/)) talign[cnt]="right";
358 else if (match(talign[cnt], /:-+/)) talign[cnt]="left";
362 ttext = "<thead>\n<tr>"
363 for (cnt = 1; cnt < cols; cnt++)
364 ttext = ttext "<th align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</th>"
365 ttext = ttext "</tr>\n</thead><tbody>\n"
367 while ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ){
368 split( gensub( /(^\||\|$)/, "", "g", \
369 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
370 substr(block, 1, match(block, /(\n|$)/)) \
372 block = substr(block, match(block, /(\n|$)/) + 1 );
375 for (cnt = 1; cnt < cols; cnt++)
376 ttext = ttext "<td align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</td>"
377 ttext = ttext "</tr>\n"
379 return "<table>" ttext "</tbody></table>\n" _block(block);
381 # Grid Tables (pandoc)
382 } else if ( match(block, "^\\+(-+\\+)+\n" \
383 "(\\|([^\n]+\\|)+\n)+" \
384 "\\+(:?=+:?\\+)+\n" \
385 "((\\|([^\n]+\\|)+\n)+" \
386 "\\+(-+\\+)+(\n|$))+" \
388 len = RLENGTH; st = RSTART;
389 #initialize empty arrays
390 split("", talign); split("", tarray); split("", tread);
391 cols = 0; cnt=0; ttext = "";
393 # table header and alignment
394 block = substr(block, match(block, /(\n|$)/) + 1 );
395 while ( match(block, "^\\|([^\n]+\\|)+\n") ) {
396 cols = split( gensub( /(^\||\|$)/, "", "g", \
397 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
398 substr(block, 1, match(block, /(\n|$)/)) \
400 block = substr(block, match(block, /(\n|$)/) + 1 );
401 for (cnt = 1; cnt < cols; cnt++)
402 tarray[cnt] = tarray[cnt] "\n" tread[cnt];
406 gensub( /(^\+|\+$)/, "", "g", \
407 substr(block, 1, match(block, /(\n|$)/)) \
409 block = substr(block, match(block, /(\n|$)/) + 1 );
411 for (cnt = 1; cnt < cols; cnt++) {
412 if (match(talign[cnt], /:=+:/)) talign[cnt]="center";
413 else if (match(talign[cnt], /=+:/)) talign[cnt]="right";
414 else if (match(talign[cnt], /:=+/ )) talign[cnt]="left";
418 ttext = "<thead>\n<tr>"
419 for (cnt = 1; cnt < cols; cnt++)
420 ttext = ttext "<th align=\"" talign[cnt] "\">" _block(tarray[cnt]) "</th>"
421 ttext = ttext "</tr>\n</thead><tbody>\n"
423 while ( match(block, /^((\|([^\n]+\|)+\n)+\+(-+\+)+(\n|$))+/ ) ){
425 while ( match(block, /^\|([^\n]+\|)+\n/) ) {
426 split( gensub( /(^\||\|$)/, "", "g", \
427 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
428 substr(block, 1, match(block, /(\n|$)/)) \
430 block = substr(block, match(block, /(\n|$)/) + 1 );
431 for (cnt = 1; cnt < cols; cnt++)
432 tarray[cnt] = tarray[cnt] "\n" tread[cnt];
434 block = substr(block, match(block, /(\n|$)/) + 1 );
437 for (cnt = 1; cnt < cols; cnt++)
438 ttext = ttext "<td align=\"" talign[cnt] "\">" _block(tarray[cnt]) "</td>"
439 ttext = ttext "</tr>\n"
441 return "<table>" ttext "</tbody></table>\n" _block(block);
443 # Line Blocks (pandoc)
444 } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
445 len = RLENGTH; st = RSTART;
446 code = substr(block, 1, len);
447 gsub(/\n[[:space:]]+/, " ", code);
448 gsub(/\n\| /, "\n", code);
449 gsub(/^\| |\n$/, "", code);
450 return "<div class=\"line-block\">" gensub(/\n/, "<br>\n", "g", inline( code )) "</div>\n" \
451 _block( substr( block, len + 1) );
453 # Indented Code Block
454 } else if ( match(block, /^( |\t)( *\t*[^ \t\n]+ *\t*)+(\n|$)(( |\t)[^\n]+(\n|$)|[ \t]*(\n|$))*/) ) {
455 len = RLENGTH; st = RSTART;
456 code = substr(block, 1, len);
457 gsub(/(^|\n)( |\t)/, "\n", code);
458 gsub(/^\n|\n+$/, "", code);
459 return "<pre><code>" HTML( code ) "</code></pre>\n" \
460 _block( substr( block, len + 1 ) );
462 # Fenced Divs (pandoc, custom)
463 } else if ( match( block, /^(:::+)/ ) ) {
464 guard = substr( block, 1, RLENGTH );
465 code = gensub(/^[^\n]+\n/, "", 1, block);
466 attrib = gensub(/^:::+[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\1", 1, block);
467 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib);
468 gsub(/(^ | $)/, "", attrib);
469 if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
470 len = RLENGTH; st = RSTART;
471 return "<div class=\"" attrib "\">" _block( substr(code, 1, st - 1) ) "</div>\n" \
472 _block( substr( code, st + len ) );
474 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
475 len = RLENGTH; st = RSTART;
476 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
477 _block( substr(block, st + len) );
480 # Fenced Code Block (pandoc)
481 } else if ( match( block, /^(~~~+|```+)/ ) ) {
482 guard = substr( block, 1, RLENGTH );
483 code = gensub(/^[^\n]+\n/, "", 1, block);
484 attrib = gensub(/^(~~~+|```+)[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\2", 1, block);
485 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib);
486 gsub(/(^ | $)/, "", attrib);
487 if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
488 len = RLENGTH; st = RSTART;
489 return "<pre><code class=\"" attrib "\">" HTML( substr(code, 1, st - 1) ) "</code></pre>\n" \
490 _block( substr( code, st + len ) );
492 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
493 len = RLENGTH; st = RSTART;
494 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
495 _block( substr(block, st + len) );
499 } else if ( match( block, "^ ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
500 "(([ \t]*\n)* ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
501 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
502 "|[^\n]+(\n|$))*" ) ) {
503 list = substr( block, 1, RLENGTH);
504 block = substr( block, RLENGTH + 1);
505 indent = length( gensub(/[-+*][ \t]+[^\n]+.*$/, "", 1, list) );
507 gsub("(^|\n) {0," indent "}", "\n", list);
508 return "\n<ul>\n" _list( substr(list, 2) ) "</ul>\n" _block( block );
511 } else if ( match( block, "^ ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
512 "(([ \t]*\n)* ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
513 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
514 "|[^\n]+(\n|$))*" ) ) {
515 list = substr( block, 1, RLENGTH);
516 block = substr( block, RLENGTH + 1);
517 indent = length( gensub(/([0-9]+|#)\.[ \t]+[^\n]+.*$/, "", 1, list) );
519 gsub("(^|\n) {0," indent "}", "\n", list);
520 return "\n<ol>\n" _list( substr(list, 2) ) "</ol>\n" _block( block );
522 # First Order Heading
523 } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
525 HL[1]++; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
526 return "<h1 id=\"" HL[1] ":" URL(gensub( /\n.*$/, "", "g", block )) "\">" \
527 inline( gensub( /\n.*$/, "", "g", block ) ) \
528 "<a class=\"anchor\" href=\"#" HL[1] ":" \
529 URL(gensub( /\n.*$/, "", "g", block )) "\"></a></h1>\n\n" \
530 _block( substr( block, len + 1 ) );
532 # Second Order Heading
533 } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
535 HL[2]++; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
536 return "<h2 id=\"" HL[1] "." HL[2] ":" URL(gensub( /\n.*$/, "", "g", block )) "\">" \
537 inline( gensub( /\n.*$/, "", "g", block ) ) \
538 "<a class=\"anchor\" href=\"#" HL[1] "." HL[2] ":" \
539 URL(gensub( /\n.*$/, "", "g", block )) "\"></a></h2>\n\n" \
540 _block( substr( block, len + 1) );
543 } else if ( match( block, /^#{1,6}[ \t]*[^\n]+([ \t]*#*)(\n|$)/ ) ) {
545 hlvl = length( gensub( /^(#{1,6}).*$/, "\\1", "g", block ) );
546 htxt = gensub(/^#{1,6}[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[^\n#])+)([ \t]*#*)(\n.*)?$/, "\\1", 1, block);
547 HL[hlvl]++; for ( n = hlvl + 1; n < 7; n++) { HL[n] = 0;}
548 hid = HL[1]; for ( n = 2; n <= hlvl; n++) { hid = hid "." HL[n] ; }
549 return "<h" hlvl " id=\"" hid ":" URL(htxt) "\">" inline( htxt ) \
550 "<a class=\"anchor\" href=\"#" hid "\"></a></h" hlvl ">\n\n" \
551 _block( substr( block, len + 1) );
554 } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) {
555 len = RLENGTH; st = RSTART;
556 return _block( substr(block, 1, st - 1) ) "\n" \
557 _block( substr(block, st + len) );
560 } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
561 len = RLENGTH; st = RSTART;
562 return _block(substr(block, 1, st - 1)) "<hr>\n" _block(substr(block, st + len));
566 return "<p>" inline(block) "</p>\n";
570 function _list( block, last, LOCAL, p) {
571 if ( ! length(block) ) return "";
572 gsub(/^([-+*]|[0-9]+\.|#\.)( ? ? ?|\t)/, "", block)
574 # slice next list item from input
575 if ( match( block, /\n([-+*]|[0-9]+\.|#\.)[ \t]+[^\n]+/) ) {
576 p = substr( block, 1, RSTART);
577 block = substr( block, RSTART + 1);
579 p = block; block = "";
581 sub( /\n +([-+*]|[0-9]+\.|#\.)/, "\n&", p );
583 # if this should be a paragraph item
584 # either previous item (last) or current item (p) contains blank lines
585 if (match(last, /\n[[:space:]]*\n/) || match(p, /\n[[:space:]]*\n/) ) {
586 last = p; p = _block(p);
588 last = p; p = _block(p);
589 sub( /^<p>/, "", p );
590 sub( /<\/p>\n/, "", p );
594 # Task List (pandoc, custom)
595 if ( p ~ /^\[ \].*/ ) { return "<li class=\"task pending\"><input type=checkbox disabled>" \
596 substr(p, 4) "</li>\n" _list( block, last );
597 } else if ( p ~ /^\[-\].*/ ) { return "<li class=\"task negative\"><input type=checkbox disabled>" \
598 substr(p, 4) "</li>\n" _list( block, last );
599 } else if ( p ~ /^\[\?\].*/ ) { return "<li class=\"task unsure\"><input type=checkbox disabled>" \
600 substr(p, 4) "</li>\n" _list( block, last );
601 } else if ( p ~ /^\[\/\].*/ ) { return "<li class=\"task partial\"><input type=checkbox disabled>" \
602 substr(p, 4) "</li>\n" _list( block, last );
603 } else if ( p ~ /^\[[xX]\].*/ ) { return "<li class=\"task done\"><input type=checkbox disabled checked>" \
604 substr(p, 4) "</li>\n" _list( block, last );
605 } else if ( p ~ /^<p>\[ \].*/ ) { return "<li class=\"task pending\"><p><input type=checkbox disabled>" \
606 substr(p, 7) "</li>\n" _list( block, last );
607 } else if ( p ~ /^<p>\[-\].*/ ) { return "<li class=\"task negative\"><p><input type=checkbox disabled>" \
608 substr(p, 7) "</li>\n" _list( block, last );
609 } else if ( p ~ /^<p>\[\?\].*/ ) { return "<li class=\"task unsure\"><p><input type=checkbox disabled>" \
610 substr(p, 7) "</li>\n" _list( block, last );
611 } else if ( p ~ /^<p>\[\/\].*/ ) { return "<li class=\"task partial\"><p><input type=checkbox disabled>" \
612 substr(p, 7) "</li>\n" _list( block, last );
613 } else if ( p ~ /^<p>\[[xX]\].*/ ) { return "<li class=\"task done\"><p><input type=checkbox disabled checked>" \
614 substr(p, 7) "</li>\n" _list( block, last );
615 } else { return "<li>" p "</li>\n" _list( block, last ); }
620 file = ""; rl_href[""] = ""; rl_title[""] = "";
621 if (ENVIRON["MD_HTML"] == "true") { AllowHTML = "true"; }
622 HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
624 # Buffering of full file ist necessary, e.g. to find reference links
625 while (getline) { file = file $0 "\n"; }
626 # Clean up MS-DOS line breaks
627 gsub(/\r\n/, "\n", file);
629 # Fill array of reference links
631 re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
632 # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
633 while ( match(f, re_reflink ) ) {
634 rl_id = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
635 rl_href[rl_id] = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
636 rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
637 f = substr(f, RSTART + RLENGTH);
638 rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
639 if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
641 # Clear reflinks from File
642 while( gsub(re_reflink, "\n", file ) );
643 # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
645 # Run Block Processing -> The Actual Markdown!
646 printf "%s", _block( file );