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 # - [x] Automatic heading identifiers (custom)
45 # - [x] Fenced code blocks (php md, pandoc)
46 # - [x] Fenced code attributes
47 # - [x] Images (as block elements, <figure>-wrapped) (custom)
48 # - [x] reference style block images
50 # - ? Simple table (pandoc)
51 # - ? Multiline table (pandoc)
52 # - [x] Grid table (pandoc)
54 # - [x] Pipe table (php md, pandoc)
55 # - [x] Line blocks (pandoc)
56 # - [x] Task lists (pandoc, custom)
57 # - [x] Definition lists (php md, pandoc)
58 # - [-] Numbered example lists (pandoc)
59 # - [-] Metadata blocks (pandoc)
60 # - [x] Metadata blocks (custom)
61 # - [x] Fenced Divs (pandoc)
63 # Extensions - Inline elements:
64 # ----------------------------
65 # - [x] Ignore embedded_underscores (php md, pandoc)
66 # - [x] ~~strikeout~~ (pandoc)
67 # - [x] ^Superscript^ ~Subscript~ (pandoc)
68 # - [-] Bracketed spans (pandoc)
69 # - [-] Inline attributes (pandoc)
70 # - [x] Image attributes (custom, pandoc inspired, not for reference style)
71 # - [x] Wiki style links [[PageName]] / [[PageName|Link Text]]
72 # - [-] TEX-Math (pandoc)
73 # - ? Footnotes (php md)
74 # - ? Abbreviations (php md)
75 # - ? "Curly quotes" (smartypants)
76 # - [ ] em-dashes (--) (smartypants old)
77 # - ? ... three-dot ellipsis (smartypants)
78 # - [-] en-dash (smartypants)
79 # - [ ] Automatic em-dash / en-dash
80 # - [x] Automatic -> Arrows <- (custom)
82 function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; }
84 function HTML ( text ) {
85 gsub( /&/, "\\&", text );
86 gsub( /</, "\\<", text );
87 gsub( />/, "\\>", text );
88 gsub( /"/, "\\"", text );
89 gsub( /'/, "\\'", text );
90 gsub( /\\/, "\\\", text );
94 function URL ( text, sharp ) {
95 gsub( /&/, "%26", text );
96 gsub( /"/, "%22", text );
97 gsub( /'/, "%27", text );
98 gsub( /`/, "%60", text );
99 gsub( /\?/, "%3F", text );
100 if (sharp) gsub( /#/, "%23", text );
101 gsub( /\[/, "%5B", text );
102 gsub( /\]/, "%5D", text );
103 gsub( / /, "%20", text );
104 gsub( / /, "%09", text );
105 gsub( /\\/, "%5C", text );
109 function inline( line, LOCAL, len, code, href, guard ) {
110 nu = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\_]|_[[:alnum:]])*" # not underline (except when escaped)
111 na = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\\\*])*" # not asterisk (except when escaped)
112 ieu = "_([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])_" # inner <em> (underline)
113 isu = "__([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])__" # inner <strong> (underline)
114 iea = "\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*" # inner <em> (asterisk)
115 isa = "\\*\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*\\*" # inner <strong> (asterisk)
117 if ( line ~ /^$/ ) { # Recursion End
120 # omit processing of escaped characters
121 } else if ( line ~ /^\\./) {
122 return HTML(substr(line, 2, 1)) inline( substr(line, 3) );
125 } else if ( match(line, /^ \n/) ) {
126 return "<br>\n" inline( substr(line, RLENGTH + 1) );
129 } else if ( match( line, /^`+/) ) {
131 guard = substr( line, 1, len )
132 if ( match(line, guard ".*" guard) ) {
133 code = substr( line, len + 1, match( substr(line, len + 1), guard ) - 1)
134 len = 2 * length(guard) + length(code)
135 # strip single surrounding white spaces
136 code = gensub( /^ | $/, "", "g" , code)
137 # escape HTML within code span
138 gsub( /&/, "\\&", code ); gsub( /</, "\\<", code ); gsub( />/, "\\>", code );
139 return "<code>" code "</code>" inline( substr( line, len + 1 ) )
143 } else if ( match( line, /^\[\[([^]|]+)(\|[^]]+)?\]\]/) ) {
145 href = gensub(/^\[\[([^]|]+)(\|([^]]+))?\]\]/, "\\1", 1, substr(line, 1, len) );
146 text = gensub(/^\[\[([^]|]+)(\|([^]]+))?\]\]/, "\\3", 1, substr(line, 1, len) );
147 if ( ! text ) text = href;
148 return "<a href=\"" URL(href) "\">" HTML(text) "</a>" inline( substr( line, len + 1) );
150 # quick links ("automatic links" in md doc)
151 } else if ( match( line, /^<[a-zA-Z]+:\/\/([-\.[:alnum:]]+)(:[0-9]*)?(\/[^>]*)?>/ ) ) {
153 href = URL( substr( line, 2, len - 2) );
154 return "<a href=\"" href "\">" href "</a>" inline( substr( line, len + 1) );
157 } else if ( match( line, /^<[a-zA-Z0-9.!#$%&'\''*+\/=?^_`{|}~-]+@[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(\.[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*>/ ) ) {
159 href = URL( substr( line, 2, len - 2) );
160 return "<a href=\"mailto:" href "\">" href "</a>" inline( substr( line, len + 1) );
163 # ,_______________________Image____________________________,
164 } else if ( match(line, /^\[([^]]+|!\[[^]]*\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/) ) {
166 text = gensub(/^\[([^]]+|!\[[^]]*\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, \
167 "\\1", 1, substr(line, 1, len) );
168 href = gensub(/^\[([^]]+|!\[[^]]*\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, \
169 "\\4", 1, substr(line, 1, len) );
170 title = gensub(/^\[([^]]+|!\[[^]]*\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, \
171 "\\6", 1, substr(line, 1, len) );
173 return "<a href=\"" URL(href) "\" title=\"" HTML(title) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
175 return "<a href=\"" URL(href) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
178 # reference style links
179 } else if ( match(line, /^\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
181 text = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, substr(line, 1, len) );
182 id = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, substr(line, 1, len) );
183 if ( ! id ) id = text;
184 if ( rl_href[id] && rl_title[id] ) {
185 return "<a href=\"" URL(rl_href[id]) "\" title=\"" HTML(rl_title[id]) "\">" inline(text) "</a>" inline( substr( line, len + 1) );
186 } else if ( rl_href[id] ) {
187 return "<a href=\"" URL(rl_href[id]) "\">" inline(text) "</a>" inline( substr( line, len + 1) );
189 return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
193 } else if ( match(line, /^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/) ) {
195 text = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\1", "g", substr(line, 1, len) );
196 href = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\2", "g", substr(line, 1, len) );
197 title = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\4", "g", substr(line, 1, len) );
198 attrib = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\6", "g", substr(line, 1, len) );
199 if ( title && attrib ) {
200 return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\" class=\"" HTML(attrib) "\">" \
201 inline( substr( line, len + 1) );
202 } else if ( title ) {
203 return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\">" \
204 inline( substr( line, len + 1) );
205 } else if ( attrib ) {
206 return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
207 inline( substr( line, len + 1) );
209 return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
210 inline( substr( line, len + 1) );
213 # reference style images
214 } else if ( match(line, /^!\[([^]]*)\] ?\[([^]]*)\]/ ) ) {
216 text = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\].*/, "\\1", 1, substr(line, 1, len) );
217 id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\].*/, "\\2", 1, substr(line, 1, len) );
218 if ( ! id ) id = text;
219 if ( rl_href[id] && rl_title[id] ) {
220 return "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\" title=\"" HTML(rl_title[id]) "\">" \
221 inline( substr( line, len + 1) );
222 } else if ( rl_href[id] ) {
223 return "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
224 inline( substr( line, len + 1) );
226 return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
229 # ~~strikeout~~ (pandoc)
230 } else if ( match(line, /^~~([[:graph:]]|[[:graph:]]([^~]|~[^~])*[[:graph:]])~~/) ) {
232 return "<del>" inline( substr( line, 3, len - 4 ) ) "</del>" inline( substr( line, len + 1 ) );
234 # ^superscript^ (pandoc)
235 } else if ( match(line, /^\^([^[:space:]^]|\\[ ^])+\^/) ) {
237 return "<sup>" inline( substr( line, 2, len - 2 ) ) "</sup>" inline( substr( line, len + 1 ) );
239 # ~subscript~ (pandoc)
240 } else if ( match(line, /^~([^[:space:]~]|\\[ ~])+~/) ) {
242 return "<sub>" inline( substr( line, 2, len - 2 ) ) "</sub>" inline( substr( line, len + 1 ) );
244 # ignore embedded underscores (pandoc, php md)
245 } else if ( match(line, "^[[:alnum:]](__|_)") ) {
246 return HTML(substr( line, 1, RLENGTH)) inline( substr(line, RLENGTH + 1) );
249 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__$") ) {
251 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
254 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__[[:space:][:punct:]]") ) {
256 return "<strong>" inline( substr( line, 3, len - 5 ) ) "</strong>" inline( substr( line, len) );
259 } else if ( match(line, "^\\*\\*(([^\\*[:space:]]|" iea ")|([^\\*[:space:]]|" iea ")(" na "|" iea ")*([^\\*[:space:]]|" iea "))\\*\\*") ) {
261 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
264 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_$") ) {
266 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
269 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_[[:space:][:punct:]]") ) {
271 return "<em>" inline( substr( line, 2, len - 3 ) ) "</em>" inline( substr( line, len ) );
274 } else if ( match(line, "^\\*(([^\\*[:space:]]|" isa ")|([^\\*[:space:]]|" isa ")(" na "|" isa ")*([^\\*[:space:]]|" isa "))\\*") ) {
276 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
279 } else if ( AllowMacros && match( line, /^<<([^>]|>[^>])+>>/) ) {
281 return macro( substr( line, 3, len - 4 ) ) inline(substr(line, len + 1));
283 # Verbatim inline HTML
284 } else if ( AllowHTML && match( line, /^(<!--([^-]|-[^-]|--[^>])*-->|<\?([^\?]|\?[^>])*\?>|<![A-Z][^>]*>|<!\[CDATA\[([^\]]|\][^\]]|\]\][^>])*\]\]>|<\/[A-Za-z][A-Za-z0-9-]*[[:space:]]*>|<[A-Za-z][A-Za-z0-9-]*([[:space:]]+[A-Za-z_:][A-Za-z0-9_\.:-]*([[:space:]]*=[[:space:]]*([[:space:]"'=<>`]+|"[^"]*"|'[^']*'))?)*[[:space:]]*\/?>)/) ) {
286 return substr( line, 1, len) inline(substr(line, len + 1));
288 # Literal HTML entities
289 } else if ( match( line, /^&([a-zA-Z]{2,32}|#[0-9]{1,7}|#[xX][0-9a-fA-F]{1,6});/) ) {
291 return substr( line, 1, len ) inline(substr(line, len + 1));
294 } else if ( line ~ /^-->( |$)/) { # ignore multidash-arrow
295 return "-->" inline( substr(line, 4) );
296 } else if ( line ~ /^<-( |$)/) {
297 return "←" inline( substr(line, 3) );
298 } else if ( line ~ /^->( |$)/) {
299 return "→" inline( substr(line, 3) );
301 # Escape lone HTML character
302 } else if ( match( line, /^[&<>"']/) ) {
303 return HTML(substr(line, 1, 1)) inline(substr(line, 2));
305 # continue walk over string
307 return substr(line, 1, 1) inline( substr(line, 2) );
311 function headline( hlvl, htxt, attrib, LOCAL, sec, n, HL) {
312 split( gensub( /^(.* )?([0-9]+( [0-9]+){5})$/, "\\2" ,"1", hstack), HL);
314 for ( n = hlvl; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
315 HL[hlvl]++; for ( n = hlvl + 1; n <= 6; n++) { HL[n] = 0;}
316 hid = ""; for ( n = 2; n <= blvl; n++) { hid = hid BL[n] "/"; }
317 hid = hid HL[1]; for ( n = 2; n <= hlvl; n++) { hid = hid "." HL[n] ; }
318 hid = hid ":" URL(htxt, 1);
320 hstack = gensub( /^(.* )?([0-9]+( [0-9]+){5})$/, "\\1" ,"1", hstack) \
321 HL[1] " " HL[2] " " HL[3] " " HL[4] " " HL[5] " " HL[6];
323 return sec "<section class=\"" (attrib ? "h" hlvl " " attrib : "h" hlvl) "\" id=\"" hid "\">" \
324 "<h" hlvl (attrib ? " class=\"" attrib "\"" : "") ">" inline( htxt ) \
325 "<a class=\"anchor\" href=\"#" hid "\"></a>" \
329 # Nested Block, resets heading counters
330 function _nblock( block, LOCAL, hlsav, sec, n ) {
332 hstack = hstack " 0 0 0 0 0 0";
336 for ( n = blvl + 1; n in BL; n++) { delete BL[n]; }
338 block = _block( block );
339 split( gensub( /^(.* )?([0-9]+( [0-9]+){5})$/, "\\2" ,"1", hstack), HL);
340 sec = ""; for ( n = 1; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
341 hstack = hlsav; blvl--;
345 function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code, indent, list ) {
346 gsub( "(^\n+|\n+$)", "", block );
352 } else if ( AllowHTML && match( block, /(^|\n) ? ? ?(<!--([^-]|-[^-]|--[^>])*(-->|$)|<\?([^\?]|\?[^>])*(\?>|$)|<![A-Z][^>]*(>|$)|<!\[CDATA\[([^\]]|\][^\]]|\]\][^>])*(\]\]>|$))/) ) {
353 len = RLENGTH; st = RSTART;
354 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
357 } else if ( AllowHTML && match( tolower(block), /(^|\n) ? ? ?<\/?(address|article|aside|base|basefont|blockquote|body|caption|center|col|colgroup|dd|details|dialog|dir|div|dl|dt|fieldset|figcaption|figure|footer|form|frame|frameset|h[123456]|head|header|hr|html|iframe|legend|li|link|main|menu|menuitem|nav|noframes|ol|optgroup|option|p|param|section|source|summary|table|tbody|td|tfoot|th|thead|title|tr|track|ul)([[:space:]\n>]|\/>)([^\n]|\n[ \t]*[^\n])*(\n[[:space:]]*\n|$)/) ) {
358 len = RLENGTH; st = RSTART;
359 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
362 } else if ( AllowHTML && match( tolower(block), /(^|\n) ? ? ?<(script|pre|style)([[:space:]\n>]).*(<\/script>|<\/pre>|<\/style>|$)/) ) {
363 len = RLENGTH; st = RSTART;
364 match( tolower(substr(block, st, len)), /(<\/script>|<\/pre>|<\/style>)/);
365 len = RSTART + RLENGTH;
366 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
369 } else if ( AllowHTML && match( block, /^ ? ? ?(<\/[A-Za-z][A-Za-z0-9-]*[[:space:]]*>|<[A-Za-z][A-Za-z0-9-]*([[:space:]]+[A-Za-z_:][A-Za-z0-9_\.:-]*([[:space:]]*=[[:space:]]*([[:space:]"'=<>`]+|"[^"]*"|'[^']*'))?)*[[:space:]]*\/?>)([[:space:]]*\n)([^\n]|\n[ \t]*[^\n])*(\n[[:space:]]*\n|$)/) ) {
370 len = RLENGTH; st = RSTART;
371 return substr(block, st, len) _block(substr(block, st + len));
373 # Metadata (custom, block starting with %something)
374 # Metadata is ignored but can be interpreted externally
375 } else if ( match(block, /^%[a-zA-Z]+([[:space:]][^\n]*)?(\n|$)(%[a-zA-Z]+([[:space:]][^\n]*)?(\n|$)|%([[:space:]][^\n]*)?(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
376 len = RLENGTH; st = RSTART;
377 return _block( substr( block, len + 1) );
379 # Blockquote (leading >)
380 } else if ( match( block, /^> /) ) {
381 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/);
382 len = RLENGTH; st = RSTART;
383 return "<blockquote>" gensub( /^\n|\n$/, "", "g", _nblock( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) ) "</blockquote>\n\n" \
384 _block( substr(block, st + len) );
386 # Pipe Tables (pandoc / php md / gfm )
387 } else if ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?)\n" \
388 "((\\|)?:?(-+:?[\\|+])+:?-+:?(\\|)?)\n" \
389 "((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ) {
390 len = RLENGTH; st = RSTART;
391 #initialize empty arrays
392 split("", talign); split("", tarray);
393 cols = 0; cnt=0; ttext = "";
395 # table header and alignment
396 split( gensub( /(^\||\|$)/, "", "g", \
397 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
398 substr(block, 1, match(block, /(\n|$)/)) \
400 block = substr(block, match(block, /(\n|$)/) + 1 );
402 gensub( /(^\||\|$)/, "", "g", \
403 substr(block, 1, match(block, /(\n|$)/)) \
405 block = substr(block, match(block, /(\n|$)/) + 1 );
407 for( cnt = 1; cnt < cols; cnt++ ) {
408 if (match(talign[cnt], /:-+:/)) talign[cnt]="center";
409 else if (match(talign[cnt], /-+:/)) talign[cnt]="right";
410 else if (match(talign[cnt], /:-+/)) talign[cnt]="left";
414 ttext = "<thead>\n<tr>"
415 for (cnt = 1; cnt < cols; cnt++)
416 ttext = ttext "<th align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</th>"
417 ttext = ttext "</tr>\n</thead><tbody>\n"
419 while ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ){
420 split( gensub( /(^\||\|$)/, "", "g", \
421 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
422 substr(block, 1, match(block, /(\n|$)/)) \
424 block = substr(block, match(block, /(\n|$)/) + 1 );
427 for (cnt = 1; cnt < cols; cnt++)
428 ttext = ttext "<td align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</td>"
429 ttext = ttext "</tr>\n"
431 return "<table>" ttext "</tbody></table>\n" _block(block);
433 # Grid Tables (pandoc)
434 # (with, and without header)
435 } else if ( match( block, "^\\+(-+\\+)+\n" \
436 "(\\|([^\n]+\\|)+\n)+" \
437 "(\\+(:?=+:?\\+)+)\n" \
438 "((\\|([^\n]+\\|)+\n)+" \
439 "\\+(-+\\+)+(\n|$))+" \
441 match( block, "^()()()" \
442 "(\\+(:?-+:?\\+)+)\n" \
443 "((\\|([^\n]+\\|)+\n)+" \
444 "\\+(-+\\+)+(\n|$))+" \
446 len = RLENGTH; st = RSTART;
447 #initialize empty arrays
448 split("", talign); split("", tarray); split("", tread);
449 cols = 0; cnt=0; ttext = "";
452 cols = split( gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block), tread, /\+/) - 2;
453 # debug(" Cols: " gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block ));
456 split( gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block ), talign, /\+/ );
457 # debug("Align: " gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block ));
459 for (cnt = 1; cnt <= cols; cnt++) {
460 if (match(talign[cnt], /:(-+|=+):/)) talign[cnt]="center";
461 else if (match(talign[cnt], /(-+|=+):/)) talign[cnt]="right";
462 else if (match(talign[cnt], /:(-+|=+)/ )) talign[cnt]="left";
466 if ( match(block, "^\\+(-+\\+)+\n" \
467 "(\\|([^\n]+\\|)+\n)+" \
468 "\\+(:?=+:?\\+)+\n" \
469 "((\\|([^\n]+\\|)+\n)+" \
470 "\\+(-+\\+)+(\n|$))+" \
473 block = substr(block, match(block, /(\n|$)/) + 1 );
474 while ( match(block, "^\\|([^\n]+\\|)+\n") ) {
475 split( gensub( /(^\||\|$)/, "", "g", \
476 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
477 substr(block, 1, match(block, /(\n|$)/)) \
479 block = substr(block, match(block, /(\n|$)/) + 1 );
480 for (cnt = 1; cnt <= cols; cnt++)
481 tarray[cnt] = tarray[cnt] "\n" tread[cnt];
484 ttext = "<thead>\n<tr>"
485 for (cnt = 1; cnt <= cols; cnt++)
486 ttext = ttext "<th align=\"" talign[cnt] "\">" _nblock(tarray[cnt]) "</th>"
487 ttext = ttext "</tr>\n</thead>"
491 block = substr(block, match(block, /(\n|$)/) + 1 );
492 ttext = ttext "<tbody>\n"
494 while ( match(block, /^((\|([^\n]+\|)+\n)+\+(-+\+)+(\n|$))+/ ) ){
496 while ( match(block, /^\|([^\n]+\|)+\n/) ) {
497 split( gensub( /(^\||\|$)/, "", "g", \
498 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
499 substr(block, 1, match(block, /(\n|$)/)) \
501 block = substr(block, match(block, /(\n|$)/) + 1 );
502 for (cnt = 1; cnt <= cols; cnt++)
503 tarray[cnt] = tarray[cnt] "\n" tread[cnt];
505 block = substr(block, match(block, /(\n|$)/) + 1 );
508 for (cnt = 1; cnt <= cols; cnt++)
509 ttext = ttext "<td align=\"" talign[cnt] "\">" _nblock(tarray[cnt]) "</td>"
510 ttext = ttext "</tr>\n"
512 return "<table>" ttext "</tbody></table>\n" _nblock(block);
514 # Line Blocks (pandoc)
515 } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
516 len = RLENGTH; st = RSTART;
517 text = substr(block, 1, len);
518 gsub(/\n[[:space:]]+/, " ", text);
519 gsub(/\n\| /, "\n", text);
520 gsub(/^\| |\n$/, "", text);
521 return "<div class=\"line-block\">" gensub(/\n/, "<br>\n", "g", inline( text )) "</div>\n" \
522 _block( substr( block, len + 1) );
524 # Indented Code Block
525 } else if ( match(block, /^( |\t)( *\t*[^ \t\n]+ *\t*)+(\n|$)(( |\t)[^\n]+(\n|$)|[ \t]*(\n|$))*/) ) {
526 len = RLENGTH; st = RSTART;
527 code = substr(block, 1, len);
528 gsub(/(^|\n)( |\t)/, "\n", code);
529 gsub(/^\n|\n+$/, "", code);
530 return "<pre><code>" HTML( code ) "</code></pre>\n" \
531 _block( substr( block, len + 1 ) );
533 # Fenced Divs (pandoc, custom)
534 } else if ( match( block, /^(:::+)/ ) ) {
535 guard = substr( block, 1, RLENGTH );
536 code = block; sub(/^[^\n]+\n/, "", code);
537 attrib = gensub(/^:::+[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\1", 1, block);
538 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib);
539 gsub(/(^ | $)/, "", attrib);
540 if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
541 len = RLENGTH; st = RSTART;
542 return "<div class=\"" attrib "\">" _nblock( substr(code, 1, st - 1) ) "</div>\n" \
543 _block( substr( code, st + len ) );
545 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
546 len = RLENGTH; st = RSTART;
547 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
548 _block( substr(block, st + len) );
551 # Fenced Code Block (pandoc)
552 } else if ( match( block, /^(~~~+|```+)/ ) ) {
553 guard = substr( block, 1, RLENGTH );
554 code = gensub(/^[^\n]+\n/, "", 1, block);
555 attrib = gensub(/^(~~~+|```+)[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\2", 1, block);
556 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib);
557 gsub(/(^ | $)/, "", attrib);
558 if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
559 len = RLENGTH; st = RSTART;
560 return "<pre><code class=\"" attrib "\">" HTML( substr(code, 1, st - 1) ) "</code></pre>\n" \
561 _block( substr( code, st + len ) );
563 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
564 len = RLENGTH; st = RSTART;
565 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
566 _block( substr(block, st + len) );
569 # First Order Heading H1 + Attrib
570 } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n|$)/ ) ) {
572 text = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "\\1", 1, block)
573 attrib = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "\\3", 1, block)
574 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
576 return headline(1, text, attrib) \
577 _block( substr( block, len + 1 ) );
579 # First Order Heading H1
580 } else if ( match( block, /^([^\n]+)\n===+(\n|$)/ ) ) {
582 text = gensub(/^([^\n]+)\n===+(\n.*)?$/, "\\1", 1, block)
584 return headline(1, text, 0) \
585 _block( substr( block, len + 1 ) );
587 # Second Order Heading H2 + Attrib
588 } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n|$)/ ) ) {
590 text = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "\\1", 1, block)
591 attrib = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "\\3", 1, block)
592 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
594 return headline(2, text, attrib) \
595 _block( substr( block, len + 1) );
597 # Second Order Heading H2
598 } else if ( match( block, /^([^\n]+)\n---+(\n|$)/ ) ) {
600 text = gensub(/^([^\n]+)\n---+(\n.*)?$/, "\\1", 1, block)
602 return headline(2, text, 0) \
603 _block( substr( block, len + 1) );
605 # Nth Order Heading H1 H2 H3 H4 H5 H6 + Attrib
606 } else if ( match( block, /^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n|$)/ ) ) {
608 n = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\1", 1, block);
609 text = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\2", 1, block);
610 attrib = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\5", 1, block);
611 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
613 return headline( length(n), text, attrib ) \
614 _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|$)/ ) ) {
619 n = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n.*)?$/, "\\1", 1, block);
620 text = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n.*)?$/, "\\2", 1, block);
622 return headline( length(n), text, 0 ) \
623 _block( substr( block, len + 1) );
625 # block images (wrapped in <figure>)
626 } else if ( match(block, /^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n|$)/) ) {
628 text = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\1", "g", block);
629 href = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\2", "g", block);
630 title = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\4", "g", block);
631 attrib = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\6", "g", block);
632 if ( title && attrib ) {
633 return "<figure data-src=\"" URL(href, 1) "\" class=\"" HTML(attrib) "\">" \
634 "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
635 "<figcaption>" inline(title) "</figcaption>" \
637 _block( substr( block, len + 1) );
638 } else if ( title ) {
639 return "<figure data-src=\"" URL(href, 1) "\">" \
640 "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
641 "<figcaption>" inline(title) "</figcaption>" \
643 _block( substr( block, len + 1) );
644 } else if ( attrib ) {
645 return "<figure data-src=\"" URL(href, 1) "\" class=\"" HTML(attrib) "\">" \
646 "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
648 _block( substr( block, len + 1) );
650 return "<figure data-src=\"" URL(href, 1) "\">" \
651 "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
653 _block( substr( block, len + 1) );
656 # reference style images (block)
657 } else if ( match(line, /^!\[([^]]*)\] ?\[([^]]*)\](\n|$)/ ) ) {
659 text = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\1", 1, block);
660 id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\2", 1, block);
661 if ( ! id ) id = text;
662 if ( rl_href[id] && rl_title[id] ) {
663 return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
664 "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
665 "<figcaption>" inline(rl_title[id]) "</figcaption>" \
667 _block( substr( block, len + 1) );
668 } else if ( rl_href[id] ) {
669 return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
670 "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
672 _block( substr( block, len + 1) );
674 return "<p>" HTML(substr(block, 1, len)) "</p>\n" _block( substr(block, len + 1) );
677 # Macros (standalone <<macro>> calls handled as block, so they are not wrapped in paragraph)
678 } else if ( AllowMacros && match( block, /^<<(([^>]|>[^>])+)>>(\n|$)/) ) {
680 text = gensub(/^<<(([^>]|>[^>])+)>>(\n.*)?$/, "\\1", 1, block);
681 return macro(text) _block(substr(block, len + 1) );
684 } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
685 len = RLENGTH; st = RSTART;
686 return _block(substr(block, 1, st - 1)) "<hr>\n" _block(substr(block, st + len));
689 } else if (match( block, "^(([ \t]*\n)*[^:\n \t][^\n]+\n" \
690 "([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
691 "(([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
692 "|[^:\n \t][^\n]+(\n|$)" \
693 "|( ? ? ?\t| +)[^\n]+(\n|$)" \
694 "|([ \t]*\n)+( ? ? ?\t| +)[^\n]+(\n|$))*)+" \
696 list = substr( block, 1, RLENGTH); block = substr( block, RLENGTH + 1);
697 return "\n<dl>\n" _dlist( list ) "</dl>\n" _block( block );
700 } else if ( match( block, "(^|\n) ? ? ?[-+*][ \t][^\n]+(\n|$)" \
701 "(([ \t]*\n)* ? ? ?[-+*][ \t][^\n]+(\n|$)" \
702 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
703 "|[^\n \t][^\n]+(\n|$))*" ) ) {
704 st = RSTART; len = RLENGTH; list = substr( block, RSTART, RLENGTH);
705 sub("^\n", "", list); match(list, "^ ? ? ?[-+*]"); indent = RLENGTH;
706 gsub( "(^|\n) {0," indent - 1 "}", "\n", list); sub("^\n", "", list);
708 text = substr(block, 1, st - 1); block = substr(block, st + len);
709 if (match( list, "\n([0-9]+\\.|#\\.)[ \t]" )) {
710 block = substr(list, RSTART + 1) block;
711 list = substr(list, 1, RSTART);
714 return _block( text ) "<ul>\n" _list( list, "[-+*]" ) "</ul>\n" _block( block );
717 } else if ( match( block, "(^|\n) ? ? ?([0-9]+\\.|#\\.)[ \t][^\n]+(\n|$)" \
718 "(([ \t]*\n)* ? ? ?([0-9]+\\.|#\\.)[ \t][^\n]+(\n|$)" \
719 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
720 "|[^\n \t][^\n]+(\n|$))*" ) ) {
721 st = RSTART; len = RLENGTH; list = substr( block, RSTART, RLENGTH);
722 sub("^\n", "", list); match(list, "^ ? ? ?[0-9#]"); indent = RLENGTH;
723 gsub( "(^|\n) {0," indent - 1 "}", "\n", list); sub("^\n", "", list);
725 text = substr(block, 1, st - 1); block = substr(block, st + len);
726 if (match( list, "\n[-+*][ \t]" )) {
727 block = substr(list, RSTART + 1) block;
728 list = substr(list, 1, RSTART);
731 return _block( text ) "<ol>\n" _list( list, "([0-9]+\\.|#\\.)" ) "</ol>\n" _block( block );
734 } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) {
735 len = RLENGTH; st = RSTART;
736 return _block( substr(block, 1, st - 1) ) "\n" \
737 _block( substr(block, st + len) );
741 return "<p>" inline(block) "</p>\n";
745 function _list (block, mark, LOCAL, len, st, text, indent, task) {
746 if ( match(block, "^([ \t]*\n)*$")) return;
747 match(block, "^" mark "[ \t]"); indent = RLENGTH;
748 sub("^" mark "[ \t]", "", block);
750 match( block, "\n" mark "[ \t][^\n]+(\n|$)" \
751 "(([ \t]*\n)* ? ? ?" mark "[ \t][^\n]+(\n|$)" \
752 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
753 "|[^\n \t][^\n]+(\n|$))*");
754 (RLENGTH == -1) ? st = length(block) + 1 : st = RSTART;
755 text = substr(block, 1, st); block = substr(block, st + 1);
757 gsub("\n {0," indent "}", "\n", text);
759 task = match( text, /^\[ \]/ ) ? "<li class=\"task pending\"><input type=checkbox disabled>" : \
760 match( text, /^\[-\]/ ) ? "<li class=\"task negative\"><input type=checkbox disabled>" : \
761 match( text, /^\[\/\]/ ) ? "<li class=\"task partial\"><input type=checkbox disabled>" : \
762 match( text, /^\[\?\]/ ) ? "<li class=\"task unsure\"><input type=checkbox disabled>" : \
763 match( text, /^\[[xX]\]/) ? "<li class=\"task done\"><input type=checkbox disabled checked>" : "<li>";
764 sub(/^\[[-? /xX]\]/, "", text);
766 text = _block( text );
767 if (match( text, "^<p>(</p[^>]|</[^p]|<[^/]|[^<])*</p>\n$" ))
768 gsub( "(^<p>|</p>\n$)", "", text);
770 return task text "</li>\n" _list(block, mark);
773 function _dlist (block, LOCAL, len, st, text, indent, p) {
774 if (match( block, "^([ \t]*\n)*[^:\n \t][^\n]+\n" )) {
775 len = RLENGTH; text = substr(block, 1, len);
776 gsub( "(^\n*|\n*$)", "", text );
777 return "<dt>" inline( text ) "</dt>\n" _dlist( substr(block, len + 1) );
778 } else if (match( block, "^([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
779 "([^:\n \t][^\n]+(\n|$)" \
780 "|( ? ? ?\t| +)[^\n]+(\n|$)" \
781 "|([ \t]*\n)+( ? ? ?\t| +)[^\n]+(\n|$))*" \
783 len = RLENGTH; text = substr(block, 1, len);
784 sub( "^([ \t]*\n)*", "", text);
785 match(text, "^ ? ? ?:(\t| +)"); indent = RLENGTH;
786 sub( "^ ? ? ?:(\t| +)", "", text);
787 gsub( "(^|\n) {0," indent "}", "\n", text );
790 if (match( text, "^<p>(</p[^>]|</[^p]|<[^/]|[^<])*</p>\n$" ))
791 gsub( "(^<p>|</p>\n$)", "", text);
793 return "<dd>" text "</dd>\n" _dlist( substr(block, len + 1) );
799 file = ""; rl_href[""] = ""; rl_title[""] = "";
800 if (ENVIRON["MD_HTML"] == "true") { AllowHTML = "true"; }
801 HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
802 # hls = "0 0 0 0 0 0";
804 # Buffering of full file ist necessary, e.g. to find reference links
805 while (getline) { file = file $0 "\n"; }
806 # Clean up MS-DOS line breaks
807 gsub(/\r\n/, "\n", file);
809 # Fill array of reference links
811 re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
812 # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
813 while ( match(f, re_reflink ) ) {
814 rl_id = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
815 rl_href[rl_id] = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
816 rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
817 f = substr(f, RSTART + RLENGTH);
818 rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
819 if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
821 # Clear reflinks from File
822 while( gsub(re_reflink, "\n", file ) );
823 # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
825 # Run Block Processing -> The Actual Markdown!
826 printf "%s", _nblock( file );