Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
2 files changed, 51 insertions, 8 deletions
-rw-r--r--md-meta.awk11
-rw-r--r--md-parser.awk48
diff --git a/md-meta.awk b/md-meta.awk
index fea34c3..e4ef2bf 100644
--- a/md-meta.awk
+++ b/md-meta.awk
@@ -6,6 +6,9 @@ function erit(id, href, title) {
print "reference[\"" id "\", \"href\"]=\"" href "\""
print "reference[\"" id "\", \"title\"]=\"\\\"" title "\\\"\""
}
+function efit(id, contents) {
+ print "footnote[\"" id "\"]=\"" contents "\""
+}
title != "" && stopdesc == 0 && /^[A-Za-z]/ {
startedGathering = 1
@@ -53,6 +56,14 @@ title == "" && /^# / {
erit(id, href, reftitle)
}
+/^\[\^[^\]]+\]:/ {
+ match($0, /^\[\^[^\]]+\]:/)
+ footnote_name = substr($0, 3, RLENGTH - 4)
+ footnote_def = substr($0, RSTART + RLENGTH)
+ sub(/^[ \t]*/, "", footnote_def)
+ efit(footnote_name, footnote_def)
+}
+
END {
emit("title", title)
emit("description", desc)
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(/_/, "\\&#95;", footnote_name)
+ gsub(/\*/, "\\&#42;", footnote_name)
+ gsub(/~/, "\\&#126;", footnote_name)
+ gsub(/`/, "\\&#96;", footnote_name)
+ gsub(/\^/, "\\&#94;", 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 "&emsp;<a href=\"#ref-" footnote_name "\">&#94;</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)