]> git.plutz.net Git - cgilite/blob - markdown.awk
bugfix: stop condition in HTML block
[cgilite] / markdown.awk
1 #!/bin/awk -f
2 #!/opt/busybox/awk -f
3
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
7 #
8 # ToDo:
9 # - HTML processing / escaping (according to environment flag)
10 # - em-dashes and arrows
11
12 # Supported Features / TODO:
13 # ==========================
14 # [x] done    [ ] todo    [-] not planned    ? unsure
15 #
16 # Basic Markdown - Block elements:
17 # -------------------------------
18 # - [x] Paragraphs
19 #   - [x] Double space line breaks
20 # - [x] Proper block element nesting
21 # - [x] Headings
22 # - [x] ATX-Style Headings
23 # - [x] Blockquotes
24 # - [x] Lists (ordered, unordered)
25 # - [x] Code blocks (using indention)
26 # - [x] Horizontal rules
27 # - [x] Verbatim HTML block (gfm)
28 #
29 # Basic Markdown - Inline elements:
30 # ---------------------------------
31 # - [x] Links
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
39 #
40 # Extensions - Block elements:
41 # ----------------------------
42 # -  ?  Heading identifiers (php md, pandoc)
43 # - [x] Fenced code blocks (php md, pandoc)
44 #   - [-] Fenced code attributes
45 # - [ ] Tables
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)
56 #
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 <-
73
74 function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; }
75
76 function HTML ( text ) {
77   gsub( /&/,  "\\&amp;",  text );
78   gsub( /</,  "\\&lt;",   text );
79   gsub( />/,  "\\&gt;",   text );
80   gsub( /"/,  "\\&quot;", text );
81   gsub( /'/,  "\\&#x27;", text );
82   gsub( /\\/, "\\&#x5C;", text );
83   return text;
84 }
85
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)
93
94   if ( line ~ /^$/ ) {  # Recursion End
95     return "";
96
97   #  omit processing of escaped characters
98   } else if ( line ~ /^\\[]\\`\*_\{\}\(\)#\+-\.![]/) {
99     return substr(line, 2, 1) inline( substr(line, 3) );
100
101   # hard brakes
102   } else if ( match(line, /^  \n/) ) {
103     return "<br />\n" inline( substr(line, RLENGTH + 1) );
104
105   #  ``code spans``
106   } else if ( match( line, /^`+/) ) {
107     len = RLENGTH
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( /&/, "\\&amp;", code ); gsub( /</, "\\&lt;", code ); gsub( />/, "\\&gt;", code );
116       return "<code>" code "</code>" inline( substr( line, len + 1 ) )
117     }
118
119   #  quick links ("automatic links" in md doc)
120   } else if ( match( line, /^<[a-zA-Z]+:\/\/([-\.[:alnum:]]+)(:[0-9]*)?(\/[^>]*)?>/ ) ) {
121     len = RLENGTH;
122     href = HTML( substr( line, 2, len - 2) );
123     return "<a href=\"" href "\">" href "</a>" inline( substr( line, len + 1) );
124
125   # inline links
126   } else if ( match(line, /^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/) ) {
127     len = RLENGTH;
128     text  = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\1", "g", line);
129     href  = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\2", "g", line);
130     title = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\4", "g", line);
131     if ( title ) {
132       return "<a href=\"" HTML(href) "\" title=\"" HTML(title) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
133     } else {
134       return "<a href=\"" HTML(href) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
135     }
136
137   # reference style links
138   } else if ( match(line, /^\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
139     len = RLENGTH;
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) );
147     } else {
148       return "" substr(line, 1, len) inline( substr(line, len + 1) );
149     }
150
151   # inline images
152   } else if ( match(line, /^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/) ) {
153     len = RLENGTH;
154     text  = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\1", "g", line);
155     href  = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\2", "g", line);
156     title = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\4", "g", line);
157     if ( title ) {
158       return "<img src=\"" HTML(href) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\" />" inline( substr( line, len + 1) );
159     } else {
160       return "<img src=\"" HTML(href) "\" alt=\"" HTML(text) "\" />" inline( substr( line, len + 1) );
161     }
162
163   # reference style images
164   } else if ( match(line, /^!\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
165     len = RLENGTH;
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) );
173     } else {
174       return "" substr(line, 1, len) inline( substr(line, len + 1) );
175     }
176
177   #  ~~strikeout~~ (pandoc)
178   } else if ( match(line, /^~~([[:graph:]]|[[:graph:]]([^~]|~[^~])*[[:graph:]])~~/) ) {
179     len = RLENGTH;
180     return "<del>" inline( substr( line, 3, len - 4 ) ) "</del>" inline( substr( line, len + 1 ) );
181
182   #  ^superscript^ (pandoc)
183   } else if ( match(line, /^\^([^[:space:]^]|\\[ ^])+\^/) ) {
184     len = RLENGTH;
185     return "<sup>" inline( substr( line, 2, len - 2 ) ) "</sup>" inline( substr( line, len + 1 ) );
186
187   #  ~subscript~ (pandoc)
188   } else if ( match(line, /^~([^[:space:]~]|\\[ ~])+~/) ) {
189     len = RLENGTH;
190     return "<sub>" inline( substr( line, 2, len - 2 ) ) "</sub>" inline( substr( line, len + 1 ) );
191
192   # ignore embedded underscores (pandoc, php md)
193   } else if ( match(line, "^[[:alnum:]](__|_)") ) {
194     return substr( line, 1, RLENGTH) inline( substr(line, RLENGTH + 1) );
195
196   #  __strong__$
197   } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__$") ) {
198     len = RLENGTH;
199     return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
200
201   #  __strong__
202   } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__[[:space:][:punct:]]") ) {
203     len = RLENGTH;
204     return "<strong>" inline( substr( line, 3, len - 5 ) ) "</strong>" inline( substr( line, len) );
205
206   #  **strong**
207   } else if ( match(line, "^\\*\\*(([^\\*[:space:]]|" iea ")|([^\\*[:space:]]|" iea ")(" na "|" iea ")*([^\\*[:space:]]|" iea "))\\*\\*") ) {
208     len = RLENGTH;
209     return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
210
211   #  _em_$
212   } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_$") ) {
213     len = RLENGTH;
214     return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
215
216   #  _em_
217   } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_[[:space:][:punct:]]") ) {
218     len = RLENGTH;
219     return "<em>" inline( substr( line, 2, len - 3 ) ) "</em>" inline( substr( line, len ) );
220
221   #  *em*
222   } else if ( match(line, "^\\*(([^\\*[:space:]]|" isa ")|([^\\*[:space:]]|" isa ")(" na "|" isa ")*([^\\*[:space:]]|" isa "))\\*") ) {
223     len = RLENGTH;
224     return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
225
226   #  continue walk over string
227   } else {
228     return substr(line, 1, 1) inline( substr(line, 2) );
229   }
230 }
231
232 function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent ) {
233   gsub( /^\n+|\n+$/, "", block );
234
235   if ( block == "" ) {
236     return "";
237
238   # HTML #2
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));
242
243   # HTML #3
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));
247
248   # HTML #4
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));
252
253   # HTML #5
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));
257
258   # HTML #6
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));
262
263   # HTML #1
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));
269
270   # HTML #7
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));
274
275   # Horizontal rule
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));
279  
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) );
286
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) );
296
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 ) );
305
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 ) );
314     } else {
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) );
319     }
320
321   # Unordered list
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) );
329
330   gsub("(^|\n) {0," indent "}", "\n", list);
331   return "\n<ul>\n" _list( substr(list, 2) ) "</ul>\n" _block( block );
332
333   # Ordered list
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) );
341
342   gsub("(^|\n) {0," indent "}", "\n", list);
343   return "\n<ol>\n" _list( substr(list, 2) ) "</ol>\n" _block( block );
344
345   # First Order Heading
346   } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
347     len = RLENGTH;
348     return "<h1>" inline( gensub( /\n.*$/, "", "g", block ) ) "</h1>\n\n" \
349            _block( substr( block, len + 1 ) );
350
351   # Second Order Heading
352   } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
353     len = RLENGTH;
354     return "<h2>" inline( gensub( /\n.*$/, "", "g", block ) ) "</h2>\n\n" \
355            _block( substr( block, len + 1) );
356
357   # Nth Order Heading
358   } else if ( match( block, /^#{1,6}[[:space:]]*[^\n]+([[:space:]]*#*)(\n|$)/ ) ) {
359     len = RLENGTH;
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) );
364
365   # Plain paragraph
366   } else {
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) );
371   }
372 }
373
374 function _list( block, last, LOCAL, p) {
375   if ( ! length(block) ) return "";
376   gsub(/^([-+*]|[0-9]+\.|#\.)(  ? ? ?|\t)/, "", block)
377
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);
382   } else {
383     p = block; block = "";
384   }
385   sub( /\n +([-+*]|[0-9]+\.|#\.)/, "\n&", p );
386
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);
391   } else {
392     last = p; p = _block(p);
393     sub( /^<p>/, "", p );
394     sub( /<\/p>\n/, "", p );
395   }
396   sub( /\n$/, "", p );
397   return "<li>" p "</li>\n" _list( block, last );
398 }
399
400 BEGIN {
401   # Global Vars
402   file = ""; rl_href[""] = ""; rl_title[""] = "";
403
404   # Buffering of full file ist necessary, e.g. to find reference links
405   while (getline) { file = file $0 "\n"; }
406
407   # Fill array of reference links
408   f = file; rl_id;
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 );
418   }
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] ); }
422
423   # Run Block Processing -> The Actual Markdown!
424   printf "%s", _block( file );
425 }