#!/bin/sh
# html_cfg_in1.sh 0.0.3, (c) 2008 by Patrick Boylan
# not to be confused with cfg2html.pl .
#
# Small variation; this one pours everything into one big HTML file.
#
# You can modify, redistribute, and pretty much do whatever you want
# with this script under the terms of the GNU General Public License,
# Version 2, or the new one if you prefer. If you didn't get a copy of
# the GNU GPL with this program, look in /usr/share/common-licenses/ on
# Debian-based systems, or http://gnu.org/licenses/gpl-2.0.txt . If
# the URL should fail for some reason, they have a search engine.
#
# Again, this shouldn't be confused with Vlad Harchev's cfg2html.pl ,
# which produces much nicer HTML anyway. My goals are different. I
# wanted a hyperlinked config file which is easily navigated from
# within Lynx -- so, via the e)dit command, Lynx becomes its own best
# configuration editor. You should be able to find the settings you
# want to change, and change them more easily this way.
#
# With that in mind, I wanted the results to look as similar as
# possible in a web browser or text editor. The plaintext view might
# not be pretty, but I've tried to keep it legible.
# CHANGES:
# 0.0.3 * I *think* it's a POSIX-compliant /bin/sh script now.
# * DOCTYPE declaration was omitted, because I wasn't sure
# what DTD to put it under but the WDG validator validates
# it now.
# * Fixed some of those multi-line echoes, so they won't
# screw up the indentation [purely cosmetic; also for
# legibility].
#
# 0.0.2 * Tried doing more in memory, less with temp-files, but
# that didn't seem to speed things up any.
# * Rearranged the `case "$foo" in' thing so the most likely
# possiblities are tested first. Went from a five minute
# job to like, a minute on my old 233 mHz Compaq Armada.
# * Went back to mostly temp-files.
# * Commented the hell out of it.
#
# 0.0.1 * You mean, it actually did something this time? Whoah...
nom="$(basename "$0")"
tmp="lynx_cfg_in1_tmp"
idx="lynx.cfg.html"
phile="lynx.cfg"
intro="html"
hr='\n
'
dtd='
'
how2 () {
echo "
Usage: $nom /path/to/lynx.cfg [/path/to/output_dir/]
If no output directory is specified, the current directory is used.
Either path may be relative or absolute, but the output directory
must already exist.
The resulting $idx file can be used as your lynx.cfg . If
you only want a browsable HTML version of lynx.cfg to look at, it
should work that way too. But if that's what you're trying to do,
I recommend using Vlad Harchev's excellent cfg2html.pl instead.
"
}
if [ "$1" = "" ] || [ "$1" = "-h" ] || [ "$1" = "-help" ] || [ "$1" = "--help" ]
then
how2
exit 0
fi
if [ -f "$1" ]
then
lynx_cfg="$(cat "$1")"
if [ "$lynx_cfg" = "" ]
then
echo "That Didn't Work."
how2
exit 1
fi
else
echo "\`$1' is not a file."
how2
exit 1
fi
# Some half-hearted idiot-proofing.
if [ "$2" = "" ]
then
src="$0"
dest="`pwd`"
elif [ -d "$2" ]
then
if [ "$(echo "$0" | grep '^\./')" != "" ]
then
src="`pwd`/$0"
elif [ "$(echo "$0" | grep '^/')" != "" ]
then
src="$0"
elif [ -x "`pwd`/$0" ]
then
src="`pwd`/$0"
# Having `.' in your $PATH is a BAD idea,
# but it could happen.
fi
cd "$2"
dest="`pwd`"
else
echo "\`$2' is not a directory."
how2
exit 1
fi
# Still half-hearted.
echo "Loaded \`$1', sorting lines..."
# Declaring a few useful functions...
scrubHeader () {
# Strip the leading `.h1' and `.h2' from input.
echo "$1" | cut --delimiter=' ' --fields='2-222'
}
scrubFileName () {
# Convert the current line into a sensible filename
# [eg, all lowercase, no spaces, no funky puctuation].
echo "$1" | tr '_' ' ' | tr --delete '[:punct:]' | \
tr -s ' ' '_' | tr '[:upper:]' '[:lower:]'
}
scrubLine () {
# Use an HTML escape sequence if absolutely necessary.
scrubbedLine="$1"
while [ "$(echo "$scrubbedLine" | grep '<' -)" != "" ]
do
scrubbedLine="$(echo "$scrubbedLine" | sed 's/\</')"
# Any way to make sed do its own recursion?
# This is a bit silly.
done
echo "$scrubbedLine"
}
num=0
tmp2="$tmp"
while [ -e "$tmp2" ]
do
tmp2="${tmp}.$num"
num=$(($num + 1))
# Just making sure the temp directory *does_not*
# already exist, because it will be deleted later.
done
tmp="$tmp2"
mkdir "$tmp"
unset $num
unset $tmp2
echo "Using \`$dest/$tmp' for temp-files..."
IFS='
'
for line in $lynx_cfg
do
case "$line" in
\#*\<*) datOut="$datOut$(scrubLine "$line")\n"
;;
\#*) datOut="$datOut$line\n"
;;
.*) case "$line" in
.h2\ *) line="$(scrubHeader "$line")"
if [ "$datOut" != "" ]
then
if [ "$intro" = "intro" ]
then
echo "\n$datOut
" \
>> "$tmp/${phile}.$intro"
else
echo "\n$datOut\n
$hr" \
>> "$tmp/${phile}.$intro"
fi
datOut=''
#
# Flush the output buffer before changing
# the subject.
fi
intro="main"
aname="$(scrubFileName "$line")"
echo "" \
>> "$tmp/${phile}.$intro"
# H2 header for this section.
echo " $line" \
>> "$tmp/${phile}.lst"
# Sub-headings list for the main index.
;;
.h1\ *) line="$(scrubHeader "$line")"
lsH1="$lsH1$line\n"
#
# Store .h1 headers for future reference.
if [ "$datOut" != "" ]
then
if [ "$intro" = "intro" ]
then
echo "\n$datOut
" \
>> "$tmp/${phile}.$intro"
else
echo "\n$datOut\n
$hr" \
>> "$tmp/${phile}.$intro"
fi
datOut=''
#
# Flush the output buffer before changing
# the subject.
fi
intro="intro"
#
# Any text which occurs after the .h1 line, but
# before any .h2 lines, will be an introduction
# to this section.
phile="$(scrubFileName "$line")"
#
# Further output under this .h1 header will go into
# `${phile}.html'.
;;
.ex) datOut="$datOut#\n# Example:\n"
;;
.ex\ *) datOut="$datOut#\n# Examples:\n"
;;
*) datOut="$datOut#\n"
# Missed .nf and .fi, but with no formatting
# to begin with, there's no need to turn it
# off.
# No need to turn it back on again, either.
;;
esac
;;
*\<*) datOut="$datOut$(scrubLine "$line") \n"
;;
*) datOut="$datOut$line\n"
;;
esac
done
unset $lynx_cfg
if [ "$datOut" != "" ]
then
echo "\n$datOut\n
$hr" \
>> "$tmp/${phile}.$intro"
unset $datOut
#
# Don't forget to flush.
fi
# Okay, so... Now that we've dissected lynx.cfg ,
# let's try to put it back together again.
#
# Won't this be fun...
echo "Split \`$1' into temp-files. Re-assembling..."
lsH1="$(echo "$lsH1" | sort -u)"
echo "Generating file, \`$idx'..."
echo "${dtd}lynx.cfg file.
lynx.cfg file.
" > "$idx"
cat "$tmp/$idx" >> "$idx"
#
# Opening lines and intro for lynx_cfg.html .
echo "\n
" \
> "$tmp/${idx}.toc"
#
# Begin table of contents.
echo "\n\n" \
> "$tmp/${idx}.dx"
#
# Begin detailed index.
echo "\nContent:
\n" \
> "$tmp/$idx"
#
# Note that we're still using a temp-file for this
# [no ToC or index in main file yet, and these should
# go first].
for line in $lsH1
do
phile="$(scrubFileName "$line")"
echo " - $line
" \
>> "$tmp/${idx}.toc"
#
# Add this header to the table of contents.
echo "" \
>> "$tmp/${idx}.dx"
if [ -f "$tmp/${phile}.intro" ]
then
cat "$tmp/${phile}.intro" \
>> "$tmp/${idx}.dx"
fi
# Introduction for this section.
if [ -f "$tmp/${phile}.lst" ]
then
{
echo ""
echo "$(cat "$tmp/${phile}.lst")"
echo "
$hr"
} >> "$tmp/${idx}.dx"
fi
# Those .h2 sub-headings go here.
echo "" \
>> "$tmp/$idx"
#
# Add a new header for this section of lynx_cfg.html
# [the temp-file, not the real one yet].
if [ -f "$tmp/${phile}.intro" ]
then
cat "$tmp/${phile}.intro" \
>> "$tmp/$idx"
fi
# Re-use the introduction for this section.
if [ -f "$tmp/${phile}.lst" ]
then
{
echo
echo "Sub-headings under $line:
"
echo ""
echo "$(cat "$tmp/${phile}.lst")"
echo "
$hr"
} >> "$tmp/$idx"
fi
# Those .h2 sub-headings go here, too.
if [ -f "$tmp/${phile}.main" ]
then
cat "$tmp/${phile}.main" \
>> "$tmp/$idx"
fi
# Dump the main text into the main file [temp].
done
cat "$tmp/${idx}.toc" >> "$idx"
echo "
$hr" >> "$idx"
cat "$tmp/${idx}.dx" "$tmp/$idx" >> "$idx"
echo "Removing temp-files, $dest/$tmp ..."
rm -rf "$dest/$tmp"
echo "Add closing to finish..."
echo "
HTML generated on
$(date)
by $nom -
For Free.