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 # Copyright 2021 - 2023 Paul Hänsch
10 # Permission to use, copy, modify, and/or distribute this software for any
11 # purpose with or without fee is hereby granted, provided that the above
12 # copyright notice and this permission notice appear in all copies.
14 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
20 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 # Supported Features / TODO:
23 # ==========================
24 # [x] done [ ] todo [-] not planned ? unsure
26 # Basic Markdown - Block elements:
27 # -------------------------------
29 # - [x] Double space line breaks
30 # - [x] Proper block element nesting
32 # - [x] ATX-Style Headings
34 # - [x] Lists (ordered, unordered)
35 # - [x] Code blocks (using indention)
36 # - [x] Horizontal rules
37 # - [x] Verbatim HTML block (disabled by default)
39 # Basic Markdown - Inline elements:
40 # ---------------------------------
42 # - [x] Reference style links
43 # - [x] Emphasis *em*/**strong** (*Asterisk*, _Underscore_)
44 # - [x] `code`, also ``code containing `backticks` ``
45 # - [x] Images / reference style images
46 # - [x] <automatic links>
47 # - [x] backslash escapes
48 # - [x] Verbatim HTML inline (disabled by default)
51 # NOTE: Set the environment variable MD_HTML=true to enable verbatim HTML
53 # Extensions - Block elements:
54 # ----------------------------
55 # - [x] Automatic <section>-wrapping (custom)
56 # - ? Heading identifiers (php md, pandoc)
57 # - [x] Heading attributes (custom)
58 # - [ ] <hr> terminates section
59 # - [x] Automatic heading identifiers (custom)
60 # - [x] Fenced code blocks (php md, pandoc)
61 # - [x] Fenced code attributes
62 # - [x] Images (as block elements, <figure>-wrapped) (custom)
63 # - [x] reference style block images
65 # - ? Simple table (pandoc)
66 # - ? Multiline table (pandoc)
67 # - [x] Grid table (pandoc)
69 # - [x] Pipe table (php md, pandoc)
70 # - [x] Line blocks (pandoc)
71 # - [x] Task lists (pandoc, custom)
72 # - [x] Definition lists (php md, pandoc)
73 # - [-] Numbered example lists (pandoc)
74 # - [-] Metadata blocks (pandoc)
75 # - [x] Metadata blocks (custom)
76 # - [x] Fenced Divs (pandoc)
78 # Extensions - Inline elements:
79 # ----------------------------
80 # - [x] Ignore embedded_underscores (php md, pandoc)
81 # - [x] ~~strikeout~~ (pandoc)
82 # - [x] ^Superscript^ ~Subscript~ (pandoc)
83 # - [-] Bracketed spans (pandoc)
84 # - [-] Inline attributes (pandoc)
85 # - [x] Image attributes (custom, pandoc inspired, not for reference style)
86 # - [x] Wiki style links [[PageName]] / [[PageName|Link Text]]
87 # - [-] TEX-Math (pandoc)
88 # - ? Footnotes (php md)
89 # - ? Abbreviations (php md)
90 # - ? "Curly quotes" (smartypants)
91 # - [ ] em-dashes (--) (smartypants old)
92 # - ? ... three-dot ellipsis (smartypants)
93 # - [-] en-dash (smartypants)
94 # - [ ] Automatic em-dash / en-dash
95 # - [x] Automatic -> Arrows <- (custom)
97 function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; }
99 function HTML ( text ) {
100 gsub( /&/, "\\&", text );
101 gsub( /</, "\\<", text );
102 gsub( />/, "\\>", text );
103 gsub( /"/, "\\"", text );
104 gsub( /'/, "\\'", text );
105 gsub( /\\/, "\\\", text );
109 function URL ( text, sharp ) {
110 gsub( /&/, "%26", text );
111 gsub( /"/, "%22", text );
112 gsub( /'/, "%27", text );
113 gsub( /`/, "%60", text );
114 gsub( /\?/, "%3F", text );
115 if (sharp) gsub( /#/, "%23", text );
116 gsub( /\[/, "%5B", text );
117 gsub( /\]/, "%5D", text );
118 gsub( / /, "%20", text );
119 gsub( / /, "%09", text );
120 gsub( /\\/, "%5C", text );
124 function inline( line, LOCAL, len, text, code, href, guard ) {
125 if ( line ~ /^$/ ) { # Recursion End
128 # omit processing of escaped characters
129 } else if ( line ~ /^\\./) {
130 return HTML(substr(line, 2, 1)) inline( substr(line, 3) );
133 } else if ( match(line, /^ \n/) ) {
134 return "<br>\n" inline( substr(line, RLENGTH + 1) );
137 } else if ( match( line, /^`+/) ) {
139 guard = substr( line, 1, len )
140 if ( match(line, guard ".*" guard) ) {
141 code = substr( line, len + 1, match( substr(line, len + 1), guard ) - 1)
142 len = 2 * length(guard) + length(code)
143 # strip single surrounding white spaces
144 gsub( /^ | $/, "", code)
145 # escape HTML within code span
146 gsub( /&/, "\\&", code ); gsub( /</, "\\<", code ); gsub( />/, "\\>", code );
147 return "<code>" code "</code>" inline( substr( line, len + 1 ) )
151 } else if ( match( line, /^<<([^>]|>[^>])+>>/ ) ) {
153 return "<code class=\"macro\">" HTML( substr( line, 3, len - 4 ) ) "</code>" inline(substr(line, len + 1));
156 } else if ( match( line, /^\[\[([^]|]+)(\|[^]]+)?\]\]/) ) {
158 href = gensub(/^\[\[([^]|]+)(\|([^]]+))?\]\]/, "\\1", 1, substr(line, 1, len) );
159 text = gensub(/^\[\[([^]|]+)(\|([^]]+))?\]\]/, "\\3", 1, substr(line, 1, len) );
160 if ( ! text ) text = href;
161 return "<a href=\"" URL(href) "\">" HTML(text) "</a>" inline( substr( line, len + 1) );
163 # quick links ("automatic links" in md doc)
164 } else if ( match( line, /^<[a-zA-Z]+:\/\/([-\.[:alnum:]]+)(:[0-9]*)?(\/[^>]*)?>/ ) ) {
166 href = URL( substr( line, 2, len - 2) );
167 return "<a href=\"" href "\">" href "</a>" inline( substr( line, len + 1) );
170 } 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])?)*>/ ) ) {
172 href = URL( substr( line, 2, len - 2) );
173 return "<a href=\"mailto:" href "\">" href "</a>" inline( substr( line, len + 1) );
175 # Verbatim inline HTML
176 } 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:]]*\/?>)/) ) {
178 return substr( line, 1, len) inline(substr(line, len + 1));
181 } else if ( match(line, "^" lii "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)") ) {
183 text = href = title = substr( line, 1, len);
184 sub("^\\[", "", text); sub("\\]\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)$", "", text);
185 sub("^" lii "\\([\n\t ]*", "", href); sub("([\n\t ]+" lit ")?[\n\t ]*\\)$", "", href);
186 sub("^" lii "\\([\n\t ]*" lid, "", title); sub("[\n\t ]*\\)$", "", title); sub("^[\n\t ]+", "", title);
188 if ( match(href, /^<.*>$/) ) { sub(/^</, "", href); sub(/>$/, "", href); }
189 if ( match(title, /^".*"$/) ) { sub(/^"/, "", title); sub(/"$/, "", title); }
190 else if ( match(title, /^'.*'$/) ) { sub(/^'/, "", title); sub(/'$/, "", title); }
191 else if ( match(title, /^\(.*\)$/) ) { sub(/^\(/, "", title); sub(/\)$/, "", title); }
193 gsub(/\\/, "", href); gsub(/\\/, "", title); gsub(/[\n\t]+/, " ", title);
195 return "<a href=\"" URL(href) "\"" (title?" title=\"" HTML(title) "\"":"") ">" \
196 inline( text ) "</a>" inline( substr( line, len + 1) );
198 # reference style links
199 } else if ( match(line, /^\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
201 text = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\1", 1, substr(line, 1, len) );
202 id = gensub(/^\[([^\n]+)\] ?\[([^\n]*)\].*/, "\\2", 1, substr(line, 1, len) );
203 if ( ! id ) id = text;
204 if ( rl_href[id] && rl_title[id] ) {
205 return "<a href=\"" URL(rl_href[id]) "\" title=\"" HTML(rl_title[id]) "\">" inline(text) "</a>" inline( substr( line, len + 1) );
206 } else if ( rl_href[id] ) {
207 return "<a href=\"" URL(rl_href[id]) "\">" inline(text) "</a>" inline( substr( line, len + 1) );
209 return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
213 } else if ( match(line, "^!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?") ) {
214 len = RLENGTH; text = href = title = attrib = substr( line, 1, len);
216 sub("^!\\[", "", text);
217 sub("\\]\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?$", "", text);
219 sub("^!" lix "\\([\n\t ]*", "", href);
220 sub("([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?$", "", href);
222 sub("^!" lix "\\([\n\t ]*" lid, "", title);
223 sub("[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?$", "", title);
224 sub("^[\n\t ]+", "", title);
226 sub("^!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)", "", attrib);
227 sub(/^\{[ \t]*/, "", attrib); sub(/[ \t]*\}$/, "", attrib); gsub(/[ \t]+/, " ", attrib);
229 if ( match(href, /^<.*>$/) ) { sub(/^</, "", href); sub(/>$/, "", href); }
230 if ( match(title, /^".*"$/) ) { sub(/^"/, "", title); sub(/"$/, "", title); }
231 else if ( match(title, /^'.*'$/) ) { sub(/^'/, "", title); sub(/'$/, "", title); }
232 else if ( match(title, /^\(.*\)$/) ) { sub(/^\(/, "", title); sub(/\)$/, "", title); }
234 gsub(/^[\t ]+$/, "", text); gsub(/\\/, "", href);
235 gsub(/\\/, "", title); gsub(/[\n\t]+/, " ", title);
237 return "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text?text:title?title:href) "\"" \
238 (title?" title=\"" HTML(title) "\"":"") (attrib?" class=\"" HTML(attrib) "\"":"") \
239 ">" inline( substr( line, len + 1) );
241 # reference style images
242 } else if ( match(line, /^!\[([^]]*)\] ?\[([^]]*)\]/ ) ) {
244 text = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\].*/, "\\1", 1, substr(line, 1, len) );
245 id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\].*/, "\\2", 1, substr(line, 1, len) );
246 if ( ! id ) id = text;
247 if ( rl_href[id] && rl_title[id] ) {
248 return "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\" title=\"" HTML(rl_title[id]) "\">" \
249 inline( substr( line, len + 1) );
250 } else if ( rl_href[id] ) {
251 return "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
252 inline( substr( line, len + 1) );
254 return "" HTML(substr(line, 1, len)) inline( substr(line, len + 1) );
257 # ~~strikeout~~ (pandoc)
258 } else if ( match(line, /^~~([[:graph:]]|[[:graph:]]([^~]|~[^~])*[[:graph:]])~~/) ) {
260 return "<del>" inline( substr( line, 3, len - 4 ) ) "</del>" inline( substr( line, len + 1 ) );
262 # ^superscript^ (pandoc)
263 } else if ( match(line, /^\^([^[:space:]^]|\\[ ^])+\^/) ) {
265 return "<sup>" inline( substr( line, 2, len - 2 ) ) "</sup>" inline( substr( line, len + 1 ) );
267 # ~subscript~ (pandoc)
268 } else if ( match(line, /^~([^[:space:]~]|\\[ ~])+~/) ) {
270 return "<sub>" inline( substr( line, 2, len - 2 ) ) "</sub>" inline( substr( line, len + 1 ) );
272 # ignore embedded underscores (pandoc, php md)
273 } else if ( match(line, "^[[:alnum:]](__|_)") ) {
274 return HTML(substr( line, 1, RLENGTH)) inline( substr(line, RLENGTH + 1) );
277 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__$") ) {
279 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
282 } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__[[:space:][:punct:]]") ) {
284 return "<strong>" inline( substr( line, 3, len - 5 ) ) "</strong>" inline( substr( line, len) );
287 } else if ( match(line, "^\\*\\*(([^\\*[:space:]]|" iea ")|([^\\*[:space:]]|" iea ")(" na "|" iea ")*([^\\*[:space:]]|" iea "))\\*\\*") ) {
289 return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
292 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_$") ) {
294 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
297 } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_[[:space:][:punct:]]") ) {
299 return "<em>" inline( substr( line, 2, len - 3 ) ) "</em>" inline( substr( line, len ) );
302 } else if ( match(line, "^\\*(([^\\*[:space:]]|" isa ")|([^\\*[:space:]]|" isa ")(" na "|" isa ")*([^\\*[:space:]]|" isa "))\\*") ) {
304 return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
306 # Literal HTML entities
307 } else if ( match( line, /^&([a-zA-Z]{2,32}|#[0-9]{1,7}|#[xX][0-9a-fA-F]{1,6});/) ) {
309 return substr( line, 1, len ) inline(substr(line, len + 1));
312 } else if ( line ~ /^-->( |$)/) { # ignore multidash-arrow
313 return "-->" inline( substr(line, 4) );
314 } else if ( line ~ /^<-( |$)/) {
315 return "←" inline( substr(line, 3) );
316 } else if ( line ~ /^->( |$)/) {
317 return "→" inline( substr(line, 3) );
319 # Escape lone HTML character
320 } else if ( match( line, /^[&<>"']/) ) {
321 return HTML(substr(line, 1, 1)) inline(substr(line, 2));
323 # continue walk over string
325 return substr(line, 1, 1) inline( substr(line, 2) );
329 function headline( hlvl, htxt, attrib, LOCAL, sec, n, HL) {
330 match(hstack, /([0-9]+( [0-9]+){5})$/); split( substr(hstack, RSTART), HL);
332 for ( n = hlvl; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
333 HL[hlvl]++; for ( n = hlvl + 1; n <= 6; n++) { HL[n] = 0;}
335 hid = ""; for ( n = 2; n <= blvl; n++) { hid = hid BL[n] "/"; }
336 hid = hid HL[1]; for ( n = 2; n <= hlvl; n++) { hid = hid "." HL[n] ; }
337 hid = hid ":" URL(htxt, 1);
339 sub(/([0-9]+( [0-9]+){5})$/, "", hstack);
340 hstack = hstack HL[1] " " HL[2] " " HL[3] " " HL[4] " " HL[5] " " HL[6];
342 return sec "<section class=\"" (attrib ? "h" hlvl " " attrib : "h" hlvl) "\" id=\"" hid "\">" \
343 "<h" hlvl (attrib ? " class=\"" attrib "\"" : "") ">" inline( htxt ) \
344 "<a class=\"anchor\" href=\"#" hid "\"></a>" \
348 # Nested Block, resets heading counters
349 function _nblock( block, LOCAL, sec, n ) {
350 hstack = hstack " 0 0 0 0 0 0";
354 for ( n = blvl + 1; n in BL; n++) { delete BL[n]; }
356 block = _block( block );
357 match(hstack, /([0-9]+( [0-9]+){5})$/); split( substr(hstack, RSTART), HL);
358 sec = ""; for ( n = 1; n <= 6; n++ ) { sec = sec (HL[n]?"</section>":""); }
360 sub("( +[0-9]+){6} *$", "", hstack); blvl--;
364 function _block( block, LOCAL, st, len, text, title, attrib, href, guard, code, indent, list ) {
365 gsub( "(^\n+|\n+$)", "", block );
371 } else if ( AllowHTML && match( block, /(^|\n) ? ? ?(<!--([^-]|-[^-]|--[^>])*(-->|$)|<\?([^\?]|\?[^>])*(\?>|$)|<![A-Z][^>]*(>|$)|<!\[CDATA\[([^\]]|\][^\]]|\]\][^>])*(\]\]>|$))/) ) {
372 len = RLENGTH; st = RSTART;
373 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
376 } 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|$)/) ) {
377 len = RLENGTH; st = RSTART;
378 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
381 } else if ( AllowHTML && match( tolower(block), /(^|\n) ? ? ?<(script|pre|style)([[:space:]\n>]).*(<\/script>|<\/pre>|<\/style>|$)/) ) {
382 len = RLENGTH; st = RSTART;
383 match( tolower(substr(block, st, len)), /(<\/script>|<\/pre>|<\/style>)/);
384 len = RSTART + RLENGTH;
385 return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
388 } 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|$)/) ) {
389 len = RLENGTH; st = RSTART;
390 return substr(block, st, len) _block(substr(block, st + len));
392 # Metadata (custom, block starting with %something)
393 # Metadata is ignored but can be interpreted externally
394 } else if ( match(block, /^%[a-zA-Z-]+([[:space:]][^\n]*)?(\n|$)(%[a-zA-Z-]+([[:space:]][^\n]*)?(\n|$)|%([[:space:]][^\n]*)?(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
395 len = RLENGTH; st = RSTART;
396 return _block( substr( block, len + 1) );
398 # Blockquote (leading >)
399 } else if ( match( block, /^> /) ) {
400 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/);
401 len = RLENGTH; st = RSTART;
402 text = substr(block, 1, st - 1); gsub( /(^|\n)> /, "\n", text );
403 text = _nblock( text ); gsub( /^\n|\n$/, "", text )
404 return "<blockquote>" text "</blockquote>\n\n" _block( substr(block, st + len) );
406 # Pipe Tables (pandoc / php md / gfm )
407 } else if ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?)\n" \
408 "((\\|)?(:?-+:?[\\|+])+:?-+:?(\\|)?)\n" \
409 "((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ) {
410 len = RLENGTH; st = RSTART;
411 #initialize empty arrays
412 split("", talign); split("", tarray);
413 cols = 0; cnt=0; ttext = "";
415 # table header and alignment
416 split( gensub( /(^\||\|$)/, "", "g", \
417 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
418 substr(block, 1, match(block, /(\n|$)/)) \
420 block = substr(block, match(block, /(\n|$)/) + 1 );
422 gensub( /(^\||\|$)/, "", "g", \
423 substr(block, 1, match(block, /(\n|$)/)) \
425 block = substr(block, match(block, /(\n|$)/) + 1 );
427 for( cnt = 1; cnt < cols; cnt++ ) {
428 if (match(talign[cnt], /:-+:/)) talign[cnt]="center";
429 else if (match(talign[cnt], /-+:/)) talign[cnt]="right";
430 else if (match(talign[cnt], /:-+/)) talign[cnt]="left";
434 ttext = "<thead>\n<tr>"
435 for (cnt = 1; cnt < cols; cnt++)
436 ttext = ttext "<th align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</th>"
437 ttext = ttext "</tr>\n</thead><tbody>\n"
439 while ( match(block, "^((\\|)?([^\n]+\\|)+[^\n]+(\\|)?(\n|$))+" ) ){
440 split( gensub( /(^\||\|$)/, "", "g", \
441 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
442 substr(block, 1, match(block, /(\n|$)/)) \
444 block = substr(block, match(block, /(\n|$)/) + 1 );
447 for (cnt = 1; cnt < cols; cnt++)
448 ttext = ttext "<td align=\"" talign[cnt] "\">" inline(tarray[cnt]) "</td>"
449 ttext = ttext "</tr>\n"
451 return "<table>" ttext "</tbody></table>\n" _block(block);
453 # Grid Tables (pandoc)
454 # (with, and without header)
455 } else if ( match( block, "^\\+(-+\\+)+\n" \
456 "(\\|([^\n]+\\|)+\n)+" \
457 "(\\+(:?=+:?\\+)+)\n" \
458 "((\\|([^\n]+\\|)+\n)+" \
459 "\\+(-+\\+)+(\n|$))+" \
461 match( block, "^()()()" \
462 "(\\+(:?-+:?\\+)+)\n" \
463 "((\\|([^\n]+\\|)+\n)+" \
464 "\\+(-+\\+)+(\n|$))+" \
466 len = RLENGTH; st = RSTART;
467 #initialize empty arrays
468 split("", talign); split("", tarray); split("", tread);
469 cols = 0; cnt=0; ttext = "";
472 cols = split( gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block), tread, /\+/) - 2;
473 # debug(" Cols: " gensub( "^(\\+(:?-+:?\\+)+)(\n.*)*$", "\\1", 1, block ));
476 split( gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block ), talign, /\+/ );
477 # debug("Align: " gensub( "^(.*\n)?\\+((:?=+:?\\+|(:-+|-+:|:-+:)\\+)+)(\n.*)$", "\\2", "g", block ));
479 for (cnt = 1; cnt <= cols; cnt++) {
480 if (match(talign[cnt], /:(-+|=+):/)) talign[cnt]="center";
481 else if (match(talign[cnt], /(-+|=+):/)) talign[cnt]="right";
482 else if (match(talign[cnt], /:(-+|=+)/ )) talign[cnt]="left";
486 if ( match(block, "^\\+(-+\\+)+\n" \
487 "(\\|([^\n]+\\|)+\n)+" \
488 "\\+(:?=+:?\\+)+\n" \
489 "((\\|([^\n]+\\|)+\n)+" \
490 "\\+(-+\\+)+(\n|$))+" \
493 block = substr(block, match(block, /(\n|$)/) + 1 );
494 while ( match(block, "^\\|([^\n]+\\|)+\n") ) {
495 split( gensub( /(^\||\|$)/, "", "g", \
496 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
497 substr(block, 1, match(block, /(\n|$)/)) \
499 block = substr(block, match(block, /(\n|$)/) + 1 );
500 for (cnt = 1; cnt <= cols; cnt++)
501 tarray[cnt] = tarray[cnt] "\n" tread[cnt];
504 ttext = "<thead>\n<tr>"
505 for (cnt = 1; cnt <= cols; cnt++)
506 ttext = ttext "<th align=\"" talign[cnt] "\">" _nblock(tarray[cnt]) "</th>"
507 ttext = ttext "</tr>\n</thead>"
511 block = substr(block, match(block, /(\n|$)/) + 1 );
512 ttext = ttext "<tbody>\n"
514 while ( match(block, /^((\|([^\n]+\|)+\n)+\+(-+\+)+(\n|$))+/ ) ){
516 while ( match(block, /^\|([^\n]+\|)+\n/) ) {
517 split( gensub( /(^\||\|$)/, "", "g", \
518 gensub( /(^|[^\\])\\\|/, "\\1\\|", "g", \
519 substr(block, 1, match(block, /(\n|$)/)) \
521 block = substr(block, match(block, /(\n|$)/) + 1 );
522 for (cnt = 1; cnt <= cols; cnt++)
523 tarray[cnt] = tarray[cnt] "\n" tread[cnt];
525 block = substr(block, match(block, /(\n|$)/) + 1 );
528 for (cnt = 1; cnt <= cols; cnt++)
529 ttext = ttext "<td align=\"" talign[cnt] "\">" _nblock(tarray[cnt]) "</td>"
530 ttext = ttext "</tr>\n"
532 return "<table>" ttext "</tbody></table>\n" _nblock(block);
534 # Line Blocks (pandoc)
535 } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
536 len = RLENGTH; st = RSTART;
538 text = substr(block, 1, len); gsub(/\n[[:space:]]+/, " ", text);
539 gsub(/\n\| /, "\n", text); gsub(/^\| |\n$/, "", text);
540 text = inline(text); gsub(/\n/, "<br>\n", text);
542 return "<div class=\"line-block\">" text "</div>\n" _block( substr( block, len + 1) );
544 # Indented Code Block
545 } else if ( match(block, /^( |\t)( *\t*[^ \t\n]+ *\t*)+(\n|$)(( |\t)[^\n]+(\n|$)|[ \t]*(\n|$))*/) ) {
546 len = RLENGTH; st = RSTART;
547 code = substr(block, 1, len);
548 gsub(/(^|\n)( |\t)/, "\n", code);
549 gsub(/^\n|\n+$/, "", code);
550 return "<pre><code>" HTML( code ) "</code></pre>\n" \
551 _block( substr( block, len + 1 ) );
553 # Fenced Divs (pandoc, custom)
554 } else if ( match( block, /^(:::+)/ ) ) {
555 guard = substr( block, 1, RLENGTH );
556 code = block; sub(/^[^\n]+\n/, "", code);
557 attrib = gensub(/^:::+[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\1", 1, block);
558 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib);
559 gsub(/(^ | $)/, "", attrib);
560 if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
561 len = RLENGTH; st = RSTART;
562 return "<div class=\"" attrib "\">" _nblock( substr(code, 1, st - 1) ) "</div>\n" \
563 _block( substr( code, st + len ) );
565 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
566 len = RLENGTH; st = RSTART;
567 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
568 _block( substr(block, st + len) );
571 # Fenced Code Block (pandoc)
572 } else if ( match( block, /^(~~~+|```+)/ ) ) {
573 guard = substr( block, 1, RLENGTH );
574 code = gensub(/^[^\n]+\n/, "", 1, block);
575 attrib = gensub(/^(~~~+|```+)[ \t]*\{?[ \t]*([^\}\n]*)\}?[ \t]*\n.*$/, "\\2", 1, block);
576 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib);
577 gsub(/(^ | $)/, "", attrib);
578 if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
579 len = RLENGTH; st = RSTART;
580 return "<pre><code class=\"" attrib "\">" HTML( substr(code, 1, st - 1) ) "</code></pre>\n" \
581 _block( substr( code, st + len ) );
583 match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
584 len = RLENGTH; st = RSTART;
585 return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
586 _block( substr(block, st + len) );
589 # First Order Heading H1 + Attrib
590 } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n===+(\n|$)/ ) ) {
591 len = RLENGTH; text = attrib = block;
592 sub(/([ \t]*\{([^\}\n]+)\})\n===+(\n.*)?$/, "", text);
593 sub(/\}\n===+(\n.*)?$/, "", attrib); sub(/^([^\n]+)[ \t]*\{/, "", attrib);
594 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
596 return headline(1, text, attrib) _block( substr( block, len + 1 ) );
598 # First Order Heading H1
599 } else if ( match( block, /^([^\n]+)\n===+(\n|$)/ ) ) {
600 len = RLENGTH; text = substr(block, 1, len);
601 sub(/\n===+(\n.*)?$/, "", text);
603 return headline(1, text, 0) _block( substr( block, len + 1 ) );
605 # Second Order Heading H2 + Attrib
606 } else if ( match( block, /^([^\n]+)([ \t]*\{([^\}\n]+)\})\n---+(\n|$)/ ) ) {
607 len = RLENGTH; text = attrib = block;
608 sub(/([ \t]*\{([^\}\n]+)\})\n---+(\n.*)?$/, "", text);
609 sub(/\}\n---+(\n.*)?$/, "", attrib); sub(/^([^\n]+)[ \t]*\{/, "", attrib);
610 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
612 return headline(2, text, attrib) _block( substr( block, len + 1) );
614 # Second Order Heading H2
615 } else if ( match( block, /^([^\n]+)\n---+(\n|$)/ ) ) {
616 len = RLENGTH; text = substr(block, 1, len);
617 sub(/\n---+(\n.*)?$/, "", text);
619 return headline(2, text, 0) _block( substr( block, len + 1) );
621 # Nth Order Heading H1 H2 H3 H4 H5 H6 + Attrib
622 } else if ( match( block, /^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n|$)/ ) ) {
623 len = RLENGTH; text = attrib = substr(block, 1, len);
624 match(block, /^#{1,6}/); n = RLENGTH;
626 sub(/^(#{1,6})[ \t]*/, "", text); sub(/[ \t]*#*([ \t]*\{([a-zA-Z \t-]*)\})(\n.*)?$/, "", text);
627 sub(/^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*[ \t]*\{/, "", attrib);
628 sub(/\})(\n.*)?$/, "", attrib);
629 gsub(/[^a-zA-Z0-9_-]+/, " ", attrib); gsub(/(^ | $)/, "", attrib);
631 return headline( n, text, attrib ) _block( substr( block, len + 1) );
633 # Nth Order Heading H1 H2 H3 H4 H5 H6
634 } else if ( match( block, /^(#{1,6})[ \t]*(([^ \t\n]+|[ \t]+[^ \t\n#]|[ \t]+#+[ \t]*[^ \t\n#])+)[ \t]*#*(\n|$)/ ) ) {
635 len = RLENGTH; text = substr(block, 1, len);
636 match(block, /^#{1,6}/); n = RLENGTH;
637 sub(/^(#{1,6})[ \t]*/, "", text); sub(/[ \t]*#*(\n.*)?$/, "", text);
639 return headline( n, text, 0 ) _block( substr( block, len + 1) );
641 # block images (wrapped in <figure>)
642 } else if ( match(block, "^!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?(\n|$)") ) {
643 len = RLENGTH; text = href = title = attrib = substr( block, 1, len);
645 sub("^!\\[", "", text);
646 sub("\\]\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?(\n.*)?$", "", text);
648 sub("^!" lix "\\([\n\t ]*", "", href);
649 sub("([\n\t ]+" lit ")?[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?(\n.*)?$", "", href);
651 sub("^!" lix "\\([\n\t ]*" lid, "", title);
652 sub("[\n\t ]*\\)(\\{[a-zA-Z \t-]*\\})?(\n.*)?$", "", title);
653 sub("^[\n\t ]+", "", title);
655 sub("^!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\)", "", attrib);
656 sub("(\n.*)?$", "", attrib);
657 sub(/^\{[ \t]*/, "", attrib); sub(/[ \t]*\}$/, "", attrib); gsub(/[ \t]+/, " ", attrib);
659 if ( match(href, /^<.*>$/) ) { sub(/^</, "", href); sub(/>$/, "", href); }
660 if ( match(title, /^".*"$/) ) { sub(/^"/, "", title); sub(/"$/, "", title); }
661 else if ( match(title, /^'.*'$/) ) { sub(/^'/, "", title); sub(/'$/, "", title); }
662 else if ( match(title, /^\(.*\)$/) ) { sub(/^\(/, "", title); sub(/\)$/, "", title); }
664 gsub(/^[\t ]+$/, "", text); gsub(/\\/, "", href);
666 return "<figure data-src=\"" URL(href, 1) "\"" (attrib?" class=\"" HTML(attrib) "\"":"") ">" \
667 "<img src=\"" URL(href, 1) "\" alt=\"" HTML(text?text:title?title:href) "\"" \
668 (attrib?" class=\"" HTML(attrib) "\"":"") ">" \
669 (title?"<figcaption>" inline(title) "</figcaption>":"") \
671 _block( substr( block, len + 1) );
673 # reference style images (block)
674 } else if ( match(line, /^!\[([^]]*)\] ?\[([^]]*)\](\n|$)/ ) ) {
676 text = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\1", 1, block);
677 id = gensub(/^!\[([^\n]*)\] ?\[([^\n]*)\](\n.*)?$/, "\\2", 1, block);
678 if ( ! id ) id = text;
679 if ( rl_href[id] && rl_title[id] ) {
680 return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
681 "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
682 "<figcaption>" inline(rl_title[id]) "</figcaption>" \
684 _block( substr( block, len + 1) );
685 } else if ( rl_href[id] ) {
686 return "<figure data-src=\"" URL(rl_href[id], 1) "\">" \
687 "<img src=\"" URL(rl_href[id], 1) "\" alt=\"" HTML(text) "\">" \
689 _block( substr( block, len + 1) );
691 return "<p>" HTML(substr(block, 1, len)) "</p>\n" _block( substr(block, len + 1) );
694 # Macros (standalone <<macro>> calls handled as block, so they are not wrapped in paragraph)
695 } else if ( match( block, /^<<(([^>]|>[^>])+)>>(\n|$)/ ) ) {
697 text = gensub(/^<<(([^>]|>[^>])+)>>(\n.*)?$/, "\\1", 1, block);
698 return "<code class=\"macro\">" HTML(text) "</code>" _block(substr(block, len + 1) );
701 } else if (match( block, "^(([ \t]*\n)*[^:\n \t][^\n]+\n" \
702 "([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
703 "(([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
704 "|[^:\n \t][^\n]+(\n|$)" \
705 "|( ? ? ?\t| +)[^\n]+(\n|$)" \
706 "|([ \t]*\n)+( ? ? ?\t| +)[^\n]+(\n|$))*)+" \
708 list = substr( block, 1, RLENGTH); block = substr( block, RLENGTH + 1);
709 return "\n<dl>\n" _dlist( list ) "</dl>\n" _block( block );
711 # Unordered list types
712 } else if ( text = _startlist( block, "ul", "-", "([+*•]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) {
714 } else if ( text = _startlist( block, "ul", "\\+", "([-*•]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) {
716 } else if ( text = _startlist( block, "ul", "\\*", "([-+•]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) {
718 } else if ( text = _startlist( block, "ul", "•", "([-+*]|[0-9]+\\.|#\\.|[0-9]+\\)|#\\))") ) {
722 } else if ( text = _startlist( block, "ol", "[0-9]+\\.", "([-+*•]|#\\.|[0-9]+\\)|#\\))") ) {
724 } else if ( text = _startlist( block, "ol", "[0-9]+\\)", "([-+*•]|[0-9]+\\.|#\\.|#\\))") ) {
726 } else if ( text = _startlist( block, "ol", "#\\.", "([-+*•]|[0-9]+\\.|[0-9]+\\)|#\\))") ) {
728 } else if ( text = _startlist( block, "ol", "#\\)", "([-+*•]|[0-9]+\\.|#\\.|[0-9]+\\))") ) {
732 } else if ( match( block, /(^|\n)[[:space:]]*(\n|$)/) ) {
733 len = RLENGTH; st = RSTART;
734 return _block( substr(block, 1, st - 1) ) "\n" \
735 _block( substr(block, st + len) );
738 } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
739 len = RLENGTH; st = RSTART;
740 return _block(substr(block, 1, st - 1)) "<hr>\n" _block(substr(block, st + len));
744 return "<p>" inline(block) "</p>\n";
748 function _startlist(block, type, mark, exclude, LOCAL, st, len, list, indent, text) {
749 if (match( block, "(^|\n) ? ? ?" mark "[ \t][^\n]+(\n|$)" \
750 "(([ \t]*\n)* ? ? ?" mark "[ \t][^\n]+(\n|$)" \
751 "|([ \t]*\n)*( ? ? ?\t| +)[^\n]+(\n|$)" \
752 "|[^\n \t][^\n]+(\n|$))*" ) ) {
753 st = RSTART; len = RLENGTH; list = substr( block, st, len);
755 sub("^\n", "", list); match(list, "^ ? ? ?"); indent = RLENGTH;
756 gsub( "(^|\n) {0," indent "}", "\n", list); sub("^\n", "", list);
758 text = substr(block, 1, st - 1); block = substr(block, st + len);
759 if (match(text, /\n[[:space:]]*\n/)) return 0;
760 if (match(text, "(^|\n) ? ? ?" exclude "[ \t][^\n]+")) return 0;
761 if (match( list, "\n" exclude "[ \t]" )) {
762 block = substr(list, RSTART + 1) block;
763 list = substr(list, 1, RSTART);
766 return _block( text ) "<" type ">\n" _list( list, mark ) "</" type ">\n" _block( block );
770 function _list (block, mark, p, LOCAL, len, st, text, indent, task) {
771 if ( match(block, "^([ \t]*\n)*$")) return;
773 match(block, "^" mark "[ \t]"); indent = RLENGTH;
774 sub("^" mark "[ \t]", "", block);
776 if (match(block, /\n[ \t]*\n/)) p = 1;
778 match( block, "\n" mark "[ \t][^\n]+(\n|$)" );
779 st = (RLENGTH == -1) ? length(block) + 1 : RSTART;
780 text = substr(block, 1, st); block = substr(block, st + 1);
782 gsub("\n {0," indent "}", "\n", text);
784 task = match( text, /^\[ \]/ ) ? "<li class=\"task pending\"><input type=checkbox disabled>" : \
785 match( text, /^\[-\]/ ) ? "<li class=\"task negative\"><input type=checkbox disabled>" : \
786 match( text, /^\[\/\]/ ) ? "<li class=\"task partial\"><input type=checkbox disabled>" : \
787 match( text, /^\[\?\]/ ) ? "<li class=\"task unsure\"><input type=checkbox disabled>" : \
788 match( text, /^\[[xX]\]/) ? "<li class=\"task done\"><input type=checkbox disabled checked>" : "<li>";
789 sub(/^\[[-? \/xX]\]/, "", text);
791 text = _nblock( text );
792 if ( ! p && match( text, "^<p>(</p[^>]|</[^p]|<[^/]|[^<])*</p>\n$" ))
793 gsub( "(^<p>|</p>\n$)", "", text);
795 return task text "</li>\n" _list(block, mark, p);
798 function _dlist (block, LOCAL, len, st, text, indent, p) {
799 if (match( block, "^([ \t]*\n)*[^:\n \t][^\n]+\n" )) {
800 len = RLENGTH; text = substr(block, 1, len);
801 gsub( "(^\n*|\n*$)", "", text );
802 return "<dt>" inline( text ) "</dt>\n" _dlist( substr(block, len + 1) );
803 } else if (match( block, "^([ \t]*\n)* ? ? ?:[ \t][^\n]+(\n|$)" \
804 "([^:\n \t][^\n]+(\n|$)" \
805 "|( ? ? ?\t| +)[^\n]+(\n|$)" \
806 "|([ \t]*\n)+( ? ? ?\t| +)[^\n]+(\n|$))*" \
808 len = RLENGTH; text = substr(block, 1, len);
809 sub( "^([ \t]*\n)*", "", text);
810 match(text, "^ ? ? ?:(\t| +)"); indent = RLENGTH;
811 sub( "^ ? ? ?:(\t| +)", "", text);
812 gsub( "(^|\n) {0," indent "}", "\n", text );
814 text = _nblock(text);
815 if (match( text, "^<p>(</p[^>]|</[^p]|<[^/]|[^<])*</p>\n$" ))
816 gsub( "(^<p>|</p>\n$)", "", text);
818 return "<dd>" text "</dd>\n" _dlist( substr(block, len + 1) );
824 file = ""; rl_href[""] = ""; rl_title[""] = "";
825 if (ENVIRON["MD_HTML"] == "true") { AllowHTML = "true"; }
826 HL[1] = 0; HL[2] = 0; HL[3] = 0; HL[4] = 0; HL[5] = 0; HL[6] = 0;
827 # hls = "0 0 0 0 0 0";
830 nu = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\_]|_[[:alnum:]])*" # not underline (except when escaped)
831 na = "(\\\\\\\\|\\\\[^\\\\]|[^\\\\\\*])*" # not asterisk (except when escaped)
832 ieu = "_([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])_" # inner <em> (underline)
833 isu = "__([^_[:space:]]|[^_[:space:]]" nu "[^_[:space:]])__" # inner <strong> (underline)
834 iea = "\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*" # inner <em> (asterisk)
835 isa = "\\*\\*([^\\*[:space:]]|[^\\*[:space:]]" na "[^\\*[:space:]])\\*\\*" # inner <strong> (asterisk)
837 lix="\\[(\\\\[^\n]|[^]\n\\\\[])*\\]" # link text
838 lid="(<(\\\\[^\n]|[^\n<>\\\\])*>|(\\\\.|[^()\"'\\\\])+|([^<\n\t ()\\\\]|\\\\[^\n])(\\\\[\n]|[^\n\t \\(\\)\\\\])*)" # link dest
839 lit="(\"(\\\\.|[^\"\\\\])*\"|'(\\\\.|[^'\\\\])*'|\\((\\\\.|[^\\(\\)\\\\])*\\))" # link text
840 # link text with image def
841 lii="\\[(\\\\[^\n]|[^]\n\\\\[])*(!" lix "\\([\n\t ]*" lid "([\n\t ]+" lit ")?[\n\t ]*\\))?(\\\\[^\n]|[^]\n\\\\[])*\\]"
843 # Buffering of full file ist necessary, e.g. to find reference links
844 while (getline) { file = file $0 "\n"; }
845 # Clean up MS-DOS line breaks
846 gsub(/\r\n/, "\n", file);
848 # Fill array of reference links
850 re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
851 # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
852 while ( match(f, re_reflink ) ) {
853 rl_id = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
854 rl_href[rl_id] = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
855 rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
856 f = substr(f, RSTART + RLENGTH);
857 rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
858 if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
860 # Clear reflinks from File
861 while( gsub(re_reflink, "\n", file ) );
862 # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
864 # Run Block Processing -> The Actual Markdown!
865 printf "%s", _nblock( file );