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
9 # - HTML processing / escaping (according to environment flag)
10 # - em-dashes and arrows
12 # Supported Features / TODO:
13 # ==========================
14 # [x] done [ ] todo [-] not planned ? unsure
16 # Basic Markdown - Block elements:
17 # -------------------------------
19 # - [x] Double space line breaks
20 # - [x] Proper block element nesting
22 # - [x] ATX-Style Headings
24 # - [x] Lists (ordered, unordered)
25 # - [x] Code blocks (using indention)
26 # - [x] Horizontal rules
27 # - [x] Verbatim HTML block (gfm)
29 # Basic Markdown - Inline elements:
30 # ---------------------------------
32 # - [x] Reference style links
33 # - [x] Emphasis *em*/**strong** (*Asterisk*, _Underscore_)
34 # - [x] `code`, also ``code containing `backticks` ``
35 # - [x] Images / reference style images
36 # - [x] <automatic links>
37 # - [x] backslash escapes
38 # - [x] Verbatim HTML inline
41 # Extensions - Block elements:
42 # ----------------------------
43 # - ? Heading identifiers (php md, pandoc)
44 # - [x] Fenced code blocks (php md, pandoc)
45 # - [-] Fenced code attributes
47 # - ? Simple table (pandoc)
48 # - ? Multiline table (pandoc)
49 # - ? Grid table (pandoc)
50 # - ? Pipe table (php md pandoc)
51 # - [x] Line blocks (pandoc)
52 # - [ ] Task lists (pandoc)
53 # - [ ] Definition lists (php md, pandoc)
54 # - [-] Numbered example lists (pandoc)
55 # - [-] Metadata blocks (pandoc)
56 # - [-] Fenced Divs (pandoc)
58 # Extensions - Inline elements:
59 # ----------------------------
60 # - [x] Ignore embedded_underscores (php md, pandoc)
61 # - [x] ~~strikeout~~ (pandoc)
62 # - [x] ^Superscript^ ~Subscript~ (pandoc)
63 # - [-] Bracketed spans (pandoc)
64 # - [-] Inline attributes (pandoc)
65 # - [-] TEX-Math (pandoc)
66 # - ? Footnotes (php md)
67 # - ? Abbreviations (php md)
68 # - ? "Curly quotes" (smartypants)
69 # - [ ] em-dashes (--) (smartypants old)
70 # - ? ... three-dot ellipsis (smartypants)
71 # - [-] en-dash (smartypants)
72 # - [ ] Automatic em-dash / en-dash
73 # - [ ] Automatic -> Arrows <-
75 function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; }
77 function HTML ( text ) {
78 gsub( /&/, "\\&", text );
79 gsub( /</, "\\<", text );
80 gsub( />/, "\\>", text );
81 gsub( /"/, "\\"", text );
82 gsub( /'/, "\\'", text );
83 gsub( /\\/, "\\\", text );
87 function inline( line, LOCAL, len, code, href, guard ) {
88 nu = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\_]|_[[:alnum:]])*" # not underline (except when escaped)
89 na = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\\\*])*" # not asterisk (except when escaped)
90 ieu = "_([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])_" # inner <em> (underline)
91 isu = "__([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])__" # inner <strong> (underline)
92 iea = "\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*" # inner <em> (asterisk)
93 isa = "\\*\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*\\*" # inner <strong> (asterisk)
95 if ( line ~ /^$/ ) { # Recursion End
98 # omit processing of escaped characters
99 } else if ( line ~ /^\\[]\\`\*_\{\}\(\)#\+-\.![]/) {
100 return substr(line, 2, 1) inline( substr(line, 3) );
103 } else if ( match(line, /^ \n/) ) {
104 return "<br />\n" inline( substr(line, RLENGTH + 1) );
106 # Verbatim inline HTML
107 } else if ( 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:]]*\/?>)/) ) {
109 return substr( line, 1, len) inline(substr(line, len + 1));
112 } else if ( match( line, /^`+/) ) {
114 guard = substr( line, 1, len )
115 if ( match(line, guard ".*" guard) ) {
116 code = substr( line, len + 1, match( substr(line, len + 1), guard ) - 1)
117 len = 2 * length(guard) + length(code)
118 # strip single surrounding white spaces
119 code = gensub( / (.*) /, "\\1", "1" , code)
120 # escape HTML within code span
121 gsub( /&/, "\\&", code ); gsub( /</, "\\<", code ); gsub( />/, "\\>", code );
122 return "<code>" code "</code>" inline( substr( line, len + 1 ) )
125 # quick links ("automatic links" in md doc)
126 } else if ( match( line, /^<[a-zA-Z]+:\/\/([-\.[:alnum:]]+)(:[0-9]*)?(\/[^>]*)?>/ ) ) {
128 href = HTML( substr( line, 2, len - 2) );
129 return "<a href=\"" href "\">" href "</a>" inline( substr( line, len + 1) );
132 } else if ( match(line, /^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/) ) {
134 text = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\1", "g", line);
135 href = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\2", "g", line);
136 title = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\4", "g", line);
138 return "<a href=\"" HTML(href) "\" title=\"" HTML(title) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
140 return "<a href=\"" HTML(href) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
143 # reference style links
144 } else if ( match(line, /^\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
146 text = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, line);
147 id = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, line);
148 if ( ! id ) id = text;
149 if ( rl_href[id] && rl_title[id] ) {
150 return "<a href=\"" rl_href[id] "\" title=\"" rl_title[id] "\">" inline(text) "</a>" inline( substr( line, len + 1) );
151 } else if ( rl_href[id] ) {
152 return "<a href=\"" rl_href[id] "\">" inline(text) "</a>" inline( substr( line, len + 1) );
154 return "" substr(line, 1, len) inline( substr(line, len + 1) );
158 } else if ( match(line, /^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/) ) {
160 text = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\1", "g", line);
161 href = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\2", "g", line);
162 title = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\4", "g", line);
164 return "<img src=\"" HTML(href) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\" />" inline( substr( line, len + 1) );
166 return "<img src=\"" HTML(href) "\" alt=\"" HTML(text) "\" />" inline( substr( line, len + 1) );
169 # reference style images
170 } else if ( match(line, /^!\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
172 text = gensub(/^!\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, line);
173 id = gensub(/^!\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, line);
174 if ( ! id ) id = text;
175 if ( rl_href[id] && rl_title[id] ) {
176 return "<img src=\"" rl_href[id] "\" alt=\"" HTML(text) "\" title=\"" rl_title[id] "\" />" inline( substr( line, len + 1) );
177 } else if ( rl_href[id] ) {
178 return "<img src=\"" rl_href[id] "\" alt=\"" HTML(text) "\" />" inline( substr( line, len + 1) );
180 return "" substr(line, 1, len) inline( substr(line, len + 1) );
183 # ~~strikeout~~ (pandoc)
184 } else if ( match(line, /^~~([[:graph:]]|[[:graph:]]([^~]|~[^~])*[[:graph:]])~~/) ) {
186 return "<del>" inline( substr( line, 3, len - 4 ) ) "</del>" inline( substr( line, len + 1 ) );
188 # ^superscript^ (pandoc)
189 } else if ( match(line, /^\^([^[:space:]^]|\\[ ^])+\^/) ) {
191 return "<sup>" inline( substr( line, 2, len - 2 ) ) "</sup>" inline( substr( line, len + 1 ) );
193 # ~subscript~ (pandoc)
194 } else if ( match(line, /^~([^[:space:]~]|\\[ ~])+~/) ) {
196 return "<sub>" inline( substr( line, 2, len - 2 ) ) "</sub>" inline( substr( line, len + 1 ) );
198 # ignore embedded underscores (pandoc, php md)
199 } else if ( match(line, "^[[:alnum:]](__|_)") ) {
200 return substr( line, 1, RLENGTH) inline( substr(line, RLENGTH + 1) );
203 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__$") ) {
205 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
208 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__[[:space:][:punct:]]") ) {
210 return "<strong>" inline( substr( line, 3, len - 5 ) ) "</strong>" inline( substr( line, len) );
213 } else if ( match(line, "^\\*\\*(([^\\*[:space:]]|" iea ")|([^\\*[:space:]]|" iea ")(" na "|" iea ")*([^\\*[:space:]]|" iea "))\\*\\*") ) {
215 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
218 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_$") ) {
220 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
223 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_[[:space:][:punct:]]") ) {
225 return "<em>" inline( substr( line, 2, len - 3 ) ) "</em>" inline( substr( line, len ) );
228 } else if ( match(line, "^\\*(([^\\*[:space:]]|" isa ")|([^\\*[:space:]]|" isa ")(" na "|" isa ")*([^\\*[:space:]]|" isa "))\\*") ) {
230 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
232 # continue walk over string
234 return substr(line, 1, 1) inline( substr(line, 2) );
238 function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent ) {
239 gsub( /^\n+|\n+$/, "", block );
245 } else if ( match( block, /(^|\n) ? ? ?(<!--([^-]|-[^-]|--[^>])*(-->|$)|<\?([^\?]|\?[^>])*(\?>|$)|<![A-Z][^>]*(>|$)|<!\[CDATA\[([^\]]|\][^\]]|\]\][^>])*(\]\]>|$))/) ) {
246 len = RLENGTH; st = RSTART;
247 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
250 } else if ( 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|$)/) ) {
251 len = RLENGTH; st = RSTART;
252 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
255 } else if ( match( tolower(block), /(^|\n) ? ? ?<(script|pre|style)([[:space:]\n>]).*(<\/script>|<\/pre>|<\/style>|$)/) ) {
256 len = RLENGTH; st = RSTART;
257 match( tolower(substr(block, st, len)), /(<\/script>|<\/pre>|<\/style>)/);
258 len = RSTART + RLENGTH;
259 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
262 } else if ( 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|$)/) ) {
263 len = RLENGTH; st = RSTART;
264 return substr(block, st, len) _block(substr(block, st + len));
267 } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
268 len = RLENGTH; st = RSTART;
269 return _block(substr(block, 1, st - 1)) "<hr />\n" _block(substr(block, st + len));
271 # Blockquote (leading >)
272 } else if ( match( block, /^> /) ) {
273 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/);
274 len = RLENGTH; st = RSTART;
275 return "<blockquote>\n" _block( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) "</blockquote>\n\n" \
276 _block( substr(block, st + len) );
278 # Line Blocks (pandoc)
279 } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
280 len = RLENGTH; st = RSTART;
281 code = substr(block, 1, len);
282 gsub(/\n[[:space:]]+/, " ", code);
283 gsub(/\n\| /, "\n", code);
284 gsub(/^\| |\n$/, "", code);
285 return "<div class=\"line-block\">" gensub(/\n/, "<br />\n", "g", inline( code )) "</div>\n" \
286 _block( substr( block, len + 1) );
288 # Indented Code Block
289 } else if ( match(block, /^( |\t)[^\n]+(\n|$)(( |\t)[^\n]+(\n|$)|[ \t]*(\n|$))*/) ) {
290 len = RLENGTH; st = RSTART;
291 code = substr(block, 1, len);
292 gsub(/(^|\n)( |\t)/, "\n", code);
293 gsub(/^\n|\n+$/, "", code);
294 return "<pre><code>" HTML( code ) "</code></pre>\n" \
295 _block( substr( block, len + 1 ) );
297 # Fenced Code Block (pandoc)
298 } else if ( match( block, /^(~~~+|```+)/ ) ) {
299 guard = substr( block, 1, RLENGTH );
300 code = gensub(/^[^\n]+\n/, "", 1, block);
301 if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
302 len = RLENGTH; st = RSTART;
303 return "<pre><code>" HTML( substr(code, 1, st - 1) ) "</code></pre>\n" \
304 _block( substr( code, st + len ) );
306 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
307 len = RLENGTH; st = RSTART;
308 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
309 _block( substr(block, st + len) );
313 } else if ( match( block, "^ ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
314 "(([ \t]*\n)* ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
315 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
316 "|[^\n]+(\n|$))*" ) ) {
317 list = substr( block, 1, RLENGTH);
318 block = substr( block, RLENGTH + 1);
319 indent = length( gensub(/[-+*][ \t]+[^\n]+.*$/, "", 1, list) );
321 gsub("(^|\n) {0," indent "}", "\n", list);
322 return "\n<ul>\n" _list( substr(list, 2) ) "</ul>\n" _block( block );
325 } else if ( match( block, "^ ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
326 "(([ \t]*\n)* ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
327 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
328 "|[^\n]+(\n|$))*" ) ) {
329 list = substr( block, 1, RLENGTH);
330 block = substr( block, RLENGTH + 1);
331 indent = length( gensub(/([0-9]+|#)\.[ \t]+[^\n]+.*$/, "", 1, list) );
333 gsub("(^|\n) {0," indent "}", "\n", list);
334 return "\n<ol>\n" _list( substr(list, 2) ) "</ol>\n" _block( block );
336 # First Order Heading
337 } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
339 return "<h1>" inline( gensub( /\n.*$/, "", "g", block ) ) "</h1>\n\n" \
340 _block( substr( block, len + 1 ) );
342 # Second Order Heading
343 } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
345 return "<h2>" inline( gensub( /\n.*$/, "", "g", block ) ) "</h2>\n\n" \
346 _block( substr( block, len + 1) );
349 } else if ( match( block, /^#{1,6}[[:space:]]*[^\n]+([[:space:]]*#*)(\n|$)/ ) ) {
351 hlvl = length( gensub( /^(#{1,6}).*$/, "\\1", "g", block ) );
352 htxt = gensub( /[[:space:]]*#*$/, "", "1", gensub( /^#{1,6}[[:space:]]*([^\n]+)([[:space:]]*#*)\n.*$/, "\\1", "g", block ) )
353 return "<h" hlvl ">" inline( htxt ) "</h" hlvl ">\n\n" \
354 _block( substr( block, len + 1) );
358 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
359 len = RLENGTH; st = RSTART;
360 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
361 _block( substr(block, st + len) );
365 function _list( block, last, LOCAL, p) {
366 if ( ! length(block) ) return "";
367 gsub(/^([-+*]|[0-9]+\.|#\.)( ? ? ?|\t)/, "", block)
369 # slice next list item from input
370 if ( match( block, /\n([-+*]|[0-9]+\.|#\.)[ \t]+[^\n]+/) ) {
371 p = substr( block, 1, RSTART);
372 block = substr( block, RSTART + 1);
374 p = block; block = "";
376 sub( /\n +([-+*]|[0-9]+\.|#\.)/, "\n&", p );
378 # if this should be a paragraph item
379 # either previous item (last) or current item (p) contains blank lines
380 if (match(last, /\n[[:space:]]*\n/) || match(p, /\n[[:space:]]*\n/) ) {
381 last = p; p = _block(p);
383 last = p; p = _block(p);
384 sub( /^<p>/, "", p );
385 sub( /<\/p>\n/, "", p );
388 return "<li>" p "</li>\n" _list( block, last );
393 file = ""; rl_href[""] = ""; rl_title[""] = "";
395 # Buffering of full file ist necessary, e.g. to find reference links
396 while (getline) { file = file $0 "\n"; }
398 # Fill array of reference links
400 re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
401 # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
402 while ( match(f, re_reflink ) ) {
403 rl_id = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
404 rl_href[rl_id] = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
405 rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
406 f = substr(f, RSTART + RLENGTH);
407 rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
408 if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
410 # Clear reflinks from File
411 while( gsub(re_reflink, "\n", file ) );
412 # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
414 # Run Block Processing -> The Actual Markdown!
415 printf "%s", _block( file );