File: py2html.py

package info (click to toggle)
pyqwt3d 0.1.8-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 984 kB
  • sloc: python: 3,932; cpp: 328; makefile: 113; sh: 7
file content (61 lines) | stat: -rwxr-xr-x 1,535 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python

# This is a trimmed down version of Marc-Andre Lemburg's py2html.py.
# The original code can be found at http://starship.python.net/~lemburg/.
#
# Borrow (or steal?) PyFontify.py from reportlab.lib.

import PyFontify
import sys
  
pattern="""<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<title>%(source)s</title>
</head>
<body bgcolor=white>
<pre>
%(target)s
</pre>
</body>
</html>
"""

formats = { 'comment': "<font color=blue>%s</font>",
            'identifier': "<font color=red>%s</font>",
            'keyword': "<b>%s</b>",
            'string': "<font color=green>%s</font>" }

def escape_html(text):
        t = (('&','&amp;'), ('<','&lt;'), ('>','&gt;'))
        for x,y in t:
            text = y.join(text.split(x))
        return text

def py2html(source):
    f = open(source)
    text = f.read()
    f.close()
    tags = PyFontify.fontify(text)
    done = 0
    chunks = []
    for tag, start, end, sublist in tags:
        chunks.append(escape_html(text[done:start]))
        chunks.append(formats[tag] % escape_html(text[start:end]))
        done = end
    chunks.append(escape_html(text[done:]))
        
    dict = { 'source' : source, 'target' : ''.join(chunks) }
    f = open(source + '.html', 'w')
    f.write(pattern % dict)
    f.close()

if __name__ == '__main__':

    if len(sys.argv) == 1:
        print("Usage: ./py2html.py files")
        print("\tfiles is a list of Python source files.")
        sys.exit(1)

    for file in sys.argv[1:]:
        py2html(file)