diff options
1 files changed, 40 insertions, 8 deletions
| -rw-r--r-- | md-parser.awk | 48 |
diff --git a/md-parser.awk b/md-parser.awk index 4d11db8..fc947bc 100644 --- a/md-parser.awk +++ b/md-parser.awk @@ -10,7 +10,7 @@ # [x] Strikethrough ~~...~~ # [x] Superscript ^...^ # [x] Subscript ~...~ -# [ ] Footnote [^...] (with user customizations) +# [.] Footnote [^...] (with user customizations) # [x] Task list - [ ] (readonly) # [x] Definition lists # [x] Single line @@ -28,6 +28,8 @@ # begins with "__" - internal, do not use BEGIN { + footnote_index = 1 + ## This is used to convert email autolinks into a series of hex values ## Escaped output of: awk 'BEGIN {for (i = 32; i <= 126; ++i){ printf("%c", i) } }' ascii = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~" @@ -142,7 +144,7 @@ STATE_isset(STATE_CODEBLOCK) { } #-- Link definition (ignore) --# -/^\[[^\]]+\]:[ \t]+.*/ { +/^\[[^\]]+\]:/ { next } @@ -393,6 +395,38 @@ STATE_isset(STATE_DEFINITION_LIST) { STATE_unset(STATE_PARAGRAPH) } +#-- Footnote references --# +/\[\^[^\]]+\]/ { + while (match($0, /\[\^[^\]]+\]/)) { + footnote_name = substr($0, RSTART + 2, RLENGTH - 3) + if (!footnote[footnote_name]) { + sub_match("<sup>?" footnote_name "?</sup>") + } + else { + fn = footnote[footnote_name] + delete footnote[footnote_name] + ## Escapes for emphasis elements + gsub(/_/, "\\_", footnote_name) + gsub(/\*/, "\\*", footnote_name) + gsub(/~/, "\\~", footnote_name) + gsub(/`/, "\\`", footnote_name) + gsub(/\^/, "\\^", footnote_name) + + footnote_short = substr(fn, 0, 97) + if (length(fn) > 97) { + footnote_short = footnote_short "..." + } + + sub_match("<sup id=\"ref-" footnote_name "\">" \ + "<a href=\"#footnote-" footnote_name "\" title=\"" footnote_short "\">" \ + (footnote_index++) "</a></sup>") + + footnote_tag = footnote_tag ORS "<li id=\"footnote-" footnote_name "\">" \ + "<p>" fn " <a href=\"#ref-" footnote_name "\">^</a></p></li>" + } + } +} + #-- Emphasis --# { ## Make sure <em> is on the inside for long chains @@ -443,12 +477,6 @@ function gsurround(brace, left, right, blen) { } } -#-- Footnote references --# -/\[\^[^\]]+\]/ { - while (match($0, /\[\^[\]]+\]/)) { - } -} - #-- Links --# /!?\[[^\]]*\] ?[(\[][^)\]]*[\])]/ { while (match($0, /!?\[[^\]]*\] ?[(\[][^)\]]*[\])]/)) { @@ -564,6 +592,10 @@ END { if (!empty(P1)) { print P1 } print if (!empty(N1)) { print N1 } + + if (footnote_tag) { + print ORS "<div class=\"footnotes\"><hr><ol>" footnote_tag "</ol></div>" + } } # PNappnd(value) |
