1 # Copyright (c) Twisted Matrix Laboratories.
2 # See LICENSE for details.
6 """HTML pretty-printing for Python source code."""
8 __version__ = '$Revision: 1.8 $'[11:-2]
10 from twisted.python import htmlizer, usage
11 from twisted import copyright
15 header = '''<html><head>
16 <title>%(title)s</title>
17 <meta name=\"Generator\" content="%(generator)s" />
23 footer = """</body>"""
25 styleLink = '<link rel="stylesheet" href="%s" type="text/css" />'
26 alternateLink = '<link rel="alternate" href="%(source)s" type="text/x-python" />'
28 class Options(usage.Options):
29 synopsis = """%s [options] source.py
31 os.path.basename(sys.argv[0]),)
34 ('stylesheet', 's', None, "URL of stylesheet to link to."),
37 compData = usage.Completions(
38 extraActions=[usage.CompleteFiles('*.py', descr='source python file')]
41 def parseArgs(self, filename):
42 self['filename'] = filename
47 options.parseOptions()
48 except usage.UsageError, e:
51 filename = options['filename']
52 if options.get('stylesheet') is not None:
53 stylesheet = styleLink % (options['stylesheet'],)
57 output = open(filename + '.html', 'w')
59 output.write(header % {
61 'generator': 'htmlizer/%s' % (copyright.longversion,),
62 'alternate': alternateLink % {'source': filename},
63 'stylesheet': stylesheet
65 htmlizer.filter(open(filename), output,
66 htmlizer.SmallerHTMLWriter)