#!/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 "<PRE>\n$datOut</PRE>" \ >> "$tmp/${phile}.$intro" else echo "<PRE>\n$datOut\n</PRE>$hr" \ >> "$tmp/${phile}.$intro" fi datOut='' # # Flush the output buffer before changing # the subject. fi intro="main" aname="$(scrubFileName "$line")" echo "<H2><A NAME=\"$aname\" HREF=\"#dx-$phile\"\n\n\n >$line</A></H2>" \ >> "$tmp/${phile}.$intro" # H2 header for this section. echo " <LI><A HREF=\"#$aname\">$line</A></LI>" \ >> "$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 "<PRE>\n$datOut</PRE>" \ >> "$tmp/${phile}.$intro" else echo "<PRE>\n$datOut\n</PRE>$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# The line above is escaped for HTML viewers.\n" datOut="$datOut# Active configuration item follows:\n" datOut="$datOut$line\n" datOut="$datOut# -->\n" ;; *) datOut="$datOut$line\n" ;; esac done unset $lynx_cfg if [ "$datOut" != "" ] then echo "<PRE>\n$datOut\n</PRE>$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

Primary configuration headings:

$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.

" >> "$idx" echo "Writing document info..." echo "${dtd}About $nom

$nom

$(head -n 44 "$src" | tail -n '+2' | tr '#' ' ')
$hr

Source || License || Back to $idx

$hr
$(tail -n 5 "$src" | tr '#' ' ')
" > "${nom}-about.html" cat "$src" > "${nom}-script.txt" # Include the script in its own documentation. echo "Done." # Dedicated to the memory of Cheetah the cat, and her invaluable # help with the Bash scripting language. [Okay, 31337 H4x0R k!tty # computing tip here: What she would actually do was fall asleep on # my lap, thereby encouraging me to sit still for extended periods # of time. It helped, and I'm grateful. I really miss her.]