#! /bin/sed -nf
#
# Get the title of an HTML document.
# Look first for TITLE tag, then fall back on first Heading tag. Otherwise
# produce nothing.
#
# Casper Boden-Cummins , 1998
#
/<[Tt][Ii][Tt][Ll][Ee]/ {
:title
# read up to closing tag
/<\/[Tt][Ii][Tt][Ll][Ee]>/ !{
N
b title
}
# strip leading/trailing whitespace
# Mario Niebaum
s/^.*<[tT][iI][tT][lL][eE]>[ ]*//
s/[ ]*<\/[tT][iI][tT][lL][eE]>.*$//
# strip HTML tags and print
b print
}
/<[Hh][0-7]/ {
:heading
# lowercase heading tags
s/\(<\/\{0,1\}\)[Hh]\([0-7]\)/\1h\2/g
# strip characters up to opening tag
G
s/\)\(.*\)\(.\)$/\3\1\2/
s/.*\n/[ ]*//
s/[ ]*<\/[Hh][0-7]>.*$//
# clean up and print
b print
}
# nothing found, so continue to next line
b
# print:
# remove embedded tags and leading/trailing whitespace from
# pattern space, print result and quit
:print
# reduce leading/trailing whitespace around newlines
s/[ ]*\n[ ]*/ /g
# convert embedded TAGS to whitespace
s/[ ]*<\/*[a-zA-Z0-9]\{1,\}>[ ]*/ /g
# remove non-printable characters
s/[^ -;=?-~]//g
# strip leading/trailing whitespace from line
s/^[ ]*\(.*[^ ]\)[ ]*$/\1/
# print and quit
p
q