#!/bin/sh
#
# Take overstrike bold/underline and/or ansi bold/inverse sequences
# and turn them into color html.  corey @ cac.washington.edu 1/11/02
#				  http://staff.washington.edu/corey
#
# -w does white background with red/blue colors and black text (the default)
# -b does black background with green/blue colors and white text
# -x does black background with my green/orange xterm colors and white text
# -g does a gray background with green/orange colors and white text
# -p does a pale background with green/orange colors and black text
# -i converts inverse video to bold+italic instead of a color
# The next args, if present, are bold and underline colors as 6 character hex
#
# example input sources: man, wdiff -p, bgrep

ESC=''
BI="${ESC}\\[7m"	# inverse video (converted to B2 below unless -i used)
BO="${ESC}\\[1m"	# bold color (green on my xterm)
B2="${ESC}\\[4m"	# italic color (red on my xterm)
NO="${ESC}\\[m"
BS=''
AMP=''
LT=''
GT=''

BGCOLOR=ffffff
TXCOLOR=000000
COLOR1=0000ff		# default bold color is blue
COLOR2=ff0000		# default underline color is red

for i in $*; do
 case "$1" in
  -w) shift;;		# default is white background with black text
  -b) BGCOLOR=000000; TXCOLOR=ffffff; COLOR1=00ff00;COLOR2=00a0ff; shift;;
  -x) BGCOLOR=000000; TXCOLOR=ffffff; COLOR1=30cf30;COLOR2=ffa279; shift;;
  -g) BGCOLOR=7a7a7a; TXCOLOR=ffffff; COLOR1=30cf30;COLOR2=ffa279; shift;;
  -p) BGCOLOR=f4f0f4; TXCOLOR=000000; COLOR1=30cf30;COLOR2=ffa279; shift;;
  -i) ITALICS="s,$BI\\(.[^${ESC}]*\\)$NO,<B><I>\\1</I></B>,g"; shift;;
   *) break;;
 esac
done

COLOR1=${1-$COLOR1}	# bold overstrike
COLOR2=${2-$COLOR2}	# underline overstrike

echo "<HTML><BODY BGCOLOR=$BGCOLOR TEXT=$TXCOLOR><PRE>"
sed "
    s,&,$AMP,g
    s,<,$LT,g
    s,>,$GT,g

    ${ITALICS-s,$BI,$B2,g}

    s,\\(.\\)$BS\\1,$BO\\1$NO,g
    : l1
    s,$BO\\(.[^${ESC}]*\\)$NO$BO,$BO\\1,
    t l1
    s,$BO,<B><FONT COLOR=#${COLOR1}>,g

    s,_$BS\\(.\\),$B2\\1$NO,g
    : l2
    s,$B2\\(.[^${ESC}]*\\)$NO$B2,$B2\\1,
    t l2
    s,$B2,<B><FONT COLOR=#${COLOR2}>,g

    s,$NO,</B></FONT>,g

    s,$AMP,\\&amp;,g
    s,$LT,\\&lt;,g
    s,$GT,\\&gt;,g

    t
    "
echo "</PRE></BODY></HTML>"
