aboutsummaryrefslogtreecommitdiff
path: root/htindex.sh
diff options
context:
space:
mode:
Diffstat (limited to 'htindex.sh')
-rwxr-xr-xhtindex.sh77
1 files changed, 77 insertions, 0 deletions
diff --git a/htindex.sh b/htindex.sh
new file mode 100755
index 0000000..54ca8d8
--- /dev/null
+++ b/htindex.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+
+# index -- add ids to headings and print index to stderr
+
+usage() { echo "usage: $0 [-s] [file ...]" 1>&2; exit 1; }
+
+flags=
+case "$1" in
+-*) [ x"$1" = x-s ] && { flags=1; shift; } || usage ;;
+*) ;;
+esac
+export flags
+
+source() {
+ [ $# -gt 0 ] && cat "$@" || cat
+}
+
+source "$@" | awk '
+ /<h[0-9]>.*<\/h[0-9]/ {
+ heading($0)
+ next
+ }
+ /<h[0-9]>/ {
+ open = 1
+ s = $0
+ next
+ }
+ open && /<\/h[0-9]>/ {
+ open = 0
+ s = s "\n" $0
+ heading(s)
+ next
+ }
+ open { s = s " " $0; next }
+ { print }
+
+ function heading(s) {
+ i = s
+ sub("^.*<h[0-9]>", "", i)
+ sub("</h[0-9]>.*$", "", i)
+ i = toascii(i)
+ sub("^<h[0-9]", "& id=\"" i "\"", s)
+ print s
+ if (!ENVIRON["flags"]) { # if not silent mode
+ sub("^<h", "", s)
+ sub(" id=\"", " ", s)
+ sub("\">", " ", s)
+ sub("<\\/h[0-9]>$", "", s)
+ print s > "/dev/stderr"
+ }
+ }
+
+ function toascii(s) {
+ s = tolower(s)
+ #gsub("[ÀÁÂÃÄÅàáâãäå]", "a", s)
+ #gsub("[Ææ]", "ae", s)
+ #gsub("[Çç]", "c", s)
+ #gsub("[Ðð]", "dh", s)
+ #gsub("[ÈÉÊËèéêë]", "e", s)
+ #gsub("[ÌÍÎÏìíîï]", "i", s)
+ #gsub("[Ññ]", "n", s)
+ #gsub("[ÒÓÔÕÖØòóôõöø]", "o", s)
+ #gsub("[ß]", "ss", s)
+ #gsub("[Þþ]", "th", s)
+ #gsub("[ÙÚÛÜùúûü]", "u", s)
+ #gsub("[×]", "x", s)
+ #gsub("[Ýýÿ]", "y", s)
+ gsub("[/]", "-", s)
+ gsub("[ \n]+\\+[ \n]+", "+", s)
+ gsub("[ \n]+=[ \n]+", "=", s)
+ gsub("[ \n]+", "-", s)
+ gsub("[^-=+a-z0-9_ ]", "", s)
+ gsub("-\\+-", "+", s)
+ gsub("--*", "-", s)
+ return s
+ }
+'