mkhtml


#! /bin/bash
#  Create html from text files.
#

# 2009-03-03
#   Added missing body tag to the generated output file.

# 2006-06-08
#   Modified to put output in GENERATED_HTML subdirectory of the
#   directory that contains the target file.  Added footer div with
#   date of generation and links to HTML and CSS validators.

# 2006-04-29
#   Apache treats the order of extensions as irrelevant.  So
#   xxx.sh.html is the same as xxx.html.sh, with the result that it
#   tries to send web pages generated by mkhtml for shell scripts to
#   the shell to be interpreted.  So I replaced all internal dots in
#   filenames with underscores in naming the output file.

#   Modified for bash I/O redirection from ksh-style

# ccv 3/17/02

IFS=""
for f
do
  echo "Making html for $f"
  outdir="$(dirname $f)/GENERATED_HTML"
  mkdir $outdir >/dev/null 2>&1

  if [[ -r $f ]]
  then
    exec < $f 
  else
    echo "Unable to read $f"
    continue
  fi
outfile="$(basename $f)"
outfile="$outdir/${outfile//./_}.html"
  cat <<!!! > "$outfile"
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
                          "http://www.w3.org/TR/html4/strict.dtd">
<html><head><title>$(basename $f)</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

  <style type="text/css" media="screen">
      body {
        background-color: #ffc;
        font-family:      sans-serif;
        color:            #661;
        margin:           2em;
        }
      h1 {
        text-align:       center;
        }
      pre {
        background-color: #fff;
        color:            #111;
        margin:           1em;
        border:           1px solid red;
        }
      #footer {
        text-align:       center;
        font-size:        0.7em;
        }
  </style>
</head>
<body>
	<h1>$(basename $f)</h1>
	<hr>
	<pre>
!!!

  while read -r REPLY
  do
    REPLY=${REPLY//&/&amp;}
    REPLY=${REPLY//>/&gt;}
    REPLY=${REPLY//</&lt;}
    echo "$REPLY" >> "$outfile"
  done

  cat <<!!! >> "$outfile"
</pre>
<div id="footer">

  <p>This web page was generated by <cite><a
    href="http://babbage.cs.qc.cuny.edu/Miscellany/Utils/mkhtml.html">mkhtml</a></cite> on
    $(date +"%Y-%m-%d")<br />
    Copyright &copy; 2002&ndash;$(date +"%Y") by Christopher Vickery<br />
    Licensed under a <a rel="license"
    href="http://creativecommons.org/licenses/by-sa/2.5/">Creative
    Commons Attribution-ShareAlike 2.5 License</a><br />
    <a href="http://validator.w3.org/check?uri=referer">HTML</a> -
    <a href="http://jigsaw.w3.org/css-validator/check/referer">CSS</a>
  </p>
</div>
</body></html>
!!!

done