#!/bin/sh
# (C) 1997,2000 Marcel Waldvogel
#
# Generates an HTML table from a tab-separated file.
# This was used for a checklist, therefore entries starting with an 'x'
# are treated specially (centered and highlighted in green). You can
# change this (or delete it or add other rules) as you please.
# Empty fields are filled with a non-breaking space, as some browsers
# will treat empty or whitespace-only fields unexpectedly.
#
# You can put comments in <> into the input file, which will not be
# in the HTML output.
#
# Usage:
#	tablist2html <tabfile> <htmlfile> <htmlheaderfile> <htmlfooterfile>
# The htmlheaderfile should contain a "<TABLE>" tag with your desired
# options; the htmlfooterfile whould contain the matching "</TABLE>".
#
# Sample htmlheaderfile:
# <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
# <HTML><HEAD><TITLE>Title</TITLE></HEAD><BODY><H1>Title</H1><TABLE BORDER=1>
#
# Sample htmlfooterfile:
# </TABLE></BODY></HTML>
#
# Of course, you can add much more stuff.

(
cat "$3"

# The first line removes all comment lines
# The next line remove all <> comments
# The next three lines make sure that empty entries also get boxed
#   (the first and second line are identical, they are only needed because
#    the replace regexps overlap, i.e. each TAB needs to be used for two
#    regexps, the one beginning and the one ending with this <TAB>)
# The next three lines make it an HTML table body
#   (each line starting with <TR> and each field starting with <TD>,
#    entries starting with 'x' are centered)
# The next line gets rid of unnecessary whitespace at the end of fields
sed -e '/^#/d' \
    -e 's/<[^<>]*>//g' \
    \
    -e 's/		/	\&nbsp;	/g' \
    -e 's/		/	\&nbsp;	/g' \
    -e 's/	$/	\&nbsp;/' \
    \
    -e 's/^/<TR><TD>/' \
    -e 's/	x/<TD ALIGN=CENTER BGCOLOR=green>x/g' \
    -e 's/	/<TD>/g' \
    \
    -e 's/ *<TD>/<TD>/g' \
    < "$1"

cat "$4"
) > "$2"
