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] Automatic heading identifiers (custom)
44 # - [x] Fenced code blocks (php md, pandoc)
45 # - [x] Fenced code attributes
46 # - [x] Images (as block elements, <figure>-wrapped) (custom)
47 # - [x] reference style block images
49 # - ? Simple table (pandoc)
50 # - ? Multiline table (pandoc)
51 # - [x] Grid table (pandoc)
53 # - [x] Pipe table (php md, pandoc)
54 # - [x] Line blocks (pandoc)
55 # - [x] Task lists (pandoc, custom)
56 # - [ ] Definition lists (php md, pandoc)
57 # - [-] Numbered example lists (pandoc)
58 # - [-] Metadata blocks (pandoc)
59 # - [x] Metadata blocks (custom)
60 # - [x] Fenced Divs (pandoc)
62 # Extensions - Inline elements:
63 # ----------------------------
64 # - [x] Ignore embedded_underscores (php md, pandoc)
65 # - [x] ~~strikeout~~ (pandoc)
66 # - [x] ^Superscript^ ~Subscript~ (pandoc)
67 # - [-] Bracketed spans (pandoc)
68 # - [-] Inline attributes (pandoc)
69 # - [x] Image attributes (custom, pandoc inspired, not for reference style)
70 # - [x] Wiki style links [[PageName]] / [[PageName|Link Text]]
71 # - [-] TEX-Math (pandoc)
72 # - ? Footnotes (php md)
73 # - ? Abbreviations (php md)
74 # - ? "Curly quotes" (smartypants)
75 # - [ ] em-dashes (--) (smartypants old)
76 # - ? ... three-dot ellipsis (smartypants)
77 # - [-] en-dash (smartypants)
78 # - [ ] Automatic em-dash / en-dash
79 # - [x] Automatic -> Arrows <- (custom)
81 function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; }
83 function HTML ( text ) {
84 gsub( /&/, "\\&", text );
85 gsub( /</, "\\<", text );
86 gsub( />/, "\\>", text );
87 gsub( /"/, "\\"", text );
88 gsub( /'/, "\\'", text );
89 gsub( /\\/, "\\\", text );
93 function URL ( text ) {
94 gsub( /&/, "%26", text );
95 gsub( /"/, "%22", text );
96 gsub( /'/, "%27", text );
97 gsub( /`/, "%60", text );
98 gsub( /\?/, "%3F", text );
99 gsub( /#/, "%23", text );
100 gsub( /\[/, "%5B", text );
101 gsub( /\]/, "%5D", text );
102 gsub( / /, "%20", text );
103 gsub( / /, "%09", text );
104 gsub( /\\/, "%5C", text );
108 function inline( line, LOCAL, len, code, href, guard ) {
109 nu = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\_]|_[[:alnum:]])*" # not underline (except when escaped)
110 na = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\\\*])*" # not asterisk (except when escaped)
111 ieu = "_([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])_" # inner <em> (underline)
112 isu = "__([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])__" # inner <strong> (underline)
113 iea = "\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*" # inner <em> (asterisk)
114 isa = "\\*\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*\\*" # inner <strong> (asterisk)
116 if ( line ~ /^$/ ) { # Recursion End
119 # omit processing of escaped characters
120 } else if ( line ~ /^\\./) {
121 return HTML(substr(line, 2, 1)) inline( substr(line, 3) );
124 } else if ( match(line, /^ \n/) ) {
125 return "<br>\n" inline( substr(line, RLENGTH + 1) );
128 } else if ( match( line, /^`+/) ) {
130 guard = substr( line, 1, len )
131 if ( match(line, guard ".*" guard) ) {
132 code = substr( line, len + 1, match( substr(line, len + 1), guard ) - 1)
133 len = 2 * length(guard) + length(code)
134 # strip single surrounding white spaces
135 code = gensub( /^ | $/, "", "g" , code)
136 # escape HTML within code span
137 gsub( /&/, "\\&", code ); gsub( /</, "\\<", code ); gsub( />/, "\\>", code );
138 return "<code>" code "</code>" inline( substr( line, len + 1 ) )
142 } else if ( match( line, /^\[\[([^]|]+)(\|[^]]+)?\]\]/) ) {
144 href = gensub(/^\[\[([^]|]+)(\|([^]]+))?\]\]/, "\\1", 1, substr(line, 1, len) );
145 text = gensub(/^\[\[([^]|]+)(\|([^]]+))?\]\]/, "\\3", 1, substr(line, 1, len) );
146 if ( ! text ) text = href;
147 return "<a href=\"" URL(href) "\">" HTML(text) "</a>" inline( substr( line, len + 1) );
149 # quick links ("automatic links" in md doc)
150 } else if ( match( line, /^<[a-zA-Z]+:\/\/([-\.[:alnum:]]+)(:[0-9]*)?(\/[^>]*)?>/ ) ) {
152 href = URL( substr( line, 2, len - 2) );
153 return "<a href=\"" href "\">" href "</a>" inline( substr( line, len + 1) );
156 } 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])?)*>/ ) ) {
158 href = URL( substr( line, 2, len - 2) );
159 return "<a href=\"mailto:" href "\">" href "</a>" inline( substr( line, len + 1) );
162 # ,_______________________Image____________________________,
163 } else if ( match(line, /^\[([^]]+|!\[[^]]*\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/) ) {
165 text = gensub(/^\[([^]]+|!\[[^]]*\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, \
166 "\\1", 1, substr(line, 1, len) );
167 href = gensub(/^\[([^]]+|!\[[^]]*\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, \
168 "\\4", 1, substr(line, 1, len) );
169 title = gensub(/^\[([^]]+|!\[[^]]*\]\([^"\)]+([ \t]+"[^"]+")?\)(\{[a-zA-Z \t-]*\})?)\]\(([^"\)]+)([[:space:]]+"([^"]+)")?\)/, \
170 "\\6", 1, substr(line, 1, len) );
172 return "<a href=\"" URL(href) "\" title=\"" HTML(title) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
174 return "<a href=\"" URL(href) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
177 # reference style links
178 } else if ( match(line, /^\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
180 text = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, substr(line, 1, len) );
181 id = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, substr(line, 1, len) );
182 if ( ! id ) id = text;
183 if ( rl_href[id] && rl_title[id] ) {
184 return "<a href=\"" URL(rl_href[id]) "\" title=\"" HTML(rl_title[id]) "\">" inline(text) "</a>" inline( substr( line, len + 1) );
185 } else if ( rl_href[id] ) {
186 return "<a href=\"" URL(rl_href[id]) "\">" inline(text) "</a>" inline( substr( line, len + 1) );
188 return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
192 } else if ( match(line, /^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/) ) {
194 text = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\1", "g", substr(line, 1, len) );
195 href = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\2", "g", substr(line, 1, len) );
196 title = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\4", "g", substr(line, 1, len) );
197 attrib = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?/, "\\6", "g", substr(line, 1, len) );
198 if ( title && attrib ) {
199 return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\" class=\"" HTML(attrib) "\">" \
200 inline( substr( line, len + 1) );
201 } else if ( title ) {
202 return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\">" \
203 inline( substr( line, len + 1) );
204 } else if ( attrib ) {
205 return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
206 inline( substr( line, len + 1) );
208 return "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\">" \
209 inline( substr( line, len + 1) );
212 # reference style images
213 } else if ( match(line, /^!\[([^]]*)\] ?\[([^]]*)\]/ ) ) {
215 text = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\].*/, "\\1", 1, substr(line, 1, len) );
216 id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\].*/, "\\2", 1, substr(line, 1, len) );
217 if ( ! id ) id = text;
218 if ( rl_href[id] && rl_title[id] ) {
219 return "<img src=\"" URL(rl_href[id]) "\" alt=\"" HTML(text) "\" title=\"" HTML(rl_title[id]) "\">" \
220 inline( substr( line, len + 1) );
221 } else if ( rl_href[id] ) {
222 return "<img src=\"" URL(rl_href[id]) "\" alt=\"" HTML(text) "\">" \
223 inline( substr( line, len + 1) );
225 return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
228 # ~~strikeout~~ (pandoc)
229 } else if ( match(line, /^~~([[:graph:]]|[[:graph:]]([^~]|~[^~])*[[:graph:]])~~/) ) {
231 return "<del>" inline( substr( line, 3, len - 4 ) ) "</del>" inline( substr( line, len + 1 ) );
233 # ^superscript^ (pandoc)
234 } else if ( match(line, /^\^([^[:space:]^]|\\[ ^])+\^/) ) {
236 return "<sup>" inline( substr( line, 2, len - 2 ) ) "</sup>" inline( substr( line, len + 1 ) );
238 # ~subscript~ (pandoc)
239 } else if ( match(line, /^~([^[:space:]~]|\\[ ~])+~/) ) {
241 return "<sub>" inline( substr( line, 2, len - 2 ) ) "</sub>" inline( substr( line, len + 1 ) );
243 # ignore embedded underscores (pandoc, php md)
244 } else if ( match(line, "^[[:alnum:]](__|_)") ) {
245 return HTML(substr( line, 1, RLENGTH)) inline( substr(line, RLENGTH + 1) );
248 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__$") ) {
250 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
253 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__[[:space:][:punct:]]") ) {
255 return "<strong>" inline( substr( line, 3, len - 5 ) ) "</strong>" inline( substr( line, len) );
258 } else if ( match(line, "^\\*\\*(([^\\*[:space:]]|" iea ")|([^\\*[:space:]]|" iea ")(" na "|" iea ")*([^\\*[:space:]]|" iea "))\\*\\*") ) {
260 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
263 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_$") ) {
265 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
268 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_[[:space:][:punct:]]") ) {
270 return "<em>" inline( substr( line, 2, len - 3 ) ) "</em>" inline( substr( line, len ) );
273 } else if ( match(line, "^\\*(([^\\*[:space:]]|" isa ")|([^\\*[:space:]]|" isa ")(" na "|" isa ")*([^\\*[:space:]]|" isa "))\\*") ) {
275 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
278 } else if ( AllowMacros && match( line, /^<<([^>]|>[^>])+>>/) ) {
280 return macro( substr( line, 3, len - 4 ) ) inline(substr(line, len + 1));
282 # Verbatim inline HTML
283 } 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:]]*\/?>)/) ) {
285 return substr( line, 1, len) inline(substr(line, len + 1));
287 # Literal HTML entities
288 } else if ( match( line, /^&([a-zA-Z]{2,32}|#[0-9]{1,7}|#[xX][0-9a-fA-F]{1,6});/) ) {
290 return substr( line, 1, len ) inline(substr(line, len + 1));
293 } else if ( line ~ /^-->( |$)/) { # ignore multidash-arrow
294 return "-->" inline( substr(line, 4) );
295 } else if ( line ~ /^<-( |$)/) {
296 return "←" inline( substr(line, 3) );
297 } else if ( line ~ /^->( |$)/) {
298 return "→" inline( substr(line, 3) );
300 # Escape lone HTML character
301 } else if ( match( line, /^[&<>"']/) ) {
302 return HTML(substr(line, 1, 1)) inline(substr(line, 2));
304 # continue walk over string
306 return substr(line, 1, 1) inline( substr(line, 2) );
310 # Nested Block, resets heading counters
311 function _nblock( block, LOCAL, hlsav, sec ) {
312 # Keeping arrays in a local scope is awkward, so we serialize the HL array
313 # into the scalar hlsav
314 hlsav = HL[1] " " HL[2] " " HL[3] " " HL[4] " " HL[5] " " HL[6];
315 HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
320 block = _block( block );
321 sec = ""; for ( n = 1; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
322 split(hlsav, HL); blvl--;
326 function _block( block, LOCAL, st, len, hlvl, htxt, sec, guard, code, indent, attrib ) {
327 gsub( /^\n+|\n+$/, "", block );
333 } else if ( AllowHTML && match( block, /(^|\n) ? ? ?(<!--([^-]|-[^-]|--[^>])*(-->|$)|<\?([^\?]|\?[^>])*(\?>|$)|<![A-Z][^>]*(>|$)|<!\[CDATA\[([^\]]|\][^\]]|\]\][^>])*(\]\]>|$))/) ) {
334 len = RLENGTH; st = RSTART;
335 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
338 } 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|$)/) ) {
339 len = RLENGTH; st = RSTART;
340 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
343 } else if ( AllowHTML && match( tolower(block), /(^|\n) ? ? ?<(script|pre|style)([[:space:]\n>]).*(<\/script>|<\/pre>|<\/style>|$)/) ) {
344 len = RLENGTH; st = RSTART;
345 match( tolower(substr(block, st, len)), /(<\/script>|<\/pre>|<\/style>)/);
346 len = RSTART + RLENGTH;
347 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
350 } 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|$)/) ) {
351 len = RLENGTH; st = RSTART;
352 return substr(block, st, len) _block(substr(block, st + len));
354 # Metadata (custom, block starting with %something)
355 # Metadata is ignored but can be interpreted externally
356 } else if ( match(block, /^%[a-zA-Z]+([[:space:]][^\n]*)?(\n|$)(%[a-zA-Z]+([[:space:]][^\n]*)?(\n|$)|%([[:space:]][^\n]*)?(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
357 len = RLENGTH; st = RSTART;
358 return _block( substr( block, len + 1) );
360 # Blockquote (leading >)
361 } else if ( match( block, /^> /) ) {
362 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/);
363 len = RLENGTH; st = RSTART;
364 return "<blockquote>\n" _nblock( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) "</blockquote>\n\n" \
365 _block( substr(block, st + len) );
367 # Pipe Tables (pandoc / php md / gfm )
368 } else if ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?)\n" \
369 "((\\|)?:?(-+:?[\\|+])+:?-+:?(\\|)?)\n" \
370 "((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ) {
371 len = RLENGTH; st = RSTART;
372 #initialize empty arrays
373 split("", talign); split("", tarray);
374 cols = 0; cnt=0; ttext = "";
376 # table header and alignment
377 split( gensub( /(^\||\|$)/, "", "g", \
378 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
379 substr(block, 1, match(block, /(\n|$)/)) \
381 block = substr(block, match(block, /(\n|$)/) + 1 );
383 gensub( /(^\||\|$)/, "", "g", \
384 substr(block, 1, match(block, /(\n|$)/)) \
386 block = substr(block, match(block, /(\n|$)/) + 1 );
388 for( cnt = 1; cnt < cols; cnt++ ) {
389 if (match(talign[cnt], /:-+:/)) talign[cnt]="center";
390 else if (match(talign[cnt], /-+:/)) talign[cnt]="right";
391 else if (match(talign[cnt], /:-+/)) talign[cnt]="left";
395 ttext = "<thead>\n<tr>"
396 for (cnt = 1; cnt < cols; cnt++)
397 ttext = ttext "<th align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</th>"
398 ttext = ttext "</tr>\n</thead><tbody>\n"
400 while ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ){
401 split( gensub( /(^\||\|$)/, "", "g", \
402 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
403 substr(block, 1, match(block, /(\n|$)/)) \
405 block = substr(block, match(block, /(\n|$)/) + 1 );
408 for (cnt = 1; cnt < cols; cnt++)
409 ttext = ttext "<td align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</td>"
410 ttext = ttext "</tr>\n"
412 return "<table>" ttext "</tbody></table>\n" _block(block);
414 # Grid Tables (pandoc)
415 # (with, and without header)
416 } else if ( match( block, "^\\+(-+\\+)+\n" \
417 "(\\|([^\n]+\\|)+\n)+" \
418 "(\\+(:?=+:?\\+)+)\n" \
419 "((\\|([^\n]+\\|)+\n)+" \
420 "\\+(-+\\+)+(\n|$))+" \
422 match( block, "^()()()" \
423 "(\\+(:?-+:?\\+)+)\n" \
424 "((\\|([^\n]+\\|)+\n)+" \
425 "\\+(-+\\+)+(\n|$))+" \
427 len = RLENGTH; st = RSTART;
428 #initialize empty arrays
429 split("", talign); split("", tarray); split("", tread);
430 cols = 0; cnt=0; ttext = "";
433 cols = split( gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block), tread, /\+/) - 2;
434 # debug(" Cols: " gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block ));
437 split( gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block ), talign, /\+/ );
438 # debug("Align: " gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block ));
440 for (cnt = 1; cnt <= cols; cnt++) {
441 if (match(talign[cnt], /:(-+|=+):/)) talign[cnt]="center";
442 else if (match(talign[cnt], /(-+|=+):/)) talign[cnt]="right";
443 else if (match(talign[cnt], /:(-+|=+)/ )) talign[cnt]="left";
447 if ( match(block, "^\\+(-+\\+)+\n" \
448 "(\\|([^\n]+\\|)+\n)+" \
449 "\\+(:?=+:?\\+)+\n" \
450 "((\\|([^\n]+\\|)+\n)+" \
451 "\\+(-+\\+)+(\n|$))+" \
454 block = substr(block, match(block, /(\n|$)/) + 1 );
455 while ( match(block, "^\\|([^\n]+\\|)+\n") ) {
456 split( gensub( /(^\||\|$)/, "", "g", \
457 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
458 substr(block, 1, match(block, /(\n|$)/)) \
460 block = substr(block, match(block, /(\n|$)/) + 1 );
461 for (cnt = 1; cnt <= cols; cnt++)
462 tarray[cnt] = tarray[cnt] "\n" tread[cnt];
465 ttext = "<thead>\n<tr>"
466 for (cnt = 1; cnt <= cols; cnt++)
467 ttext = ttext "<th align=\"" talign[cnt] "\">" _nblock(tarray[cnt]) "</th>"
468 ttext = ttext "</tr>\n</thead>"
472 block = substr(block, match(block, /(\n|$)/) + 1 );
473 ttext = ttext "<tbody>\n"
475 while ( match(block, /^((\|([^\n]+\|)+\n)+\+(-+\+)+(\n|$))+/ ) ){
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];
486 block = substr(block, match(block, /(\n|$)/) + 1 );
489 for (cnt = 1; cnt <= cols; cnt++)
490 ttext = ttext "<td align=\"" talign[cnt] "\">" _nblock(tarray[cnt]) "</td>"
491 ttext = ttext "</tr>\n"
493 return "<table>" ttext "</tbody></table>\n" _nblock(block);
495 # Line Blocks (pandoc)
496 } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
497 len = RLENGTH; st = RSTART;
498 code = substr(block, 1, len);
499 gsub(/\n[[:space:]]+/, " ", code);
500 gsub(/\n\| /, "\n", code);
501 gsub(/^\| |\n$/, "", code);
502 return "<div class=\"line-block\">" gensub(/\n/, "<br>\n", "g", inline( code )) "</div>\n" \
503 _block( substr( block, len + 1) );
505 # Indented Code Block
506 } else if ( match(block, /^( |\t)( *\t*[^ \t\n]+ *\t*)+(\n|$)(( |\t)[^\n]+(\n|$)|[ \t]*(\n|$))*/) ) {
507 len = RLENGTH; st = RSTART;
508 code = substr(block, 1, len);
509 gsub(/(^|\n)( |\t)/, "\n", code);
510 gsub(/^\n|\n+$/, "", code);
511 return "<pre><code>" HTML( code ) "</code></pre>\n" \
512 _block( substr( block, len + 1 ) );
514 # Fenced Divs (pandoc, custom)
515 } else if ( match( block, /^(:::+)/ ) ) {
516 guard = substr( block, 1, RLENGTH );
517 code = gensub(/^[^\n]+\n/, "", 1, block);
518 attrib = gensub(/^:::+[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\1", 1, block);
519 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib);
520 gsub(/(^ | $)/, "", attrib);
521 if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
522 len = RLENGTH; st = RSTART;
523 return "<div class=\"" attrib "\">" _nblock( substr(code, 1, st - 1) ) "</div>\n" \
524 _block( substr( code, st + len ) );
526 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
527 len = RLENGTH; st = RSTART;
528 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
529 _block( substr(block, st + len) );
532 # Fenced Code Block (pandoc)
533 } else if ( match( block, /^(~~~+|```+)/ ) ) {
534 guard = substr( block, 1, RLENGTH );
535 code = gensub(/^[^\n]+\n/, "", 1, block);
536 attrib = gensub(/^(~~~+|```+)[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\2", 1, block);
537 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib);
538 gsub(/(^ | $)/, "", attrib);
539 if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
540 len = RLENGTH; st = RSTART;
541 return "<pre><code class=\"" attrib "\">" HTML( substr(code, 1, st - 1) ) "</code></pre>\n" \
542 _block( substr( code, st + len ) );
544 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
545 len = RLENGTH; st = RSTART;
546 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
547 _block( substr(block, st + len) );
551 } else if ( match( block, "^ ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
552 "(([ \t]*\n)* ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
553 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
554 "|[^\n]+(\n|$))*" ) ) {
555 list = substr( block, 1, RLENGTH);
556 block = substr( block, RLENGTH + 1);
557 indent = length( gensub(/[-+*][ \t]+[^\n]+.*$/, "", 1, list) );
559 gsub("(^|\n) {0," indent "}", "\n", list);
560 return "\n<ul>\n" _list( substr(list, 2) ) "</ul>\n" _block( block );
563 } else if ( match( block, "^ ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
564 "(([ \t]*\n)* ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
565 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
566 "|[^\n]+(\n|$))*" ) ) {
567 list = substr( block, 1, RLENGTH);
568 block = substr( block, RLENGTH + 1);
569 indent = length( gensub(/([0-9]+|#)\.[ \t]+[^\n]+.*$/, "", 1, list) );
571 gsub("(^|\n) {0," indent "}", "\n", list);
572 return "\n<ol>\n" _list( substr(list, 2) ) "</ol>\n" _block( block );
574 # First Order Heading H1
575 } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
576 len = RLENGTH; sec = "";
578 for ( n = 1; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
579 HL[1]++; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
580 hid = HL[1] ":" URL(gensub( /\n.*$/, "", "g", block ));
582 return sec "<section class=\"h1\"" ((blvl > 1)?"":" id=\"" hid "\"") ">" \
583 "<h1>" inline( gensub( /\n.*$/, "", "g", block ) ) \
584 ((blvl > 1)?"":"<a class=\"anchor\" href=\"#" hid "\"></a>") \
586 _block( substr( block, len + 1 ) );
588 # Second Order Heading H2
589 } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
590 len = RLENGTH; sec = "";
592 for ( n = 2; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
593 HL[2]++; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
594 hid= HL[1] "." HL[2] ":" URL(gensub( /\n.*$/, "", "g", block ));
596 return sec "<section class=\"h2\"" ((blvl > 1)?"":" id=\"" hid "\"") ">" \
597 "<h2>" inline( gensub( /\n.*$/, "", "g", block ) ) \
598 ((blvl > 1)?"":"<a class=\"anchor\" href=\"#" hid "\"></a>") \
600 _block( substr( block, len + 1) );
602 # Nth Order Heading H1 H2 H3 H4 H5 H6
603 } else if ( match( block, /^#{1,6}[ \t]*[^\n]+([ \t]*#*)(\n|$)/ ) ) {
604 len = RLENGTH; sec = "";
606 hlvl = length( gensub( /^(#{1,6}).*$/, "\\1", "g", block ) );
607 htxt = gensub(/^#{1,6}[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[^\n#])+)([ \t]*#*)(\n.*)?$/, "\\1", 1, block);
609 for ( n = hlvl; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
610 HL[hlvl]++; for ( n = hlvl + 1; n <= 6; n++) { HL[n] = 0;}
611 hid = HL[1]; for ( n = 2; n <= hlvl; n++) { hid = hid "." HL[n] ; }
612 hid = hid ":" URL(htxt);
614 return sec "<section class=\"h" hlvl "\"" ((blvl > 1)?"":" id=\"" hid "\"") ">" \
615 "<h" hlvl ">" inline( htxt ) \
616 ((blvl > 1)?"":"<a class=\"anchor\" href=\"#" hid "\"></a>") \
618 _block( substr( block, len + 1) );
620 # block images (wrapped in <figure>)
621 } else if ( match(block, /^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n|$)/) ) {
623 text = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\1", "g", block);
624 href = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\2", "g", block);
625 title = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\4", "g", block);
626 attrib = gensub(/^!\[([^]]*)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)(\{([a-zA-Z \t-]*)\})?(\n.*)?$/, "\\6", "g", block);
627 if ( title && attrib ) {
628 return "<figure data-src=\"" URL(href) "\" class=\"" HTML(attrib) "\">" \
629 "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
630 "<figcaption>" inline(title) "</figcaption>" \
632 _block( substr( block, len + 1) );
633 } else if ( title ) {
634 return "<figure data-src=\"" URL(href) "\">" \
635 "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\">" \
636 "<figcaption>" inline(title) "</figcaption>" \
638 _block( substr( block, len + 1) );
639 } else if ( attrib ) {
640 return "<figure data-src=\"" URL(href) "\" class=\"" HTML(attrib) "\">" \
641 "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\" class=\"" HTML(attrib) "\">" \
643 _block( substr( block, len + 1) );
645 return "<figure data-src=\"" URL(href) "\">" \
646 "<img src=\"" URL(href) "\" alt=\"" HTML(text) "\">" \
648 _block( substr( block, len + 1) );
651 # reference style images (block)
652 } else if ( match(line, /^!\[([^]]*)\] ?\[([^]]*)\](\n|$)/ ) ) {
654 text = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\1", 1, block);
655 id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\2", 1, block);
656 if ( ! id ) id = text;
657 if ( rl_href[id] && rl_title[id] ) {
658 return "<figure data-src=\"" URL(rl_href[id]) "\">" \
659 "<img src=\"" URL(rl_href[id]) "\" alt=\"" HTML(text) "\">" \
660 "<figcaption>" inline(rl_title[id]) "</figcaption>" \
662 _block( substr( block, len + 1) );
663 } else if ( rl_href[id] ) {
664 return "<figure data-src=\"" URL(rl_href[id]) "\">" \
665 "<img src=\"" URL(rl_href[id]) "\" alt=\"" HTML(text) "\">" \
667 _block( substr( block, len + 1) );
669 return "<p>" HTML(substr(block, 1, len)) "</p>\n" _block( substr(block, len + 1) );
672 # Macros (standalone <<macro>> calls handled as block, so they are not wrapped in paragraph)
673 } else if ( AllowMacros && match( block, /^<<(([^>]|>[^>])+)>>(\n|$)/) ) {
675 text = gensub(/^<<(([^>]|>[^>])+)>>(\n.*)?$/, "\\1", 1, block);
676 return macro(text) _block(substr(block, len + 1) );
679 } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) {
680 len = RLENGTH; st = RSTART;
681 return _block( substr(block, 1, st - 1) ) "\n" \
682 _block( substr(block, st + len) );
685 } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
686 len = RLENGTH; st = RSTART;
687 return _block(substr(block, 1, st - 1)) "<hr>\n" _block(substr(block, st + len));
691 return "<p>" inline(block) "</p>\n";
695 function _list( block, last, LOCAL, p) {
696 if ( ! length(block) ) return "";
697 gsub(/^([-+*]|[0-9]+\.|#\.)( ? ? ?|\t)/, "", block)
699 # slice next list item from input
700 if ( match( block, /\n([-+*]|[0-9]+\.|#\.)[ \t]+[^\n]+/) ) {
701 p = substr( block, 1, RSTART);
702 block = substr( block, RSTART + 1);
704 p = block; block = "";
706 sub( /\n +([-+*]|[0-9]+\.|#\.)/, "\n&", p );
708 # if this should be a paragraph item
709 # either previous item (last) or current item (p) contains blank lines
710 if (match(last, /\n[[:space:]]*\n/) || match(p, /\n[[:space:]]*\n/) ) {
711 last = p; p = _nblock(p);
713 last = p; p = _nblock(p);
714 sub( /^<p>/, "", p );
715 sub( /<\/p>\n/, "", p );
719 # Task List (pandoc, custom)
720 if ( p ~ /^\[ \].*/ ) { return "<li class=\"task pending\"><input type=checkbox disabled>" \
721 substr(p, 4) "</li>\n" _list( block, last );
722 } else if ( p ~ /^\[-\].*/ ) { return "<li class=\"task negative\"><input type=checkbox disabled>" \
723 substr(p, 4) "</li>\n" _list( block, last );
724 } else if ( p ~ /^\[\?\].*/ ) { return "<li class=\"task unsure\"><input type=checkbox disabled>" \
725 substr(p, 4) "</li>\n" _list( block, last );
726 } else if ( p ~ /^\[\/\].*/ ) { return "<li class=\"task partial\"><input type=checkbox disabled>" \
727 substr(p, 4) "</li>\n" _list( block, last );
728 } else if ( p ~ /^\[[xX]\].*/ ) { return "<li class=\"task done\"><input type=checkbox disabled checked>" \
729 substr(p, 4) "</li>\n" _list( block, last );
730 } else if ( p ~ /^<p>\[ \].*/ ) { return "<li class=\"task pending\"><p><input type=checkbox disabled>" \
731 substr(p, 7) "</li>\n" _list( block, last );
732 } else if ( p ~ /^<p>\[-\].*/ ) { return "<li class=\"task negative\"><p><input type=checkbox disabled>" \
733 substr(p, 7) "</li>\n" _list( block, last );
734 } else if ( p ~ /^<p>\[\?\].*/ ) { return "<li class=\"task unsure\"><p><input type=checkbox disabled>" \
735 substr(p, 7) "</li>\n" _list( block, last );
736 } else if ( p ~ /^<p>\[\/\].*/ ) { return "<li class=\"task partial\"><p><input type=checkbox disabled>" \
737 substr(p, 7) "</li>\n" _list( block, last );
738 } else if ( p ~ /^<p>\[[xX]\].*/ ) { return "<li class=\"task done\"><p><input type=checkbox disabled checked>" \
739 substr(p, 7) "</li>\n" _list( block, last );
740 } else { return "<li>" p "</li>\n" _list( block, last ); }
745 file = ""; rl_href[""] = ""; rl_title[""] = "";
746 if (ENVIRON["MD_HTML"] == "true") { AllowHTML = "true"; }
747 HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
748 # hls = "0 0 0 0 0 0";
750 # Buffering of full file ist necessary, e.g. to find reference links
751 while (getline) { file = file $0 "\n"; }
752 # Clean up MS-DOS line breaks
753 gsub(/\r\n/, "\n", file);
755 # Fill array of reference links
757 re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
758 # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
759 while ( match(f, re_reflink ) ) {
760 rl_id = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
761 rl_href[rl_id] = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
762 rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
763 f = substr(f, RSTART + RLENGTH);
764 rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
765 if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
767 # Clear reflinks from File
768 while( gsub(re_reflink, "\n", file ) );
769 # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
771 # Run Block Processing -> The Actual Markdown!
772 printf "%s", _nblock( file );