Utility to output valid internal anchors

This commit is contained in:
Ben Edgington
2021-11-16 17:39:52 +00:00
parent c48721f3f4
commit f90707042d

35
bin/anchors.awk Executable file
View File

@@ -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 id=".*"><\/a>$/ {
name = gensub(/^<a id="(.*)"><\/a>$/, "\\1", "1")
print path "#" name
}