]> git.plutz.net Git - cgilite/blob - markdown.awk
md: verbatim html block, md: allow emphasis before punctuation
[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     return _block(substr(block, 1, st - 1)) substr(block, st, len) _block(substr(block, st + len));
267
268   # HTML #7
269   } 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[[:space:]]*\n|$)/) ) {
270     len = RLENGTH; st = RSTART;
271     return substr(block, st, len) _block(substr(block, st + len));
272
273   # Horizontal rule
274   } else if ( match( block, /(^|\n) ? ? ?((\* *){3,}|(- *){3,}|(_ *){3,})($|\n)/) ) {
275     len = RLENGTH; st = RSTART;
276     return _block(substr(block, 1, st - 1)) "<hr />\n" _block(substr(block, st + len));
277  
278   # Blockquote (leading >)
279   } else if ( match( block, /^> /) ) {
280     match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match(block, /$/);
281     len = RLENGTH; st = RSTART;
282     return "<blockquote>\n" _block( gensub( /(^|\n)> /, "\n", "g", substr(block, 1, st - 1) ) ) "</blockquote>\n\n" \
283            _block( substr(block, st + len) );
284
285   # Line Blocks (pandoc)
286   } else if ( match(block, /^\| [^\n]*(\n|$)(\| [^\n]*(\n|$)|[ \t]+[^\n[:space:]][^\n]*(\n|$))*/) ) {
287     len = RLENGTH; st = RSTART;
288     code = substr(block, 1, len);
289     gsub(/\n[[:space:]]+/, " ", code);
290     gsub(/\n\| /, "\n", code);
291     gsub(/^\| |\n$/, "", code);
292     return "<div class=\"line-block\">" gensub(/\n/, "<br />\n", "g", inline( code )) "</div>\n" \
293            _block( substr( block, len + 1) );
294
295   # Indented Code Block
296   } else if ( match(block, /^(    |\t)[^\n]+(\n|$)((    |\t)[^\n]+(\n|$)|[ \t]*(\n|$))*/) ) {
297     len = RLENGTH; st = RSTART;
298     code = substr(block, 1, len);
299     gsub(/(^|\n)(    |\t)/, "\n", code);
300     gsub(/^\n|\n+$/, "", code);
301     return "<pre><code>" HTML( code ) "</code></pre>\n" \
302            _block( substr( block, len + 1 ) );
303
304   # Fenced Code Block (pandoc)
305   } else if ( match( block, /^(~~~+|```+)/ ) ) {
306     guard = substr( block, 1, RLENGTH );
307     code = gensub(/^[^\n]+\n/, "", 1, block);
308     if ( match(code, "(^|\n)" guard "+(\n|$)" ) ) {
309       len = RLENGTH; st = RSTART;
310       return "<pre><code>" HTML( substr(code, 1, st - 1) ) "</code></pre>\n" \
311              _block( substr( code, st + len ) );
312     } else {
313       match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
314       len = RLENGTH; st = RSTART;
315       return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
316              _block( substr(block, st + len) );
317     }
318
319   # Unordered list
320   } else if ( match( block, "^ ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
321                             "(([ \t]*\n)* ? ? ?[-+*][ \t]+[^\n]+(\n|$)" \
322                             "|([ \t]*\n)*( ? ? ?\t|  +)[^\n]+(\n|$)" \
323                             "|[^\n]+(\n|$))*" ) ) {
324   list = substr( block, 1, RLENGTH);
325   block = substr( block, RLENGTH + 1);
326   indent = length( gensub(/[-+*][ \t]+[^\n]+.*$/, "", 1, list) );
327
328   gsub("(^|\n) {0," indent "}", "\n", list);
329   return "\n<ul>\n" _list( substr(list, 2) ) "</ul>\n" _block( block );
330
331   # Ordered list
332   } else if ( match( block, "^ ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
333                             "(([ \t]*\n)* ? ? ?([0-9]+|#)\\.[ \t]+[^\n]+(\n|$)" \
334                             "|([ \t]*\n)*( ? ? ?\t|  +)[^\n]+(\n|$)" \
335                             "|[^\n]+(\n|$))*" ) ) {
336   list = substr( block, 1, RLENGTH);
337   block = substr( block, RLENGTH + 1);
338   indent = length( gensub(/([0-9]+|#)\.[ \t]+[^\n]+.*$/, "", 1, list) );
339
340   gsub("(^|\n) {0," indent "}", "\n", list);
341   return "\n<ol>\n" _list( substr(list, 2) ) "</ol>\n" _block( block );
342
343   # First Order Heading
344   } else if ( match( block, /^[^\n]+\n===+(\n|$)/ ) ) {
345     len = RLENGTH;
346     return "<h1>" inline( gensub( /\n.*$/, "", "g", block ) ) "</h1>\n\n" \
347            _block( substr( block, len + 1 ) );
348
349   # Second Order Heading
350   } else if ( match( block, /^[^\n]+\n---+(\n|$)/ ) ) {
351     len = RLENGTH;
352     return "<h2>" inline( gensub( /\n.*$/, "", "g", block ) ) "</h2>\n\n" \
353            _block( substr( block, len + 1) );
354
355   # Nth Order Heading
356   } else if ( match( block, /^#{1,6}[[:space:]]*[^\n]+([[:space:]]*#*)(\n|$)/ ) ) {
357     len = RLENGTH;
358     hlvl = length( gensub( /^(#{1,6}).*$/, "\\1", "g", block ) );
359     htxt = gensub( /[[:space:]]*#*$/, "", "1", gensub( /^#{1,6}[[:space:]]*([^\n]+)([[:space:]]*#*)\n.*$/, "\\1", "g", block ) )
360     return "<h" hlvl ">" inline( htxt ) "</h" hlvl ">\n\n" \
361            _block( substr( block, len + 1) );
362
363   # Plain paragraph
364   } else {
365     match( block, /(^|\n)[[:space:]]*(\n|$)/ ) || match( block, /$/ );
366     len = RLENGTH; st = RSTART;
367     return "<p>" inline( substr(block, 1, st - 1) ) "</p>\n" \
368            _block( substr(block, st + len) );
369   }
370 }
371
372 function _list( block, last, LOCAL, p) {
373   if ( ! length(block) ) return "";
374   gsub(/^([-+*]|[0-9]+\.|#\.)(  ? ? ?|\t)/, "", block)
375
376   # slice next list item from input
377   if ( match( block, /\n([-+*]|[0-9]+\.|#\.)[ \t]+[^\n]+/) ) {
378     p = substr( block, 1, RSTART);
379     block = substr( block, RSTART + 1);
380   } else {
381     p = block; block = "";
382   }
383   sub( /\n +([-+*]|[0-9]+\.|#\.)/, "\n&", p );
384
385   # if this should be a paragraph item
386   # either previous item (last) or current item (p) contains blank lines
387   if (match(last, /\n[[:space:]]*\n/) || match(p, /\n[[:space:]]*\n/) ) {
388     last = p; p = _block(p);
389   } else {
390     last = p; p = _block(p);
391     sub( /^<p>/, "", p );
392     sub( /<\/p>\n/, "", p );
393   }
394   sub( /\n$/, "", p );
395   return "<li>" p "</li>\n" _list( block, last );
396 }
397
398 BEGIN {
399   # Global Vars
400   file = ""; rl_href[""] = ""; rl_title[""] = "";
401
402   # Buffering of full file ist necessary, e.g. to find reference links
403   while (getline) { file = file $0 "\n"; }
404
405   # Fill array of reference links
406   f = file; rl_id;
407   re_reflink = "(^|\n) ? ? ?\\[([^]\n]+)\\]: ([^ \t\n]+)(\n?[ \t]+(\"([^\"]+)\"|'([^']+)'|\\(([^)]+)\\)))?(\n|$)";
408   # /(^|\n) ? ? ?\[([^]\n]+)\]: ([^ \t\n]+)(\n?[ \t]+("([^"]+)"|'([^']+)'|\(([^)]+)\)))?(\n|$)/
409   while ( match(f, re_reflink ) ) {
410     rl_id           = gensub( re_reflink, "\\2", 1, substr(f, RSTART, RLENGTH) );
411     rl_href[rl_id]  = gensub( re_reflink, "\\3", 1, substr(f, RSTART, RLENGTH) );
412     rl_title[rl_id] = gensub( re_reflink, "\\5", 1, substr(f, RSTART, RLENGTH) );
413     f = substr(f, RSTART + RLENGTH);
414     rl_title[rl_id] = substr( rl_title[rl_id], 2, length(rl_title[rl_id]) - 2 );
415     if ( rl_href[rl_id] ~ /<.*>/ ) rl_href[rl_id] = substr( rl_href[rl_id], 2, length(rl_href[rl_id]) - 2 );
416   }
417   # Clear reflinks from File
418   while( gsub(re_reflink, "\n", file ) );
419   # for (n in rl_href) { debug(n " | " rl_href[n] " | " rl_title[n] ); }
420
421   # Run Block Processing -> The Actual Markdown!
422   printf "%s", _block( file );
423 }