#! /bin/csh -f
# Last edited on 1998-12-28 07:04:59 by stolfi

set usage = "$0 TITLE";

# Converts a VMS transcription with locators to HTML, quick and dirty

if ( $#argv != 1 ) then
  echo "usage: ${usage}" > /dev/tty; exit 1
endif

echo '<html>'
echo '<head>'
echo '<title>'"$1"'</title>'
echo '</head>'
echo '<body bgcolor="#ffeedd">'
echo '<small><p>Created '`date '+%Y-%m-%d %H:%M'`'</small></p>'
echo '<h1>'"$1"'</h1>'
echo '<tt><p>'
cat \
  | gawk \
      ' \
        /^##/{ \
          match($0,/<[^<>]*>/); \
          un=substr($0,RSTART+1,RLENGTH-2); \
          if (index(un, ".") == 0) { pg = un; } \
          printf "## %s\n", un; next; \
        } \
        /^</ { \
          i = index($0,">"); \
          loc = substr($0,2,i-2); txt = substr($0,i+1); \
          gsub(/^[ ]*/, "", txt); gsub(/;[A-Z]/, "", loc); \
          if (match(loc, /^f[0-9]+[rv]?[1-6]?[.]([A-Za-z]([0-9]?[.]|)|)[0-9]+[a-z]?$/)) \
            { loc = gensub(/([A-Z])([0-9]+[a-z]?)$/,"\\1.\\2","g",loc); \
              loc = gensub(/([a-z]?)$/,".\\1","g",loc); \
              if((split(loc,fld,/[.]/) == 4) && (fld[1] == pg)) \
                { loc = sprintf("%2s %2s%1s", fld[2], fld[3], fld[4]); } \
            } \
          gsub(/[ ]/, "¨", loc); \
          printf "@[%-6s] %s\n", loc, txt; next; \
        } \
      ' \
  | sed \
      -e 's:\([&]\)\([0-9][0-9][0-9]\);:\1#\2;:g' \
      -e 's:\([&]\)\([^#]\):\1amp;\2:g' \
      -e 's:\([&]\)$:\1amp;:g' \
      -e 's:<:\&lt;:g' \
      -e 's:>:\&gt;:g' \
      -e 's:":\&qt;:g' \
      -e 's:## *\([^. ]*\)$:</p><h2><tt>\1</tt></h2><p>:g' \
      -e 's:## *\([^ ]*\)$:</p><h3><tt>\1</tt></h3><p>:g' \
      -e '/^[^@]/b' \
      -e 's:^@\[\([^ ]*\)\]:\1<font face="EVA Hand 1">:' \
      -e 's:[-.]*$:-:g' \
      -e 's:[-.]*=[-.]*$:=:g' \
      -e 's:[.]: :g' \
      -e 's:=$:</font></p><p>:g' \
      -e 's:-$:</font><br>:' \
  | tr '\015¨' '\012\240'

echo '</p></tt>'
echo '</body>'
echo '</html>'
