script to dynamically generate the hints list

Olivier Pérès olivier.peres at laposte.net
Sat Sep 13 02:22:37 PDT 2003


    Here it is. I tried to conform to the specs, with the addition of error 
messages (to stderr, not to the html file) in case information to be extracted 
is missing. It seems to work on my system, tell me if it crashes on yours...

    Note: I cut my lines to 80 characters so it should work, but if copy-pasting 
the script is a problem for you I can resend it as an attachment or upload it to 
a server.

-- beginning of file: genlist.sh --
#!/bin/sh


# genlist.sh
# Script to automatically generate a list of LFS hints
# by Olivier Pérès (Olivier Peres if your terminal cannot print accents)
# email: olivier dot peres at laposte dot net
# Last updated September 13th, 2003
# License: GPL.

# Usage: copy it to the directory that contains the hints/ subdirectory and
# run it. If you want to run it from somewhere else, the easiest way is to
# edit the cd command, below.

#   Limitations
#   ===========
#
# * hint file names must not contain colons, spaces, or any control characters.

#   That wasn't in the specs...
#   ===========================
#
# * I noticed information is missing from a few hints supposedly in the new
#   format, so I added messages on stderr to report missing fields. I think
#   that makes sense since they are mandatory, so their absence is a bug in
#   the hint file. If you don't want them, comment out the echo line in the
#   check_and_report function.




# the simplest way to handle paths properly is to work in the hints/ directory.

if [ -d hints ]
then
	cd hints
else
	echo "Error - no hints/ subdirectory" >> /dev/stderr
	exit 1
fi


# the file to which to write

OUTPUTFILE="list.html"


function output
{
	# echoes its argument to OUTPUTFILE

	echo $1 >> $OUTPUTFILE
}


function htmlclean
{
	# replaces '<' and '>' with the corresponding entities so that they
	# can appear in documents (useful for the authors' email addresses).

	echo `echo $1 | sed -e 's/>/\>/' -e 's/</\</'`
}

function check_and_report
{
	# reports an error message if a field is empty
	# (but continues processing as it is not a fatal error)

	if [ -z "$1" ]
	then
		echo "hint "`basename $2 .txt`": missing "$3 >> /dev/stderr
	fi
}




# DATEDHINTS = list of all the files that contain a DATE
#              in reverse chronological order.

DATEDHINTS=`grep -H "DATE:" files/* | sort -fbi -r -k3 -t: | cut -f1 -d:`


# UNDATEDHINTS = list of all the files that do *not* contain a DATE.

UNDATEDHINTS=`grep -LH "DATE:" files/*`


# initialise output file

echo "" > $OUTPUTFILE		# create it if it didn't exist and empty it.

output "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"
output "<HTML>"
output "<HEAD>"
output "<TITLE>Linux From Scratch Hints list</TITLE>"
output "</HEAD>"
output "<BODY>"
output "<UL>"


# write hints

for HINT in $DATEDHINTS $UNDATEDHINTS
do
	DATE=`echo \`grep -h "DATE:" $HINT | cut -f2 -d:\``
	SYNOPSIS=`echo \`grep -h "SYNOPSIS:" $HINT | cut -f2 -d:\``
	AUTHORS=`echo \`grep "AUTHOR:" $HINT | \
		cut -f2 -d: | sed 's/$/,/'\` | sed 's/,$//'`
	LICENSE=`echo \`grep -h "LICENSE:" $HINT | cut -f2 -d:\``

	check_and_report "$DATE" $HINT "date"
	check_and_report "$SYNOPSIS" $HINT "synopsis"
	check_and_report "$AUTHORS" $HINT "authors"
	check_and_report "$LICENSE" $HINT "license"

	# if the synopsis is missing we have to do something about it
	# (otherwise there will be no usable link to it in the web page!)

	if [ -z "$SYNOPSIS" ]
	then
		SYNOPSIS=`basename $HINT .txt`
	fi

	output "<LI><H4><A HREF=\"$HINT\">$SYNOPSIS</A></H4>"
	output "<DL>"
	output "<DT>Author(s):</DT>"
	output "<DD>`htmlclean "$AUTHORS"`</DD>"
	output "<DT>Date Last Updated:</DT>"
	output "<DD>$DATE</DD>"
	output "<DT>License:</DT>"
	output "<DD>$LICENSE</DD>"
	output "</DL>"
done


# finalise output file

output "</UL>"
output "</BODY>"
output "</HTML>"

-- end of file --

    Olivier.




More information about the website mailing list