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 # - [ ] 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, hlvl, htxt, sec, guard, code, indent, attrib ) {
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>\n" _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 code = substr(block, 1, len);
518 gsub(/\n[[:space:]]+/, " ", code);
519 gsub(/\n\| /, "\n", code);
520 gsub(/^\| |\n$/, "", code);
521 return "<div class=\"line-block\">" gensub(/\n/, "<br>\n", "g", inline( code )) "</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 = gensub(/^[^\n]+\n/, "", 1, block);
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) );
570 } else if ( match( block, "^ ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
571 "(([ \t]*\n)* ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
572 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
573 "|[^\n]+(\n|$))*" ) ) {
574 list = substr( block, 1, RLENGTH);
575 block = substr( block, RLENGTH + 1);
576 indent = length( gensub(/[-+*][ \t]+[^\n]+.*$/, "", 1, list) );
578 gsub("(^|\n) {0," indent "}", "\n", list);
579 return "\n<ul>\n" _list( substr(list, 2) ) "</ul>\n" _block( block );
582 } else if ( match( block, "^ ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
583 "(([ \t]*\n)* ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
584 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
585 "|[^\n]+(\n|$))*" ) ) {
586 list = substr( block, 1, RLENGTH);
587 block = substr( block, RLENGTH + 1);
588 indent = length( gensub(/([0-9]+|#)\.[ \t]+[^\n]+.*$/, "", 1, list) );
590 gsub("(^|\n) {0," indent "}", "\n", list);
591 return "\n<ol>\n" _list( substr(list, 2) ) "</ol>\n" _block( block );
593 # First Order Heading H1 + Attrib
594 } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n|$)/ ) ) {
596 text = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "\\1", 1, block)
597 attrib = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "\\3", 1, block)
598 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
600 return headline(1, text, attrib) \
601 _block( substr( block, len + 1 ) );
603 # First Order Heading H1
604 } else if ( match( block, /^([^\n]+)\n===+(\n|$)/ ) ) {
606 text = gensub(/^([^\n]+)\n===+(\n.*)?$/, "\\1", 1, block)
608 return headline(1, text, 0) \
609 _block( substr( block, len + 1 ) );
611 # Second Order Heading H2 + Attrib
612 } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n|$)/ ) ) {
614 text = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "\\1", 1, block)
615 attrib = gensub(/^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "\\3", 1, block)
616 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
618 return headline(2, text, attrib) \
619 _block( substr( block, len + 1) );
621 # Second Order Heading H2
622 } else if ( match( block, /^([^\n]+)\n---+(\n|$)/ ) ) {
624 text = gensub(/^([^\n]+)\n---+(\n.*)?$/, "\\1", 1, block)
626 return headline(2, text, 0) \
627 _block( substr( block, len + 1) );
629 # Nth Order Heading H1 H2 H3 H4 H5 H6 + Attrib
630 } else if ( match( block, /^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n|$)/ ) ) {
632 n = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\1", 1, block);
633 text = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\2", 1, block);
634 attrib = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "\\5", 1, block);
635 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
637 return headline( length(n), text, attrib ) \
638 _block( substr( block, len + 1) );
640 # Nth Order Heading H1 H2 H3 H4 H5 H6
641 } else if ( match( block, /^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n|$)/ ) ) {
643 n = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n.*)?$/, "\\1", 1, block);
644 text = gensub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n.*)?$/, "\\2", 1, block);
646 return headline( length(n), text, 0 ) \
647 _block( substr( block, len + 1) );
649 # block images (wrapped in <figure>)
650 } else if ( match(block, /^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n|$)/) ) {
652 text = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\1", "g", block);
653 href = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\2", "g", block);
654 title = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\4", "g", block);
655 attrib = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\6", "g", block);
656 if ( title && attrib ) {
657 return "<figure data-src=\"" URL(href, 1) "\" class=\"" HTML(attrib) "\">" \
658 "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
659 "<figcaption>" inline(title) "</figcaption>" \
661 _block( substr( block, len + 1) );
662 } else if ( title ) {
663 return "<figure data-src=\"" URL(href, 1) "\">" \
664 "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
665 "<figcaption>" inline(title) "</figcaption>" \
667 _block( substr( block, len + 1) );
668 } else if ( attrib ) {
669 return "<figure data-src=\"" URL(href, 1) "\" class=\"" HTML(attrib) "\">" \
670 "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
672 _block( substr( block, len + 1) );
674 return "<figure data-src=\"" URL(href, 1) "\">" \
675 "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text) "\">" \
677 _block( substr( block, len + 1) );
680 # reference style images (block)
681 } else if ( match(line, /^!\[([^]]*)\] ?\[([^]]*)\](\n|$)/ ) ) {
683 text = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\1", 1, block);
684 id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\2", 1, block);
685 if ( ! id ) id = text;
686 if ( rl_href[id] && rl_title[id] ) {
687 return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
688 "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
689 "<figcaption>" inline(rl_title[id]) "</figcaption>" \
691 _block( substr( block, len + 1) );
692 } else if ( rl_href[id] ) {
693 return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
694 "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
696 _block( substr( block, len + 1) );
698 return "<p>" HTML(substr(block, 1, len)) "</p>\n" _block( substr(block, len + 1) );
701 # Macros (standalone <<macro>> calls handled as block, so they are not wrapped in paragraph)
702 } else if ( AllowMacros && match( block, /^<<(([^>]|>[^>])+)>>(\n|$)/) ) {
704 text = gensub(/^<<(([^>]|>[^>])+)>>(\n.*)?$/, "\\1", 1, block);
705 return macro(text) _block(substr(block, len + 1) );
708 } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) {
709 len = RLENGTH; st = RSTART;
710 return _block( substr(block, 1, st - 1) ) "\n" \
711 _block( substr(block, st + len) );
714 } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
715 len = RLENGTH; st = RSTART;
716 return _block(substr(block, 1, st - 1)) "<hr>\n" _block(substr(block, st + len));
720 return "<p>" inline(block) "</p>\n";
724 function _list( block, last, LOCAL, p) {
725 if ( ! length(block) ) return "";
726 gsub(/^([-+*]|[0-9]+\.|#\.)( ? ? ?|\t)/, "", block)
728 # slice next list item from input
729 if ( match( block, /\n([-+*]|[0-9]+\.|#\.)[ \t]+[^\n]+/) ) {
730 p = substr( block, 1, RSTART);
731 block = substr( block, RSTART + 1);
733 p = block; block = "";
735 sub( /\n +([-+*]|[0-9]+\.|#\.)/, "\n&", p );
737 # if this should be a paragraph item
738 # either previous item (last) or current item (p) contains blank lines
739 if (match(last, /\n[[:space:]]*\n/) || match(p, /\n[[:space:]]*\n/) ) {
740 last = p; p = _nblock(p);
742 last = p; p = _nblock(p);
743 sub( /^<p>/, "", p );
744 sub( /<\/p>\n/, "", p );
748 # Task List (pandoc, custom)
749 if ( p ~ /^\[ \].*/ ) { return "<li class=\"task pending\"><input type=checkbox disabled>" \
750 substr(p, 4) "</li>\n" _list( block, last );
751 } else if ( p ~ /^\[-\].*/ ) { return "<li class=\"task negative\"><input type=checkbox disabled>" \
752 substr(p, 4) "</li>\n" _list( block, last );
753 } else if ( p ~ /^\[\?\].*/ ) { return "<li class=\"task unsure\"><input type=checkbox disabled>" \
754 substr(p, 4) "</li>\n" _list( block, last );
755 } else if ( p ~ /^\[\/\].*/ ) { return "<li class=\"task partial\"><input type=checkbox disabled>" \
756 substr(p, 4) "</li>\n" _list( block, last );
757 } else if ( p ~ /^\[[xX]\].*/ ) { return "<li class=\"task done\"><input type=checkbox disabled checked>" \
758 substr(p, 4) "</li>\n" _list( block, last );
759 } else if ( p ~ /^<p>\[ \].*/ ) { return "<li class=\"task pending\"><p><input type=checkbox disabled>" \
760 substr(p, 7) "</li>\n" _list( block, last );
761 } else if ( p ~ /^<p>\[-\].*/ ) { return "<li class=\"task negative\"><p><input type=checkbox disabled>" \
762 substr(p, 7) "</li>\n" _list( block, last );
763 } else if ( p ~ /^<p>\[\?\].*/ ) { return "<li class=\"task unsure\"><p><input type=checkbox disabled>" \
764 substr(p, 7) "</li>\n" _list( block, last );
765 } else if ( p ~ /^<p>\[\/\].*/ ) { return "<li class=\"task partial\"><p><input type=checkbox disabled>" \
766 substr(p, 7) "</li>\n" _list( block, last );
767 } else if ( p ~ /^<p>\[[xX]\].*/ ) { return "<li class=\"task done\"><p><input type=checkbox disabled checked>" \
768 substr(p, 7) "</li>\n" _list( block, last );
769 } else { return "<li>" p "</li>\n" _list( block, last ); }
774 file = ""; rl_href[""] = ""; rl_title[""] = "";
775 if (ENVIRON["MD_HTML"] == "true") { AllowHTML = "true"; }
776 HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
777 # hls = "0 0 0 0 0 0";
779 # Buffering of full file ist necessary, e.g. to find reference links
780 while (getline) { file = file $0 "\n"; }
781 # Clean up MS-DOS line breaks
782 gsub(/\r\n/, "\n", file);
784 # Fill array of reference links
786 re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
787 # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
788 while ( match(f, re_reflink ) ) {
789 rl_id = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
790 rl_href[rl_id] = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
791 rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
792 f = substr(f, RSTART + RLENGTH);
793 rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
794 if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
796 # Clear reflinks from File
797 while( gsub(re_reflink, "\n", file ) );
798 # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
800 # Run Block Processing -> The Actual Markdown!
801 printf "%s", _nblock( file );