#!/bin/sed -rf
#-- a script to make a html table of contents from a plain text
#-- 'book' file. This script requires gnused 4+
#--
#----------------------------
# start the table of contents
1 i
#-- remove spaces on blank lines
s/^[[:space:].]+$//g;
#-- delete lines which are not section headings
/^[[:upper:] .]+$/ !{ d; b; }
#-- remove leading and trailing space
s/^[[:space:]]+//g;
s/[[:space:]]+$//g;
#-- Convert section headings to a list of hyperlinks
#-- Section headings are in all capitals
/^[[:upper:] ]+$/ {
s/[[:space:]]+$//g;
s/^.*$/\L&/g;
H;
y/ /-/;
s/.*//;
x; s/[[:alnum:]]+/\u&/g; #-- make capital case (eg 'the title' -> 'The Title')
x;
G;
s@.*@&@;
s@\n@@g;
/22/ { s/22//; s/^/<\/ul>\n/; }
/11/ { s/11//; }
x; s/.*//; s/^/11/; x; #-- put a flag in the hold space
}
#-- convert subsection headings (eg: TITLE ....)
/^[[:upper:] .]+$/ {
s/\.//g; #-- dots mark subsection headings
s/[[:space:]]+$//g;
s/^.*$/\L&/g; #-- lower case the whole line
H; #-- add to the hold space
y/ /-/; #--
s/.*//;
x; s/[[:alnum:]]+/\u&/g; #-- make capital case (eg 'the title' -> 'The Title')
x;
G;
s@.*@&@;
s/\n//g; #-- remove newlines
/11/ { s/11//; s/^/\n/; }
/22/ { s/22//; } #-- delete the flag in the workspace
x; s/.*//; s/^/22/; x; #-- put a 'subsection' flag in the hold space
}