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 # - [ ] Verbatim HTML inline
40 # Extensions - Block elements:
41 # ----------------------------
42 # - ? Heading identifiers (php md, pandoc)
43 # - [x] Fenced code blocks (php md, pandoc)
44 # - [-] Fenced code attributes
46 # - ? Simple table (pandoc)
47 # - ? Multiline table (pandoc)
48 # - ? Grid table (pandoc)
49 # - ? Pipe table (php md pandoc)
50 # - [x] Line blocks (pandoc)
51 # - [ ] Task lists (pandoc)
52 # - [ ] Definition lists (php md, pandoc)
53 # - [-] Numbered example lists (pandoc)
54 # - [-] Metadata blocks (pandoc)
55 # - [-] Fenced Divs (pandoc)
57 # Extensions - Inline elements:
58 # ----------------------------
59 # - [x] Ignore embedded_underscores (php md, pandoc)
60 # - [x] ~~strikeout~~ (pandoc)
61 # - [x] ^Superscript^ ~Subscript~ (pandoc)
62 # - [-] Bracketed spans (pandoc)
63 # - [-] Inline attributes (pandoc)
64 # - [-] TEX-Math (pandoc)
65 # - ? Footnotes (php md)
66 # - ? Abbreviations (php md)
67 # - ? "Curly quotes" (smartypants)
68 # - [ ] em-dashes (--) (smartypants old)
69 # - ? ... three-dot ellipsis (smartypants)
70 # - [-] en-dash (smartypants)
71 # - [ ] Automatic em-dash / en-dash
72 # - [ ] Automatic -> Arrows <-
74 function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; }
76 function HTML ( text ) {
77 gsub( /&/, "\\&", text );
78 gsub( /</, "\\<", text );
79 gsub( />/, "\\>", text );
80 gsub( /"/, "\\"", text );
81 gsub( /'/, "\\'", text );
82 gsub( /\\/, "\\\", text );
86 function inline( line, LOCAL, len, code, href, guard ) {
87 nu = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\_]|_[[:alnum:]])*" # not underline (except when escaped)
88 na = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\\\*])*" # not asterisk (except when escaped)
89 ieu = "_([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])_" # inner <em> (underline)
90 isu = "__([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])__" # inner <strong> (underline)
91 iea = "\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*" # inner <em> (asterisk)
92 isa = "\\*\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*\\*" # inner <strong> (asterisk)
94 if ( line ~ /^$/ ) { # Recursion End
97 # omit processing of escaped characters
98 } else if ( line ~ /^\\[]\\`\*_\{\}\(\)#\+-\.![]/) {
99 return substr(line, 2, 1) inline( substr(line, 3) );
102 } else if ( match(line, /^ \n/) ) {
103 return "<br />\n" inline( substr(line, RLENGTH + 1) );
106 } else if ( match( line, /^`+/) ) {
108 guard = substr( line, 1, len )
109 if ( match(line, guard ".*" guard) ) {
110 code = substr( line, len + 1, match( substr(line, len + 1), guard ) - 1)
111 len = 2 * length(guard) + length(code)
112 # strip single surrounding white spaces
113 code = gensub( / (.*) /, "\\1", "1" , code)
114 # escape HTML within code span
115 gsub( /&/, "\\&", code ); gsub( /</, "\\<", code ); gsub( />/, "\\>", code );
116 return "<code>" code "</code>" inline( substr( line, len + 1 ) )
119 # quick links ("automatic links" in md doc)
120 } else if ( match( line, /^<[a-zA-Z]+:\/\/([-\.[:alnum:]]+)(:[0-9]*)?(\/[^>]*)?>/ ) ) {
122 href = HTML( substr( line, 2, len - 2) );
123 return "<a href=\"" href "\">" href "</a>" inline( substr( line, len + 1) );
126 } else if ( match(line, /^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/) ) {
128 text = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\1", "g", line);
129 href = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\2", "g", line);
130 title = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\4", "g", line);
132 return "<a href=\"" HTML(href) "\" title=\"" HTML(title) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
134 return "<a href=\"" HTML(href) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
137 # reference style links
138 } else if ( match(line, /^\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
140 text = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, line);
141 id = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, line);
142 if ( ! id ) id = text;
143 if ( rl_href[id] && rl_title[id] ) {
144 return "<a href=\"" rl_href[id] "\" title=\"" rl_title[id] "\">" inline(text) "</a>" inline( substr( line, len + 1) );
145 } else if ( rl_href[id] ) {
146 return "<a href=\"" rl_href[id] "\">" inline(text) "</a>" inline( substr( line, len + 1) );
148 return "" substr(line, 1, len) inline( substr(line, len + 1) );
152 } else if ( match(line, /^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/) ) {
154 text = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\1", "g", line);
155 href = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\2", "g", line);
156 title = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\4", "g", line);
158 return "<img src=\"" HTML(href) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\" />" inline( substr( line, len + 1) );
160 return "<img src=\"" HTML(href) "\" alt=\"" HTML(text) "\" />" inline( substr( line, len + 1) );
163 # reference style images
164 } else if ( match(line, /^!\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
166 text = gensub(/^!\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, line);
167 id = gensub(/^!\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, line);
168 if ( ! id ) id = text;
169 if ( rl_href[id] && rl_title[id] ) {
170 return "<img src=\"" rl_href[id] "\" alt=\"" HTML(text) "\" title=\"" rl_title[id] "\" />" inline( substr( line, len + 1) );
171 } else if ( rl_href[id] ) {
172 return "<img src=\"" rl_href[id] "\" alt=\"" HTML(text) "\" />" inline( substr( line, len + 1) );
174 return "" substr(line, 1, len) inline( substr(line, len + 1) );
177 # ~~strikeout~~ (pandoc)
178 } else if ( match(line, /^~~([[:graph:]]|[[:graph:]]([^~]|~[^~])*[[:graph:]])~~/) ) {
180 return "<del>" inline( substr( line, 3, len - 4 ) ) "</del>" inline( substr( line, len + 1 ) );
182 # ^superscript^ (pandoc)
183 } else if ( match(line, /^\^([^[:space:]^]|\\[ ^])+\^/) ) {
185 return "<sup>" inline( substr( line, 2, len - 2 ) ) "</sup>" inline( substr( line, len + 1 ) );
187 # ~subscript~ (pandoc)
188 } else if ( match(line, /^~([^[:space:]~]|\\[ ~])+~/) ) {
190 return "<sub>" inline( substr( line, 2, len - 2 ) ) "</sub>" inline( substr( line, len + 1 ) );
192 # ignore embedded underscores (pandoc, php md)
193 } else if ( match(line, "^[[:alnum:]](__|_)") ) {
194 return substr( line, 1, RLENGTH) inline( substr(line, RLENGTH + 1) );
197 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__$") ) {
199 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
202 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__[[:space:][:punct:]]") ) {
204 return "<strong>" inline( substr( line, 3, len - 5 ) ) "</strong>" inline( substr( line, len) );
207 } else if ( match(line, "^\\*\\*(([^\\*[:space:]]|" iea ")|([^\\*[:space:]]|" iea ")(" na "|" iea ")*([^\\*[:space:]]|" iea "))\\*\\*") ) {
209 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
212 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_$") ) {
214 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
217 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_[[:space:][:punct:]]") ) {
219 return "<em>" inline( substr( line, 2, len - 3 ) ) "</em>" inline( substr( line, len ) );
222 } else if ( match(line, "^\\*(([^\\*[:space:]]|" isa ")|([^\\*[:space:]]|" isa ")(" na "|" isa ")*([^\\*[:space:]]|" isa "))\\*") ) {
224 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
226 # continue walk over string
228 return substr(line, 1, 1) inline( substr(line, 2) );
232 function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent ) {
233 gsub( /^\n+|\n+$/, "", block );
239 } else if ( match( block, /(^|\n) ? ? ?<!--([^-]|-[^-]|--[^>])*(-->|$)/) ) {
240 len = RLENGTH; st = RSTART;
241 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
244 } else if ( match( block, /(^|\n) ? ? ?<\?([^\?]|\?[^>])*(\?>|$)/) ) {
245 len = RLENGTH; st = RSTART;
246 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
249 } else if ( match( block, /(^|\n) ? ? ?<![A-Z][^>]*(>|$)/) ) {
250 len = RLENGTH; st = RSTART;
251 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
254 } else if ( match( block, /(^|\n) ? ? ?<!\[CDATA\[([^\]]|\][^\]]|\]\][^>])*(\]\]>|$)/) ) {
255 len = RLENGTH; st = RSTART;
256 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
259 } 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|$)/) ) {
260 len = RLENGTH; st = RSTART;
261 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
264 } else if ( match( tolower(block), /(^|\n) ? ? ?<(script|pre|style)([[:space:]\n>]).*(<\/script>|<\/pre>|<\/style>|$)/) ) {
265 len = RLENGTH; st = RSTART;
266 match( tolower(substr(block, st, len)), /(<\/script>|<\/pre>|<\/style>)/);
267 len = RSTART + RLENGTH;
268 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
271 } else if ( match( block, /^ ? ? ?(<\/[A-Za-z][A-Za-z0-9-]* \t]*>|<[A-Za-z][A-Za-z0-9-]*([ \t]+[A-Za-z_:][A-Za-z0-9_\.:-]*[ \t]*=[ \t]*([[:space:]"'=<>`]+|"[^"]*"|'[^']*'))*[ \t]*\/?>)([[:space:]]*\n)([^\n]|\n[[:space:]]*[^\n])*(\n[ \t]*\n|$)/) ) {
272 len = RLENGTH; st = RSTART;
273 return substr(block, st, len) _block(substr(block, st + len));
276 } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
277 len = RLENGTH; st = RSTART;
278 return _block(substr(block, 1, st - 1)) "<hr />\n" _block(substr(block, st + len));
280 # Blockquote (leading >)
281 } else if ( match( block, /^> /) ) {
282 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/);
283 len = RLENGTH; st = RSTART;
284 return "<blockquote>\n" _block( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) "</blockquote>\n\n" \
285 _block( substr(block, st + len) );
287 # Line Blocks (pandoc)
288 } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
289 len = RLENGTH; st = RSTART;
290 code = substr(block, 1, len);
291 gsub(/\n[[:space:]]+/, " ", code);
292 gsub(/\n\| /, "\n", code);
293 gsub(/^\| |\n$/, "", code);
294 return "<div class=\"line-block\">" gensub(/\n/, "<br />\n", "g", inline( code )) "</div>\n" \
295 _block( substr( block, len + 1) );
297 # Indented Code Block
298 } else if ( match(block, /^( |\t)[^\n]+(\n|$)(( |\t)[^\n]+(\n|$)|[ \t]*(\n|$))*/) ) {
299 len = RLENGTH; st = RSTART;
300 code = substr(block, 1, len);
301 gsub(/(^|\n)( |\t)/, "\n", code);
302 gsub(/^\n|\n+$/, "", code);
303 return "<pre><code>" HTML( code ) "</code></pre>\n" \
304 _block( substr( block, len + 1 ) );
306 # Fenced Code Block (pandoc)
307 } else if ( match( block, /^(~~~+|```+)/ ) ) {
308 guard = substr( block, 1, RLENGTH );
309 code = gensub(/^[^\n]+\n/, "", 1, block);
310 if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
311 len = RLENGTH; st = RSTART;
312 return "<pre><code>" HTML( substr(code, 1, st - 1) ) "</code></pre>\n" \
313 _block( substr( code, st + len ) );
315 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
316 len = RLENGTH; st = RSTART;
317 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
318 _block( substr(block, st + len) );
322 } else if ( match( block, "^ ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
323 "(([ \t]*\n)* ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
324 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
325 "|[^\n]+(\n|$))*" ) ) {
326 list = substr( block, 1, RLENGTH);
327 block = substr( block, RLENGTH + 1);
328 indent = length( gensub(/[-+*][ \t]+[^\n]+.*$/, "", 1, list) );
330 gsub("(^|\n) {0," indent "}", "\n", list);
331 return "\n<ul>\n" _list( substr(list, 2) ) "</ul>\n" _block( block );
334 } else if ( match( block, "^ ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
335 "(([ \t]*\n)* ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
336 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
337 "|[^\n]+(\n|$))*" ) ) {
338 list = substr( block, 1, RLENGTH);
339 block = substr( block, RLENGTH + 1);
340 indent = length( gensub(/([0-9]+|#)\.[ \t]+[^\n]+.*$/, "", 1, list) );
342 gsub("(^|\n) {0," indent "}", "\n", list);
343 return "\n<ol>\n" _list( substr(list, 2) ) "</ol>\n" _block( block );
345 # First Order Heading
346 } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
348 return "<h1>" inline( gensub( /\n.*$/, "", "g", block ) ) "</h1>\n\n" \
349 _block( substr( block, len + 1 ) );
351 # Second Order Heading
352 } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
354 return "<h2>" inline( gensub( /\n.*$/, "", "g", block ) ) "</h2>\n\n" \
355 _block( substr( block, len + 1) );
358 } else if ( match( block, /^#{1,6}[[:space:]]*[^\n]+([[:space:]]*#*)(\n|$)/ ) ) {
360 hlvl = length( gensub( /^(#{1,6}).*$/, "\\1", "g", block ) );
361 htxt = gensub( /[[:space:]]*#*$/, "", "1", gensub( /^#{1,6}[[:space:]]*([^\n]+)([[:space:]]*#*)\n.*$/, "\\1", "g", block ) )
362 return "<h" hlvl ">" inline( htxt ) "</h" hlvl ">\n\n" \
363 _block( substr( block, len + 1) );
367 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
368 len = RLENGTH; st = RSTART;
369 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
370 _block( substr(block, st + len) );
374 function _list( block, last, LOCAL, p) {
375 if ( ! length(block) ) return "";
376 gsub(/^([-+*]|[0-9]+\.|#\.)( ? ? ?|\t)/, "", block)
378 # slice next list item from input
379 if ( match( block, /\n([-+*]|[0-9]+\.|#\.)[ \t]+[^\n]+/) ) {
380 p = substr( block, 1, RSTART);
381 block = substr( block, RSTART + 1);
383 p = block; block = "";
385 sub( /\n +([-+*]|[0-9]+\.|#\.)/, "\n&", p );
387 # if this should be a paragraph item
388 # either previous item (last) or current item (p) contains blank lines
389 if (match(last, /\n[[:space:]]*\n/) || match(p, /\n[[:space:]]*\n/) ) {
390 last = p; p = _block(p);
392 last = p; p = _block(p);
393 sub( /^<p>/, "", p );
394 sub( /<\/p>\n/, "", p );
397 return "<li>" p "</li>\n" _list( block, last );
402 file = ""; rl_href[""] = ""; rl_title[""] = "";
404 # Buffering of full file ist necessary, e.g. to find reference links
405 while (getline) { file = file $0 "\n"; }
407 # Fill array of reference links
409 re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
410 # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
411 while ( match(f, re_reflink ) ) {
412 rl_id = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
413 rl_href[rl_id] = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
414 rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
415 f = substr(f, RSTART + RLENGTH);
416 rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
417 if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
419 # Clear reflinks from File
420 while( gsub(re_reflink, "\n", file ) );
421 # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
423 # Run Block Processing -> The Actual Markdown!
424 printf "%s", _block( file );