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 # - [ ] Horizontal rules
28 # Basic Markdown - Inline elements:
29 # ---------------------------------
31 # - [x] Reference style links
32 # - [x] Emphasis *em*/**strong** (*Asterisk*, _Underscore_)
33 # - [x] `code`, also ``code containing `backticks` ``
34 # - [ ] Images / reference style images
35 # - [x] <automatic links>
36 # - [x] backslash escapes
38 # Extensions - Block elements:
39 # ----------------------------
40 # - ? Heading identifiers (php md, pandoc)
41 # - [x] Fenced code blocks (php md, pandoc)
42 # - [-] Fenced code attributes
44 # - ? Simple table (pandoc)
45 # - ? Multiline table (pandoc)
46 # - ? Grid table (pandoc)
47 # - ? Pipe table (php md pandoc)
48 # - [x] Line blocks (pandoc)
49 # - [ ] Task lists (pandoc)
50 # - [ ] Definition lists (php md, pandoc)
51 # - [-] Numbered example lists (pandoc)
52 # - [-] Metadata blocks (pandoc)
53 # - [-] Fenced Divs (pandoc)
55 # Extensions - Inline elements:
56 # ----------------------------
57 # - [x] Ignore embedded_underscores (php md, pandoc)
58 # - [x] ~~strikeout~~ (pandoc)
59 # - [x] ^Superscript^ ~Subscript~ (pandoc)
60 # - [-] Bracketed spans (pandoc)
61 # - [-] Inline attributes (pandoc)
62 # - [-] TEX-Math (pandoc)
63 # - ? Footnotes (php md)
64 # - ? Abbreviations (php md)
65 # - ? "Curly quotes" (smartypants)
66 # - [ ] em-dashes (--) (smartypants old)
67 # - ? ... three-dot ellipsis (smartypants)
68 # - [-] en-dash (smartypants)
69 # - [ ] Automatic em-dash / en-dash
70 # - [ ] Automatic -> Arrows <-
72 function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; }
74 function HTML ( text ) {
75 gsub( /&/, "\\&", text );
76 gsub( /</, "\\<", text );
77 gsub( />/, "\\>", text );
78 gsub( /"/, "\\"", text );
79 gsub( /'/, "\\'", text );
80 gsub( /\\/, "\\\", text );
84 function inline( line, LOCAL, len, code, href, guard ) {
85 nu = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\_]|_[[:alnum:]])*" # not underline (except when escaped)
86 na = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\\\*])*" # not asterisk (except when escaped)
87 ieu = "_([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])_" # inner <em> (underline)
88 isu = "__([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])__" # inner <strong> (underline)
89 iea = "\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*" # inner <em> (asterisk)
90 isa = "\\*\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*\\*" # inner <strong> (asterisk)
92 if ( line ~ /^$/ ) { # Recursion End
95 # omit processing of escaped characters
96 } else if ( line ~ /^\\[]\\`\*_\{\}\(\)#\+-\.![]/) {
97 return substr(line, 2, 1) inline( substr(line, 3) );
100 } else if ( match(line, /^ \n/) ) {
101 return "<br />\n" inline( substr(line, RLENGTH + 1) );
104 } else if ( match( line, /^`+/) ) {
106 guard = substr( line, 1, len )
107 if ( match(line, guard ".*" guard) ) {
108 code = substr( line, len + 1, match( substr(line, len + 1), guard ) - 1)
109 len = 2 * length(guard) + length(code)
110 # strip single surrounding white spaces
111 code = gensub( / (.*) /, "\\1", "1" , code)
112 # escape HTML within code span
113 gsub( /&/, "\\&", code ); gsub( /</, "\\<", code ); gsub( />/, "\\>", code );
114 return "<code>" code "</code>" inline( substr( line, len + 1 ) )
117 # quick links ("automatic links" in md doc)
118 } else if ( match( line, /^<[a-zA-Z]+:\/\/([-\.[:alnum:]]+)(:[0-9]*)?(\/[^>]*)?>/ ) ) {
120 href = HTML( substr( line, 2, len - 2) );
121 return "<a href=\"" href "\">" href "</a>" inline( substr( line, len + 1) );
124 } else if ( match(line, /^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/) ) {
126 text = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\1", "g", line);
127 href = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\2", "g", line);
128 title = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\4", "g", line);
130 return "<a href=\"" HTML(href) "\" title=\"" HTML(title) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
132 return "<a href=\"" HTML(href) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
135 # reference style links
136 } else if ( match(line, /^\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
138 text = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, line);
139 id = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, line);
140 if ( ! id ) id = text;
141 if ( rl_href[id] && rl_title[id] ) {
142 return "<a href=\"" rl_href[id] "\" title=\"" rl_title[id] "\">" inline(text) "</a>" inline( substr( line, len + 1) );
143 } else if ( rl_href[id] ) {
144 return "<a href=\"" rl_href[id] "\">" inline(text) "</a>" inline( substr( line, len + 1) );
146 return "" substr(line, 1, len) inline( substr(line, len + 1) );
149 # ~~strikeout~~ (pandoc)
150 } else if ( match(line, /^~~([[:graph:]]|[[:graph:]]([^~]|~[^~])*[[:graph:]])~~/) ) {
152 return "<del>" inline( substr( line, 3, len - 4 ) ) "</del>" inline( substr( line, len + 1 ) );
154 # ^superscript^ (pandoc)
155 } else if ( match(line, /^\^([^[:space:]^]|\\[ ^])+\^/) ) {
157 return "<sup>" inline( substr( line, 2, len - 2 ) ) "</sup>" inline( substr( line, len + 1 ) );
159 # ~subscript~ (pandoc)
160 } else if ( match(line, /^~([^[:space:]~]|\\[ ~])+~/) ) {
162 return "<sub>" inline( substr( line, 2, len - 2 ) ) "</sub>" inline( substr( line, len + 1 ) );
164 # ignore embedded underscores (pandoc, php md)
165 } else if ( match(line, "^[[:alnum:]](__|_)") ) {
166 return substr( line, 1, RLENGTH) inline( substr(line, RLENGTH + 1) );
169 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__$") ) {
171 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
174 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__[[:space:]]") ) {
176 return "<strong>" inline( substr( line, 3, len - 5 ) ) "</strong>" inline( substr( line, len) );
179 } else if ( match(line, "^\\*\\*(([^\\*[:space:]]|" iea ")|([^\\*[:space:]]|" iea ")(" na "|" iea ")*([^\\*[:space:]]|" iea "))\\*\\*") ) {
181 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
184 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_$") ) {
186 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
189 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_[[:space:]]") ) {
191 return "<em>" inline( substr( line, 2, len - 3 ) ) "</em>" inline( substr( line, len ) );
194 } else if ( match(line, "^\\*(([^\\*[:space:]]|" isa ")|([^\\*[:space:]]|" isa ")(" na "|" isa ")*([^\\*[:space:]]|" isa "))\\*") ) {
196 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
198 # continue walk over string
200 return substr(line, 1, 1) inline( substr(line, 2) );
204 function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent ) {
205 gsub( /^\n+|\n+$/, "", block );
210 # Blockquote (leading >)
211 } else if ( match( block, /^> /) ) {
212 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/);
213 len = RLENGTH; st = RSTART;
214 return "<blockquote>\n" _block( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) "</blockquote>\n\n" \
215 _block( substr(block, st + len) );
217 # Line Blocks (pandoc)
218 } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
219 len = RLENGTH; st = RSTART;
220 code = substr(block, 1, len);
221 gsub(/\n[[:space:]]+/, " ", code);
222 gsub(/\n\| /, "\n", code);
223 gsub(/^\| |\n$/, "", code);
224 return "<div class=\"line-block\">" gensub(/\n/, "<br />\n", "g", inline( code )) "</div>\n" \
225 _block( substr( block, len + 1) );
227 # Indented Code Block
228 } else if ( match(block, /^( |\t)[^\n]+(\n|$)(( |\t)[^\n]+(\n|$)|[ \t]*(\n|$))*/) ) {
229 len = RLENGTH; st = RSTART;
230 code = substr(block, 1, len);
231 gsub(/(^|\n)( |\t)/, "\n", code);
232 gsub(/^\n|\n+$/, "", code);
233 return "<pre><code>" HTML( code ) "</code></pre>\n" \
234 _block( substr( block, len + 1 ) );
236 # Fenced Code Block (pandoc)
237 } else if ( match( block, /^(~~~+|```+)/ ) ) {
238 guard = substr( block, 1, RLENGTH );
239 code = gensub(/^[^\n]+\n/, "", 1, block);
240 if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
241 len = RLENGTH; st = RSTART;
242 return "<pre><code>" HTML( substr(code, 1, st - 1) ) "</code></pre>\n" \
243 _block( substr( code, st + len ) );
245 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
246 len = RLENGTH; st = RSTART;
247 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
248 _block( substr(block, st + len) );
252 } else if ( match( block, "^ ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
253 "(([ \t]*\n)* ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
254 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
255 "|[^\n]+(\n|$))*" ) ) {
256 list = substr( block, 1, RLENGTH);
257 block = substr( block, RLENGTH + 1);
258 indent = length( gensub(/[-+*][ \t]+[^\n]+.*$/, "", 1, list) );
260 gsub("(^|\n) {0," indent "}", "\n", list);
261 return "\n<ul>\n" _list( substr(list, 2) ) "</ul>\n" _block( block );
264 } else if ( match( block, "^ ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
265 "(([ \t]*\n)* ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
266 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
267 "|[^\n]+(\n|$))*" ) ) {
268 list = substr( block, 1, RLENGTH);
269 block = substr( block, RLENGTH + 1);
270 indent = length( gensub(/([0-9]+|#)\.[ \t]+[^\n]+.*$/, "", 1, list) );
272 gsub("(^|\n) {0," indent "}", "\n", list);
273 return "\n<ol>\n" _list( substr(list, 2) ) "</ol>\n" _block( block );
275 # First Order Heading
276 } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
278 return "<h1>" inline( gensub( /\n.*$/, "", "g", block ) ) "</h1>\n\n" \
279 _block( substr( block, len + 1 ) );
281 # Second Order Heading
282 } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
284 return "<h2>" inline( gensub( /\n.*$/, "", "g", block ) ) "</h2>\n\n" \
285 _block( substr( block, len + 1) );
288 } else if ( match( block, /^#{1,6}[[:space:]]*[^\n]+([[:space:]]*#*)(\n|$)/ ) ) {
290 hlvl = length( gensub( /^(#{1,6}).*$/, "\\1", "g", block ) );
291 htxt = gensub( /[[:space:]]*#*$/, "", "1", gensub( /^#{1,6}[[:space:]]*([^\n]+)([[:space:]]*#*)\n.*$/, "\\1", "g", block ) )
292 return "<h" hlvl ">" inline( htxt ) "</h" hlvl ">\n\n" \
293 _block( substr( block, len + 1) );
297 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
298 len = RLENGTH; st = RSTART;
299 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
300 _block( substr(block, st + len) );
304 function _list( block, last, LOCAL, p) {
305 if ( ! length(block) ) return "";
306 gsub(/^([-+*]|[0-9]+\.|#\.)( ? ? ?|\t)/, "", block)
308 # slice next list item from input
309 if ( match( block, /\n([-+*]|[0-9]+\.|#\.)[ \t]+[^\n]+/) ) {
310 p = substr( block, 1, RSTART);
311 block = substr( block, RSTART + 1);
313 p = block; block = "";
315 sub( /\n +([-+*]|[0-9]+\.|#\.)/, "\n&", p );
317 # if this should be a paragraph item
318 # either previous item (last) or current item (p) contains blank lines
319 if (match(last, /\n[[:space:]]*\n/) || match(p, /\n[[:space:]]*\n/) ) {
320 last = p; p = _block(p);
322 last = p; p = _block(p);
323 sub( /^<p>/, "", p );
324 sub( /<\/p>\n/, "", p );
327 return "<li>" p "</li>\n" _list( block, last );
332 file = ""; rl_href[""] = ""; rl_title[""] = "";
334 # Buffering of full file ist necessary, e.g. to find reference links
335 while (getline) { file = file $0 "\n"; }
337 # Fill array of reference links
339 re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
340 # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
341 while ( match(f, re_reflink ) ) {
342 rl_id = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
343 rl_href[rl_id] = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
344 rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
345 f = substr(f, RSTART + RLENGTH);
346 rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
347 if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
349 # Clear reflinks from File
350 while( gsub(re_reflink, "\n", file ) );
351 # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
353 # Run Block Processing -> The Actual Markdown!
354 printf "%s", _block( file );