]> git.plutz.net Git - cgilite/blob - markdown.awk
md: inline HTML
[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 # - [x] Verbatim HTML inline
39 # - [ ] HTML escaping
40 #
41 # Extensions - Block elements:
42 # ----------------------------
43 # -  ?  Heading identifiers (php md, pandoc)
44 # - [x] Fenced code blocks (php md, pandoc)
45 #   - [-] Fenced code attributes
46 # - [ ] Tables
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)
57 #
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 <-
74
75 function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; }
76
77 function HTML ( text ) {
78   gsub( /&/,  "\\&amp;",  text );
79   gsub( /</,  "\\&lt;",   text );
80   gsub( />/,  "\\&gt;",   text );
81   gsub( /"/,  "\\&quot;", text );
82   gsub( /'/,  "\\&#x27;", text );
83   gsub( /\\/, "\\&#x5C;", text );
84   return text;
85 }
86
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)
94
95   if ( line ~ /^$/ ) {  # Recursion End
96     return "";
97
98   #  omit processing of escaped characters
99   } else if ( line ~ /^\\[]\\`\*_\{\}\(\)#\+-\.![]/) {
100     return substr(line, 2, 1) inline( substr(line, 3) );
101
102   # hard brakes
103   } else if ( match(line, /^  \n/) ) {
104     return "<br />\n" inline( substr(line, RLENGTH + 1) );
105
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:]]*\/?>)/) ) {
108     len = RLENGTH;
109     return substr( line, 1, len) inline(substr(line, len + 1));
110
111   #  ``code spans``
112   } else if ( match( line, /^`+/) ) {
113     len = RLENGTH
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( /&/, "\\&amp;", code ); gsub( /</, "\\&lt;", code ); gsub( />/, "\\&gt;", code );
122       return "<code>" code "</code>" inline( substr( line, len + 1 ) )
123     }
124
125   #  quick links ("automatic links" in md doc)
126   } else if ( match( line, /^<[a-zA-Z]+:\/\/([-\.[:alnum:]]+)(:[0-9]*)?(\/[^>]*)?>/ ) ) {
127     len = RLENGTH;
128     href = HTML( substr( line, 2, len - 2) );
129     return "<a href=\"" href "\">" href "</a>" inline( substr( line, len + 1) );
130
131   # inline links
132   } else if ( match(line, /^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/) ) {
133     len = RLENGTH;
134     text  = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\1", "g", line);
135     href  = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\2", "g", line);
136     title = gensub(/^\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\4", "g", line);
137     if ( title ) {
138       return "<a href=\"" HTML(href) "\" title=\"" HTML(title) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
139     } else {
140       return "<a href=\"" HTML(href) "\">" inline( text ) "</a>" inline( substr( line, len + 1) );
141     }
142
143   # reference style links
144   } else if ( match(line, /^\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
145     len = RLENGTH;
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) );
153     } else {
154       return "" substr(line, 1, len) inline( substr(line, len + 1) );
155     }
156
157   # inline images
158   } else if ( match(line, /^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/) ) {
159     len = RLENGTH;
160     text  = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\1", "g", line);
161     href  = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\2", "g", line);
162     title = gensub(/^!\[([^]]+)\]\(([^"\)]+)([ \t]+"([^"]+)")?\)/, "\\4", "g", line);
163     if ( title ) {
164       return "<img src=\"" HTML(href) "\" alt=\"" HTML(text) "\" title=\"" HTML(title) "\" />" inline( substr( line, len + 1) );
165     } else {
166       return "<img src=\"" HTML(href) "\" alt=\"" HTML(text) "\" />" inline( substr( line, len + 1) );
167     }
168
169   # reference style images
170   } else if ( match(line, /^!\[([^]]+)\] ?\[([^]]*)\]/ ) ) {
171     len = RLENGTH;
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) );
179     } else {
180       return "" substr(line, 1, len) inline( substr(line, len + 1) );
181     }
182
183   #  ~~strikeout~~ (pandoc)
184   } else if ( match(line, /^~~([[:graph:]]|[[:graph:]]([^~]|~[^~])*[[:graph:]])~~/) ) {
185     len = RLENGTH;
186     return "<del>" inline( substr( line, 3, len - 4 ) ) "</del>" inline( substr( line, len + 1 ) );
187
188   #  ^superscript^ (pandoc)
189   } else if ( match(line, /^\^([^[:space:]^]|\\[ ^])+\^/) ) {
190     len = RLENGTH;
191     return "<sup>" inline( substr( line, 2, len - 2 ) ) "</sup>" inline( substr( line, len + 1 ) );
192
193   #  ~subscript~ (pandoc)
194   } else if ( match(line, /^~([^[:space:]~]|\\[ ~])+~/) ) {
195     len = RLENGTH;
196     return "<sub>" inline( substr( line, 2, len - 2 ) ) "</sub>" inline( substr( line, len + 1 ) );
197
198   # ignore embedded underscores (pandoc, php md)
199   } else if ( match(line, "^[[:alnum:]](__|_)") ) {
200     return substr( line, 1, RLENGTH) inline( substr(line, RLENGTH + 1) );
201
202   #  __strong__$
203   } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__$") ) {
204     len = RLENGTH;
205     return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
206
207   #  __strong__
208   } else if ( match(line, "^__(([^_[:space:]]|" ieu ")|([^_[:space:]]|" ieu ")(" nu "|" ieu ")*([^_[:space:]]|" ieu "))__[[:space:][:punct:]]") ) {
209     len = RLENGTH;
210     return "<strong>" inline( substr( line, 3, len - 5 ) ) "</strong>" inline( substr( line, len) );
211
212   #  **strong**
213   } else if ( match(line, "^\\*\\*(([^\\*[:space:]]|" iea ")|([^\\*[:space:]]|" iea ")(" na "|" iea ")*([^\\*[:space:]]|" iea "))\\*\\*") ) {
214     len = RLENGTH;
215     return "<strong>" inline( substr( line, 3, len - 4 ) ) "</strong>" inline( substr( line, len + 1 ) );
216
217   #  _em_$
218   } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_$") ) {
219     len = RLENGTH;
220     return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
221
222   #  _em_
223   } else if ( match(line, "^_(([^_[:space:]]|" isu ")|([^_[:space:]]|" isu ")(" nu "|" isu ")*([^_[:space:]]|" isu "))_[[:space:][:punct:]]") ) {
224     len = RLENGTH;
225     return "<em>" inline( substr( line, 2, len - 3 ) ) "</em>" inline( substr( line, len ) );
226
227   #  *em*
228   } else if ( match(line, "^\\*(([^\\*[:space:]]|" isa ")|([^\\*[:space:]]|" isa ")(" na "|" isa ")*([^\\*[:space:]]|" isa "))\\*") ) {
229     len = RLENGTH;
230     return "<em>" inline( substr( line, 2, len - 2 ) ) "</em>" inline( substr( line, len + 1 ) );
231
232   #  continue walk over string
233   } else {
234     return substr(line, 1, 1) inline( substr(line, 2) );
235   }
236 }
237
238 function _block( block, LOCAL, st, len, hlvl, htxt, guard, code, indent ) {
239   gsub( /^\n+|\n+$/, "", block );
240
241   if ( block == "" ) {
242     return "";
243
244   # HTML #2 #3 #4 $5
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));
248
249   # HTML #6
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));
253
254   # HTML #1
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));
260
261   # HTML #7
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));
265
266   # Horizontal rule
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));
270  
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) );
277
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) );
287
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 ) );
296
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 ) );
305     } else {
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) );
310     }
311
312   # Unordered list
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) );
320
321   gsub("(^|\n) {0," indent "}", "\n", list);
322   return "\n<ul>\n" _list( substr(list, 2) ) "</ul>\n" _block( block );
323
324   # Ordered list
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) );
332
333   gsub("(^|\n) {0," indent "}", "\n", list);
334   return "\n<ol>\n" _list( substr(list, 2) ) "</ol>\n" _block( block );
335
336   # First Order Heading
337   } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
338     len = RLENGTH;
339     return "<h1>" inline( gensub( /\n.*$/, "", "g", block ) ) "</h1>\n\n" \
340            _block( substr( block, len + 1 ) );
341
342   # Second Order Heading
343   } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
344     len = RLENGTH;
345     return "<h2>" inline( gensub( /\n.*$/, "", "g", block ) ) "</h2>\n\n" \
346            _block( substr( block, len + 1) );
347
348   # Nth Order Heading
349   } else if ( match( block, /^#{1,6}[[:space:]]*[^\n]+([[:space:]]*#*)(\n|$)/ ) ) {
350     len = RLENGTH;
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) );
355
356   # Plain paragraph
357   } else {
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) );
362   }
363 }
364
365 function _list( block, last, LOCAL, p) {
366   if ( ! length(block) ) return "";
367   gsub(/^([-+*]|[0-9]+\.|#\.)(  ? ? ?|\t)/, "", block)
368
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);
373   } else {
374     p = block; block = "";
375   }
376   sub( /\n +([-+*]|[0-9]+\.|#\.)/, "\n&", p );
377
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);
382   } else {
383     last = p; p = _block(p);
384     sub( /^<p>/, "", p );
385     sub( /<\/p>\n/, "", p );
386   }
387   sub( /\n$/, "", p );
388   return "<li>" p "</li>\n" _list( block, last );
389 }
390
391 BEGIN {
392   # Global Vars
393   file = ""; rl_href[""] = ""; rl_title[""] = "";
394
395   # Buffering of full file ist necessary, e.g. to find reference links
396   while (getline) { file = file $0 "\n"; }
397
398   # Fill array of reference links
399   f = file; rl_id;
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 );
409   }
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] ); }
413
414   # Run Block Processing -> The Actual Markdown!
415   printf "%s", _block( file );
416 }