#!/bin/sh
version="rman_html_split V1.0 (c) 1998 Robert K. Moniot"
Usage(){
myname=`echo $0 | sed -e 's,.*/,,'`
cat <<EOF
Usage: $myname [ options ] [ file ]
Breaks up rman HTML output in file (or standard input) into separate
section files, placing them into a directory.
Options:
-d | --html-dir dir Place output into directory dir (default = html).
-m | --merge -t | --toc-first Link index.html to table of contents instead
of to sect0.html.
-T | --title "title" Use "title" as title on top of each page
instead of rman's title. "title" may be empty.
-h | --help Print synopsis and exit.
-V | --version Print version information and exit.
EOF
exit 0
}
Version(){
echo "$version"
exit 0
}
do_header(){
cat <<EOF
<HTML>
<HEAD>
<TITLE>$*</TITLE>
</HEAD>
<BODY>
EOF
do_title
}
do_title(){
if [ -n "$title" ]
then
echo "<H2>$title</H2>"
fi
}
do_href_toc(){
cat <<EOF
<A HREF="toc.html">Table of Contents</A><P>
EOF
}
do_href_prev(){
echo "<P>Previous: <A HREF=\"$1.html\">$2</A><HR><P>"
}
do_footer(){
cat <<EOF
</BODY></HTML>
EOF
}
do_href_next(){
sed -e '$s,^.*<A NAME="\([^"]*\)" HREF=[^>]*>\([^<]*\).*$,<P><HR><P>Next: <A HREF="\1.html">\2</A>,' \
-e '$s,<A NAME="\(toc[^"]*\)",Next: <A HREF="\1.html",'
}
change_hrefs(){
sed -e 's,HREF="#toc\([0-9][0-9]*\)",HREF="toc.html#toc\1",g' \
-e 's,HREF="#sect\([0-9][0-9]*\)",HREF="sect\1.html#sect\1",g' \
-e 's,HREF="#\(toc\)",HREF="\1.html",g'
}
htmldir="html"
mergesect="1"
while [ $do
case $1 in
-d | --html-dir)
shift
if [ $ then
htmldir="$1"
shift
fi
;;
-m | --merge)
shift
if [ $ then
mergesect="$1"
shift
fi
case $mergesect in
[0-9])
;;
*)
echo "ERROR: merge section must be a digit from 0 to 9."
Usage
;;
esac
;;
-t | --toc-first)
shift
tocfirst="t"
;;
-T | --title)
shift
if [ $ then
title="$1"
shift
fi
if [ -z "$title" ] then
notitle="t"
fi
;;
-h | --help)
Usage
;;
-V | --version)
Version
;;
-*)
echo "Unrecognized option: $1"
Usage
;;
*)
break
;;
esac
done
if [ $then
Usage
fi
if [ $then
if [ ! -r "$1" ]
then
echo "ERROR: Cannot open file $1."
exit 1
fi
fi
filename="/tmp/rman_html_split_$$.html"
trap "rm -f $filename" 0
sed -e ':top' \
-e '/^<H[1-9]><A NAME="sect.*[^>]$/N' \
-e '/^<LI><A NAME="toc.*[^>]$/N' \
-e 's/\n/ /' \
-e 't top' $1 > $filename
if [ -z "$title" -a -z "$notitle" ]
then
title=`sed -e '/^<TITLE>/q' $filename |
sed -n -e 's,^<TITLE>\([^<]*\).*$,\1,p'`
fi
allsects=`sed -n 's,^.*<A NAME="\(sect[0-9][0-9]*\)".*$,\1,p' $filename`
mergesects=`echo $allsects | \
awk '{for(i=1; i<=NF && i<'"$mergesect"+2'; i++) print \$i;}'`
sectlist=`echo $allsects |
awk '{for(i='"$mergesect"'+2; i<=NF; i++) print \$i;}'`
if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
ac_n= ac_c='
' ac_t=' '
else
ac_n=-n ac_c= ac_t=
fi
else
ac_n= ac_c='\c' ac_t=
fi
if [ -d $htmldir ]
then
echo "Re-using directory $htmldir."
else
echo "Creating directory $htmldir..."
if mkdir $htmldir
then
true
else
echo "Failed!"
exit 1
fi
fi
echo "Creating section pages..."
nextsect=`echo $sectlist | awk '{print $1;}'`
echo $ac_n $mergesects "$ac_c"
(do_title ;
sed -n -e "1,/^<H[23]><A NAME=\"$nextsect\"/p" $filename | \
do_href_next | \
change_hrefs ;
do_footer) > $htmldir/sect0.html
prevsect="sect$mergesect"
prevtext=`sed -n 's,^<H[23]><A NAME="'$prevsect'" HREF="#toc[0-9][0-9]*">\([^<]*\).*$,\1,p' $filename | sed -e 's/ *$//'`
for sect in $sectlist;
do
echo $ac_n "$sect $ac_c"
headtext=`sed -n 's,^<H[23]><A NAME="'$sect'" HREF="#toc[0-9][0-9]*">\([^<]*\).*$,\1,p' $filename | sed -e 's/ *$//'`
(do_header $headtext ;
do_href_toc ;
do_href_prev "$prevsect" "$prevtext" ;
sed -n -e '/<A NAME="'$sect'"/,/<A NAME=/p' $filename |
do_href_next |
change_hrefs ;
do_footer) > $htmldir/$sect.html
prevsect="$sect"
prevtext="$headtext"
done
echo "toc"
(do_header Table of Contents ;
sed -n -e '/<A NAME="toc">/,$p' $filename | \
change_hrefs ;
) > $htmldir/toc.html
echo "Doing symlinks..."
cd $htmldir
rm -f index.html
if [ -z "$tocfirst" ]
then
echo "Linking index.html -> sect0.html"
ln -s sect0.html index.html
else
echo "Linking index.html -> toc.html"
ln -s toc.html index.html
fi
for sect in $mergesects
do
if [ "$sect" != "sect0" ]
then
echo "Linking $sect.html -> sect0.html"
rm -f $sect.html
ln -fs sect0.html $sect.html
fi
done
echo "Done."