diff options
author | John Ankarström <john@ankarstrom.se> | 2021-01-31 12:49:22 +0000 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-01-31 12:49:22 +0000 |
commit | dfb9b24814264abb429e4d289685654c469ff362 (patch) | |
tree | 9e4686fc56cfbb92ded6e514204210775da794b7 /index | |
parent | 2f7147aa05cbdcb8b1b2470877dffc663cb7a83e (diff) | |
download | em-dfb9b24814264abb429e4d289685654c469ff362.tar.gz |
Add index script
Index automatically adds appropriate ids to HTML headings
and prints an index of all headings on stderr (unless given the
-s, silent, option).
Diffstat (limited to 'index')
-rwxr-xr-x | index | 74 |
1 files changed, 74 insertions, 0 deletions
@@ -0,0 +1,74 @@ +#!/bin/rc -e + +# index -- add ids to headings and print index to stderr + +flagfmt=s +args='[file ...]' + +if(! ifs=() eval `{aux/getflags $*}){ + aux/usage + exit usage +} + +fn source{ + if(! ~ $#* 0) + cat $* + if not + 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("[^-a-z_ ]", "", s) + gsub("[ ]+", "-", s) + return s + } +' |