Computational Physics

Dr. Ayars

#!/usr/bin/python

"""
Given Python program(s) as an argument, this program writes a
corresponding html file that displays that program listing for 
my webpages.

The template for the html is stored at .../tools/listing_template.
"""

import sys, string

# Load template file as list of lines.
template = open('/Users/eayars/50/tools/listing_template').readlines()

# Find line to change.
insertion_point = template.index('<!--insertion-->\n')

for listing in sys.argv[1:]:
    
    # Load code to include in page
    code = open(listing, 'r').readlines()
    
    # change '<' to &lt; 
    for line in code:
        line = line.replace('<', '&lt;')
    
    # Put together new list of lines
    newlines=template[:insertion_point]+code+template[(insertion_point+1):]
        
    # Figure out the appropriate filename.
    # Split the filename at '.'
    temp_filename = listing.split('.')
    # Replace the last ".*" with ".shtml".
    temp_filename[-1] = "shtml"
    # rejoin filename
    filename = '.'.join(temp_filename)

    # Write the resulting file.
    newfile = open(filename, 'w')
    newfile.writelines(newlines)
    newfile.close
    
    # Let user know how things are going.
    print filename