L. J. Jaeckel
CMPSC 210 — Linux System Administration
March 5, 2011

Listing of shell script:   txt2html

Given any plain-old plain-text file, creates a new HTML file which, when viewed in a browser, displays a listing of the original text file (with optional line numbers). The input file can even itself be a HTML file.

Specifically:

This script uses sed(1) to do all the text transformations.
This listing was itself produced by running txt2html on itself:
   txt2html -n < txt2html > txt2html_list.html

See "Usage" remarks in the list below for instructions.


     1	#!/bin/bash
     2	# txt2html
     3	# L. J. Jaeckel
     4	# March 5, 2011
     5	#
     6	# Convert any plain text file to a HTML file that, when viewed,
     7	# will show what the original file had.  The input can even
     8	# be a HTML file.
     9	
    10	# Usage:
    11	# ------
    12	# As currently written, this script is strictly a simple
    13	# stdin --> stdout filter.  It will not take input from
    14	# a named file argument on the command line.  It only
    15	# reads from stdin.  It will take one argument, -n
    16	# to produce a line numbered listing.
    17	#
    18	#    Right:  txt2html < infile > outfile
    19	#    Right:  txt2html -n < infile > outfile
    20	#
    21	#    Wrong:  txt2html infile > outfile
    22	#    Wrong:  txt2html -n infile > outfile
    23	#
    24	# The output file has some template header lines (including
    25	# the <title>xxxxx</title> line) that you will probably want
    26	# to edit manually.
    27	
    28	lnums=''
    29	indarg1='s/^/    /'
    30	if [ "$1" = "-n" ]
    31	then
    32	  lnums=-n
    33	  indarg1='1s/x/x/'
    34	fi
    35	
    36	cat $lnums | sed -e '1i\
    37	<html>\
    38	  <head>\
    39	    <title>Listing of file</title>\
    40	    <style type="text/css">\
    41	      cmnt {color:green; font-style:italic}\
    42	      cmnd {font-family:Courier; text-decoration:underline}\
    43	      inp  {color:red; font-weight:bold; text-decoration:underline}\
    44	      badd {color:red; font-weight:bold}\
    45	      look {background-color:yellow}\
    46	    </style>\
    47	  </head>\
    48	  <body>\
    49	    <strong>L. J. Jaeckel</strong><br />\
    50	    <strong>CMPSC 210 &mdash; Linux System Administration</strong><br />\
    51	    <strong>'"`date '+%B %-e, %Y'`"'</strong><br /><br />\
    52	    <center>\
    53	      <h2><strong>Listing of file</strong></h2>\
    54	    </center>\
    55	    <p>Put any remarks you want here, or delete this line altogether.</p>\
    56	    <hr />\
    57	    <pre>'     -e \
    58	's/&/\&amp;/g' -e \
    59	's/</\&lt;/g'  -e \
    60	's/>/\&gt;/g'  -e \
    61	"$indarg1"     -e \
    62	'$a\
    63	    <hr />\
    64	    </pre>\
    65	  </body>\
    66	</html>\
    67	'
    68