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 # - [x] Automatic <section>-wrapping (custom)
42 # - ? Heading identifiers (php md, pandoc)
43 # - [x] Heading attributes (custom)
44 # - [ ] <hr> ends section
45 # - [x] Automatic heading identifiers (custom)
46 # - [x] Fenced code blocks (php md, pandoc)
47 # - [x] Fenced code attributes
48 # - [x] Images (as block elements, <figure>-wrapped) (custom)
49 # - [x] reference style block images
51 # - ? Simple table (pandoc)
52 # - ? Multiline table (pandoc)
53 # - [x] Grid table (pandoc)
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)
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)
83 function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; }
85 function HTML ( text ) {
86 gsub( /&/, "\\&", text );
87 gsub( /</, "\\<", text );
88 gsub( />/, "\\>", text );
89 gsub( /"/, "\\"", text );
90 gsub( /'/, "\\'", text );
91 gsub( /\\/, "\\\", text );
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 );
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)
118 if ( line ~ /^$/ ) { # Recursion End
121 # omit processing of escaped characters
122 } else if ( line ~ /^\\./) {
123 return HTML(substr(line, 2, 1)) inline( substr(line, 3) );
126 } else if ( match(line, /^ \n/) ) {
127 return "<br>\n" inline( substr(line, RLENGTH + 1) );
130 } else if ( match( line, /^`+/) ) {
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( /&/, "\\&", code ); gsub( /</, "\\<", code ); gsub( />/, "\\>", code );
140 return "<code>" code "</code>" inline( substr( line, len + 1 ) )
144 } else if ( match( line, /^\[\[([^]|]+)(\|[^]]+)?\]\]/) ) {
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) );
151 # quick links ("automatic links" in md doc)
152 } else if ( match( line, /^<[a-zA-Z]+:\/\/([-\.[:alnum:]]+)(:[0-9]*)?(\/[^>]*)?>/ ) ) {
154 href = URL( substr( line, 2, len - 2) );
155 return "<a href=\"" href "\">" href "</a>" inline( substr( line, len + 1) );
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])?)*>/ ) ) {
160 href = URL( substr( line, 2, len - 2) );
161 return "<a href=\"mailto:" href "\">" href "</a>" inline( substr( line, len + 1) );
164 # ,_______________________Image____________________________,
165 } else if ( match(line, /^\[([^]]+|!\[[^]]*\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/) ) {
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) );
174 return "<a href=\"" URL(href) "\" title=\"" HTML(title) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
176 return "<a href=\"" URL(href) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
179 # reference style links
180 } else if ( match(line, /^\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
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) );
190 return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
194 } else if ( match(line, /^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/) ) {
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) );
210 return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
211 inline( substr( line, len + 1) );
214 # reference style images
215 } else if ( match(line, /^!\[([^]]*)\] ?\[([^]]*)\]/ ) ) {
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) );
227 return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
230 # ~~strikeout~~ (pandoc)
231 } else if ( match(line, /^~~([[:graph:]]|[[:graph:]]([^~]|~[^~])*[[:graph:]])~~/) ) {
233 return "<del>" inline( substr( line, 3, len - 4 ) ) "</del>" inline( substr( line, len + 1 ) );
235 # ^superscript^ (pandoc)
236 } else if ( match(line, /^\^([^[:space:]^]|\\[ ^])+\^/) ) {
238 return "<sup>" inline( substr( line, 2, len - 2 ) ) "</sup>" inline( substr( line, len + 1 ) );
240 # ~subscript~ (pandoc)
241 } else if ( match(line, /^~([^[:space:]~]|\\[ ~])+~/) ) {
243 return "<sub>" inline( substr( line, 2, len - 2 ) ) "</sub>" inline( substr( line, len + 1 ) );
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) );
250 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__$") ) {
252 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
255 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__[[:space:][:punct:]]") ) {
257 return "<strong>" inline( substr( line, 3, len - 5 ) ) "</strong>" inline( substr( line, len) );
260 } else if ( match(line, "^\\*\\*(([^\\*[:space:]]|" iea ")|([^\\*[:space:]]|" iea ")(" na "|" iea ")*([^\\*[:space:]]|" iea "))\\*\\*") ) {
262 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
265 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_$") ) {
267 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
270 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_[[:space:][:punct:]]") ) {
272 return "<em>" inline( substr( line, 2, len - 3 ) ) "</em>" inline( substr( line, len ) );
275 } else if ( match(line, "^\\*(([^\\*[:space:]]|" isa ")|([^\\*[:space:]]|" isa ")(" na "|" isa ")*([^\\*[:space:]]|" isa "))\\*") ) {
277 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
280 } else if ( AllowMacros && match( line, /^<<([^>]|>[^>])+>>/) ) {
282 return macro( substr( line, 3, len - 4 ) ) inline(substr(line, len + 1));
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:]]*\/?>)/) ) {
287 return substr( line, 1, len) inline(substr(line, len + 1));
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});/) ) {
292 return substr( line, 1, len ) inline(substr(line, len + 1));
295 } else if ( line ~ /^-->( |$)/) { # ignore multidash-arrow
296 return "-->" inline( substr(line, 4) );
297 } else if ( line ~ /^<-( |$)/) {
298 return "←" inline( substr(line, 3) );
299 } else if ( line ~ /^->( |$)/) {
300 return "→" inline( substr(line, 3) );
302 # Escape lone HTML character
303 } else if ( match( line, /^[&<>"']/) ) {
304 return HTML(substr(line, 1, 1)) inline(substr(line, 2));
306 # continue walk over string
308 return substr(line, 1, 1) inline( substr(line, 2) );
312 function headline( hlvl, htxt, attrib, LOCAL, sec, n, HL) {
313 match(hstack, /([0-9]+( [0-9]+){5})$/); split( substr(hstack, RSTART), HL);
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;}
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);
322 sub(/([0-9]+( [0-9]+){5})$/, "", hstack);
323 hstack = hstack HL[1] " " HL[2] " " HL[3] " " HL[4] " " HL[5] " " HL[6];
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>" \
331 # Nested Block, resets heading counters
332 function _nblock( block, LOCAL, sec, n ) {
333 hstack = hstack " 0 0 0 0 0 0";
337 for ( n = blvl + 1; n in BL; n++) { delete BL[n]; }
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>":""); }
343 sub("( +[0-9]+){6} *$", "", hstack); blvl--;
347 function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code, indent, list ) {
348 gsub( "(^\n+|\n+$)", "", block );
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));
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));
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));
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));
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) );
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) );
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 = "";
398 # table header and alignment
399 split( gensub( /(^\||\|$)/, "", "g", \
400 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
401 substr(block, 1, match(block, /(\n|$)/)) \
403 block = substr(block, match(block, /(\n|$)/) + 1 );
405 gensub( /(^\||\|$)/, "", "g", \
406 substr(block, 1, match(block, /(\n|$)/)) \
408 block = substr(block, match(block, /(\n|$)/) + 1 );
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";
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"
422 while ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ){
423 split( gensub( /(^\||\|$)/, "", "g", \
424 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
425 substr(block, 1, match(block, /(\n|$)/)) \
427 block = substr(block, match(block, /(\n|$)/) + 1 );
430 for (cnt = 1; cnt < cols; cnt++)
431 ttext = ttext "<td align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</td>"
432 ttext = ttext "</tr>\n"
434 return "<table>" ttext "</tbody></table>\n" _block(block);
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|$))+" \
444 match( block, "^()()()" \
445 "(\\+(:?-+:?\\+)+)\n" \
446 "((\\|([^\n]+\\|)+\n)+" \
447 "\\+(-+\\+)+(\n|$))+" \
449 len = RLENGTH; st = RSTART;
450 #initialize empty arrays
451 split("", talign); split("", tarray); split("", tread);
452 cols = 0; cnt=0; ttext = "";
455 cols = split( gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block), tread, /\+/) - 2;
456 # debug(" Cols: " gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block ));
459 split( gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block ), talign, /\+/ );
460 # debug("Align: " gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block ));
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";
469 if ( match(block, "^\\+(-+\\+)+\n" \
470 "(\\|([^\n]+\\|)+\n)+" \
471 "\\+(:?=+:?\\+)+\n" \
472 "((\\|([^\n]+\\|)+\n)+" \
473 "\\+(-+\\+)+(\n|$))+" \
476 block = substr(block, match(block, /(\n|$)/) + 1 );
477 while ( match(block, "^\\|([^\n]+\\|)+\n") ) {
478 split( gensub( /(^\||\|$)/, "", "g", \
479 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
480 substr(block, 1, match(block, /(\n|$)/)) \
482 block = substr(block, match(block, /(\n|$)/) + 1 );
483 for (cnt = 1; cnt <= cols; cnt++)
484 tarray[cnt] = tarray[cnt] "\n" tread[cnt];
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>"
494 block = substr(block, match(block, /(\n|$)/) + 1 );
495 ttext = ttext "<tbody>\n"
497 while ( match(block, /^((\|([^\n]+\|)+\n)+\+(-+\+)+(\n|$))+/ ) ){
499 while ( match(block, /^\|([^\n]+\|)+\n/) ) {
500 split( gensub( /(^\||\|$)/, "", "g", \
501 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
502 substr(block, 1, match(block, /(\n|$)/)) \
504 block = substr(block, match(block, /(\n|$)/) + 1 );
505 for (cnt = 1; cnt <= cols; cnt++)
506 tarray[cnt] = tarray[cnt] "\n" tread[cnt];
508 block = substr(block, match(block, /(\n|$)/) + 1 );
511 for (cnt = 1; cnt <= cols; cnt++)
512 ttext = ttext "<td align=\"" talign[cnt] "\">" _nblock(tarray[cnt]) "</td>"
513 ttext = ttext "</tr>\n"
515 return "<table>" ttext "</tbody></table>\n" _nblock(block);
517 # Line Blocks (pandoc)
518 } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
519 len = RLENGTH; st = RSTART;
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);
525 return "<div class=\"line-block\">" text "</div>\n" _block( substr( block, len + 1) );
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 ) );
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 ) );
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) );
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 ) );
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) );
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);
579 return headline(1, text, attrib) _block( substr( block, len + 1 ) );
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);
586 return headline(1, text, 0) _block( substr( block, len + 1 ) );
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);
595 return headline(2, text, attrib) _block( substr( block, len + 1) );
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);
602 return headline(2, text, 0) _block( substr( block, len + 1) );
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;
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);
614 return headline( n, text, attrib ) _block( substr( block, len + 1) );
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);
622 return headline( n, text, 0 ) _block( substr( block, len + 1) );
624 # block images (wrapped in <figure>)
625 } else if ( match(block, /^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n|$)/) ) {
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>" \
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>" \
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) "\">" \
647 _block( substr( block, len + 1) );
649 return "<figure data-src=\"" URL(href, 1) "\">" \
650 "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
652 _block( substr( block, len + 1) );
655 # reference style images (block)
656 } else if ( match(line, /^!\[([^]]*)\] ?\[([^]]*)\](\n|$)/ ) ) {
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>" \
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) "\">" \
671 _block( substr( block, len + 1) );
673 return "<p>" HTML(substr(block, 1, len)) "</p>\n" _block( substr(block, len + 1) );
676 # Macros (standalone <<macro>> calls handled as block, so they are not wrapped in paragraph)
677 } else if ( AllowMacros && match( block, /^<<(([^>]|>[^>])+)>>(\n|$)/) ) {
679 text = gensub(/^<<(([^>]|>[^>])+)>>(\n.*)?$/, "\\1", 1, block);
680 return macro(text) _block(substr(block, len + 1) );
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|$))*)+" \
690 list = substr( block, 1, RLENGTH); block = substr( block, RLENGTH + 1);
691 return "\n<dl>\n" _dlist( list ) "</dl>\n" _block( block );
694 } else if ( match( block, "(^|\n) ? ? ?[-+*][ \t][^\n]+(\n|$)" \
695 "(([ \t]*\n)* ? ? ?[-+*][ \t][^\n]+(\n|$)" \
696 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
697 "|[^\n \t][^\n]+(\n|$))*" ) ) {
698 st = RSTART; len = RLENGTH; list = substr( block, RSTART, RLENGTH);
699 sub("^\n", "", list); match(list, "^ ? ? ?[-+*]"); indent = RLENGTH;
700 gsub( "(^|\n) {0," indent - 1 "}", "\n", list); sub("^\n", "", list);
702 text = substr(block, 1, st - 1); block = substr(block, st + len);
703 if (match( list, "\n([0-9]+\\.|#\\.)[ \t]" )) {
704 block = substr(list, RSTART + 1) block;
705 list = substr(list, 1, RSTART);
708 return _block( text ) "<ul>\n" _list( list, "[-+*]" ) "</ul>\n" _block( block );
711 } else if ( match( block, "(^|\n) ? ? ?([0-9]+\\.|#\\.)[ \t][^\n]+(\n|$)" \
712 "(([ \t]*\n)* ? ? ?([0-9]+\\.|#\\.)[ \t][^\n]+(\n|$)" \
713 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
714 "|[^\n \t][^\n]+(\n|$))*" ) ) {
715 st = RSTART; len = RLENGTH; list = substr( block, RSTART, RLENGTH);
716 sub("^\n", "", list); match(list, "^ ? ? ?[0-9#]"); indent = RLENGTH;
717 gsub( "(^|\n) {0," indent - 1 "}", "\n", list); sub("^\n", "", list);
719 text = substr(block, 1, st - 1); block = substr(block, st + len);
720 if (match( list, "\n[-+*][ \t]" )) {
721 block = substr(list, RSTART + 1) block;
722 list = substr(list, 1, RSTART);
725 return _block( text ) "<ol>\n" _list( list, "([0-9]+\\.|#\\.)" ) "</ol>\n" _block( block );
728 } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) {
729 len = RLENGTH; st = RSTART;
730 return _block( substr(block, 1, st - 1) ) "\n" \
731 _block( substr(block, st + len) );
734 } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
735 len = RLENGTH; st = RSTART;
736 return _block(substr(block, 1, st - 1)) "<hr>\n" _block(substr(block, st + len));
740 return "<p>" inline(block) "</p>\n";
744 function _list (block, mark, LOCAL, len, st, text, indent, task) {
745 if ( match(block, "^([ \t]*\n)*$")) return;
746 match(block, "^" mark "[ \t]"); indent = RLENGTH;
747 sub("^" mark "[ \t]", "", block);
749 match( block, "\n" mark "[ \t][^\n]+(\n|$)" \
750 "(([ \t]*\n)* ? ? ?" mark "[ \t][^\n]+(\n|$)" \
751 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
752 "|[^\n \t][^\n]+(\n|$))*");
753 (RLENGTH == -1) ? st = length(block) + 1 : st = RSTART;
754 text = substr(block, 1, st); block = substr(block, st + 1);
756 gsub("\n {0," indent "}", "\n", text);
758 task = match( text, /^\[ \]/ ) ? "<li class=\"task pending\"><input type=checkbox disabled>" : \
759 match( text, /^\[-\]/ ) ? "<li class=\"task negative\"><input type=checkbox disabled>" : \
760 match( text, /^\[\/\]/ ) ? "<li class=\"task partial\"><input type=checkbox disabled>" : \
761 match( text, /^\[\?\]/ ) ? "<li class=\"task unsure\"><input type=checkbox disabled>" : \
762 match( text, /^\[[xX]\]/) ? "<li class=\"task done\"><input type=checkbox disabled checked>" : "<li>";
763 sub(/^\[[-? /xX]\]/, "", text);
765 text = _nblock( text );
766 if (match( text, "^<p>(</p[^>]|</[^p]|<[^/]|[^<])*</p>\n$" ))
767 gsub( "(^<p>|</p>\n$)", "", text);
769 return task text "</li>\n" _list(block, mark);
772 function _dlist (block, LOCAL, len, st, text, indent, p) {
773 if (match( block, "^([ \t]*\n)*[^:\n \t][^\n]+\n" )) {
774 len = RLENGTH; text = substr(block, 1, len);
775 gsub( "(^\n*|\n*$)", "", text );
776 return "<dt>" inline( text ) "</dt>\n" _dlist( substr(block, len + 1) );
777 } else if (match( block, "^([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
778 "([^:\n \t][^\n]+(\n|$)" \
779 "|( ? ? ?\t| +)[^\n]+(\n|$)" \
780 "|([ \t]*\n)+( ? ? ?\t| +)[^\n]+(\n|$))*" \
782 len = RLENGTH; text = substr(block, 1, len);
783 sub( "^([ \t]*\n)*", "", text);
784 match(text, "^ ? ? ?:(\t| +)"); indent = RLENGTH;
785 sub( "^ ? ? ?:(\t| +)", "", text);
786 gsub( "(^|\n) {0," indent "}", "\n", text );
788 text = _nblock(text);
789 if (match( text, "^<p>(</p[^>]|</[^p]|<[^/]|[^<])*</p>\n$" ))
790 gsub( "(^<p>|</p>\n$)", "", text);
792 return "<dd>" text "</dd>\n" _dlist( substr(block, len + 1) );
798 file = ""; rl_href[""] = ""; rl_title[""] = "";
799 if (ENVIRON["MD_HTML"] == "true") { AllowHTML = "true"; }
800 HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
801 # hls = "0 0 0 0 0 0";
803 # Buffering of full file ist necessary, e.g. to find reference links
804 while (getline) { file = file $0 "\n"; }
805 # Clean up MS-DOS line breaks
806 gsub(/\r\n/, "\n", file);
808 # Fill array of reference links
810 re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
811 # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
812 while ( match(f, re_reflink ) ) {
813 rl_id = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
814 rl_href[rl_id] = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
815 rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
816 f = substr(f, RSTART + RLENGTH);
817 rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
818 if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
820 # Clear reflinks from File
821 while( gsub(re_reflink, "\n", file ) );
822 # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
824 # Run Block Processing -> The Actual Markdown!
825 printf "%s", _nblock( file );