diff --git a/bin/anchors.awk b/bin/anchors.awk new file mode 100755 index 0000000..a021a55 --- /dev/null +++ b/bin/anchors.awk @@ -0,0 +1,35 @@ +#!/usr/bin/gawk -f + +# Output all eligible anchors in the document + +BEGIN { + # /contents is OK as a page, but will not be picked up automatically + print "/contents" + name = "" +} + +/(^# |^## |^### ).* $/ { + path = gensub(/^#+ .* $/, "\\1", "1") + print path +} + +# Headings +/^#/ { + if ($0 ~ / $/) { + name = gensub(/^#+ (.*) $/, "\\1", "1") + } else { + name = gensub(/^#+ (.*)$/, "\\1", "1") + } + name = tolower(name) + gsub(/ /, "-", name) + gsub(/[^a-z0-9_-]/, "", name) + + print path "#" name +} + +# Explicit anchors - only one per line allowed, at the start of the line +/^<\/a>$/ { + name = gensub(/^<\/a>$/, "\\1", "1") + print path "#" name +} +