From 7ddce59190f8cb35a4f5108114d8575dd868940e Mon Sep 17 00:00:00 2001 From: Syndamia Date: Mon, 3 Mar 2025 16:01:06 +0200 Subject: feat: Preliminary implementation of footnotes Markdown isn't parsed in the definition --- md-parser.awk | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) (limited to 'md-parser.awk') 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("?" footnote_name "?") + } + 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("" \ + "" \ + (footnote_index++) "") + + footnote_tag = footnote_tag ORS "
  • " \ + "

    " fn " ^

  • " + } + } +} + #-- Emphasis --# { ## Make sure 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 "

      " footnote_tag "
    " + } } # PNappnd(value) -- cgit v1.3.1-23-gd82d