00001 #!/usr/bin/env python
00002 from htmlbuilder import html
00003 import sys, os
00004 
00005 pystyle = '''
00006     .python { padding-bottom: 50px; }
00007     .class { color: black; font-weight: bold }
00008     .comment { color: green; font-weight: bold }
00009     .decorator { color: firebrick; }
00010     .function { color: darkorange; font-weight: bold; }
00011     .keyword { color: lightseagreen; font-weight: bold; }
00012     .lineno { color: gray; border-right:1px solid gray; padding-right: 5px; }
00013     .string { color: olive;  font-weight: bold; }
00014 '''
00015 
00016 def usage():
00017     print '''\nUsage:\n\npy2html infile.py [outfile.html]'''
00018 
00019 def get_file_names():
00020     args = sys.argv[1:]
00021     numargs = len(args)
00022     if numargs == 0:
00023         usage()
00024     else:
00025         pyfile = args[0]
00026         if numargs > 1:
00027             htmlfile = args[1]
00028         else:
00029             htmlfile = os.path.splitext( pyfile )[0] + '.html'
00030     return pyfile, htmlfile
00031 
00032 def main():
00033     pyfile, htmlfile = get_file_names()
00034     pagetitle = os.path.split(pyfile)[1]
00035     page = html.page( pagetitle )
00036     page.styleblocks.append( pystyle )
00037     page.python( pyfile )
00038     outfile = open( htmlfile, 'w' )
00039     try:
00040         print 'Writing %s...' % htmlfile
00041         page.tidywrite( outfile )
00042         print 'Done.'
00043     finally:
00044         outfile.close()
00045 
00046 if __name__ == '__main__':
00047     main()