#!/usr/bin/env python # convert CLAWPACK source code, data files, READMEs, etc. into html. # for example, # clawcode2html filename.f # generates # filename.f.html # If filename.f.html already exists, you will be prompted before # overwriting unless the -f or --force flag is used, e.g. # clawcode2html --force filename.f # # Based on mathcode2html.py from # http://www.amath.washington.edu/~rjl/mathcode2html # with some modifications for CLAWPACK. # # The environment variable CLAW must be properly set to the root # of the claw directory before using this script. # # Most of code is put into
environment.
# Any comments enclosed by begin_html and end_html are taken out
# of the environment and indented properly (using a table)
# to match the surrounding code.
# By default, the html comments are offset in blue font, except if the
# input file has extension .txt or no extension, in which case it's
# left as black. The default_color can be changed below.
# Also, the begin_html line may contain [color: red] for example,
# to determine the color of this comment.
# You may modify the recognized extensions and comment characters below.
# Allows many latex commands in comments if jsMath is used on webpage.
# In this case make sure the variable jsMathScript is properly set below.
# If you don't want to include a call to the jsMath script on the html
# page, invoke mathcode2html with the --nojsmath option.
#---------------------------------------------------------------------------
# Copyright (C) 2008 Randall J. LeVeque
#
# Distributed as part of Clawpack, under the BSD license
# See www.clawpack.org
#---------------------------------------------------------------------------
import sys,os,glob
import string,re
import time
try:
from optparse import OptionParser
except:
print 'You must use a more recent version of Python'
sys.exit(1)
# parse command line arguments:
parser = OptionParser()
parser.add_option("-f", "--force",
action="store_true", dest="forced", default=False,
help="force action even if it overwrites html file")
parser.add_option("--nojsmath",
action="store_false", dest="nojsMath", default=False,
help="don't include call to jsMath script in html")
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="don't print status messages to stdout")
parser.add_option("--noheader",
action="store_false", dest="header", default=True,
help="suppress printing header at top of html page")
parser.add_option("--dropext",
action="store_true", dest="dropext", default=False,
help="drop the file extension before adding .html for output")
parser.add_option("--eagle",
action="store_true", dest="eagle", default=False,
help="load the eagleclaw.css style")
(options, infiles) = parser.parse_args()
forced = options.forced
nojsMath = options.nojsMath
verbose = options.verbose
header = options.header
dropext = options.dropext
eagle = options.eagle
# CLAWPACK environment variables:
try:
clawdir = os.getenv('CLAW') # assumes environment variable set properly
except:
print "CLAW environment variable not set"
print "You need to run setenv.py before setup.py"
sys.exit(1)
if clawdir == None:
print "CLAW environment variable not set"
print "You need to run setenv.py before setup.py"
sys.exit(1)
# Create addresses for links on html pages.
# change the lines below if you want to point to a webpage home instead
# of the local file system (e.g. clawaddr = "http://www.mywebpage/claw")
clawaddr = 'http://depts.washington.edu/clawpack/clawpack-4.x'
# Set comment characters for different programming languages:
# Augment or modify as desired.
commentchar = { '.f' : ['!', '#'], \
'.f95' : ['!','#'], \
'.f90' : ['!','#'], \
'.m' : '%', \
'.py' : '#', \
'.sh' : '#', \
'.data' : ['#','=:'], \
'.txt': None, \
'Makefile': '#', \
'' : None}
firstfort = ['c','*','C','!'] # valid fortran .f comment char's in col. 1
firstfort95 = ['!'] # valid fortran .f95 comment char's in col. 1
leadingindent = '' # additional indentation for webpage, if desired
default_color = 'blue'
try:
infiles.remove('clawcode2html.py') # can't apply this code to itself!
except:
pass
for infilename in infiles:
# check if this is a code file of a recognized language.
ext = os.path.splitext(infilename)[1]
if infilename == 'Makefile':
ext = 'Makefile' # special case
if not commentchar.has_key(ext):
print " "
print " Warning: Unrecognized extension, will proceed"
print " with no replacement of comment characters"
commentchar[ext] = None
# open input and output files:
#-----------------------------
try:
ifile = open(infilename,'r')
except:
print "File not found:", infilename
sys.exit(1)
# Search for [use: ...] statements in file:
all_lines = ifile.read()
regexp = re.compile(r"\[use:[^\]]*jsMath")
result = regexp.search(all_lines)
# use jsMath if [use:jsMath] found and option --nojsmath was
# not used in call:
usejsMath = (result is not None) & (not nojsMath)
regexp = re.compile(r"\[use:(?P[^\]]*).css\]")
result = regexp.search(all_lines)
if result is not None:
cssfile = result.group('cssfile').strip() + '.css'
else:
cssfile = None
ifile.seek(0) # return to start of file
lines = ifile.readlines()
if dropext:
infileroot = os.path.splitext(infilename)[0]
outfilename = infileroot + '.html'
else:
outfilename = infilename + '.html'
if (glob.glob(outfilename) != []) & (not forced):
sys.stdout.write(' OK to overwrite %s? ' % outfilename)
answer = raw_input()
if answer not in ['y','Y','yes']:
print ' Aborting!'
sys.exit(1)
ofile = open(outfilename,'w')
# start creating html file:
#--------------------------
if verbose:
print ' Converting ', infilename, ' to ', outfilename
ofile.write("""""" % infilename)
ofile.write('\n\n\n %s \n\n' % outfilename)
if eagle:
ofile.write("""
""" % (clawaddr,clawaddr))
elif cssfile:
ofile.write("""
""" % (clawaddr, cssfile, clawaddr))
else:
ofile.write(
"""
""" % clawaddr)
# determine time and reformat:
time1 = time.asctime()
year = time1[-5:]
day = time1[:-14]
hour = time1[-13:-5]
creationtime = day + year + ' at ' + hour
# put full file name with path into a comment for future reference:
fullinfilename = os.path.join(os.getcwd(),infilename)
ofile.write('\n\n' % fullinfilename)
ofile.write('\n\n' % creationtime)
if header:
#ofile.write('