/usr/share/pyshared/scitools/pyreport/python_parser.py is in python-scitools 0.9.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | """
Python synthax higlighting
Borrowed from http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52298
"""
# Original copyright : 2001 by Juergen Hermann <jh@web.de>
import cgi, cStringIO, keyword, token, tokenize
class PythonParser:
""" Send colored python source.
"""
_KEYWORD = token.NT_OFFSET + 1
_TEXT = token.NT_OFFSET + 2
def __init__(self):
""" Store the source text.
"""
self._tags = {
token.NUMBER: 'pynumber',
token.OP: 'pyoperator',
token.STRING: 'pystring',
tokenize.COMMENT: 'pycomment',
tokenize.ERRORTOKEN: 'pyerror',
self._KEYWORD: 'pykeyword',
self._TEXT: 'pytext',
}
self.pysrcid = 0;
def __call__(self, raw):
""" Parse and send the colored source.
"""
self.out = cStringIO.StringIO()
self.raw = raw.expandtabs().strip()
# store line offsets in self.lines
self.lines = [0, 0]
pos = 0
while 1:
pos = self.raw.find('\n', pos) + 1
if not pos: break
self.lines.append(pos)
self.lines.append(len(self.raw))
#
# parse the source and write it
self.pos = 0
text = cStringIO.StringIO(self.raw)
self.out.write("<table width=100% cellpadding=0 cellspacing=0 " +
"""onclick="toggle_hidden('pysrc%d','toggle%d');"><tr>
<td rowspan="3"> """ % (self.pysrcid, self.pysrcid) )
self.out.write("""<div class="pysrc" id="pysrc%dinv" style="display:
none">...</div>"""% self.pysrcid)
self.out.write('<div class="pysrc" id="pysrc%d" style="display: block ">'% self.pysrcid)
try:
tokenize.tokenize(text.readline, self.format)
except tokenize.TokenError, ex:
msg = ex[0]
line = ex[1][0]
print >> self.out, ("<h3>ERROR: %s</h3>%s" %
(msg, self.raw[self.lines[line]:]))
self.out.write('</div>')
self.out.write('''
</td>
<td colspan="2" class="collapse bracket"></td>
</tr>
<tr>
<td class="bracketfill"></td>
<td width=5px class="collapse">
<div id="toggle%d">
<small>.</small>
</div>
</td>
</tr>
<tr><td colspan="2" class="collapse bracket"></td>
</tr>
</table>
''' % (self.pysrcid))
self.pysrcid += 1
return self.out.getvalue()
def format(self, toktype, toktext, (srow, scol), (erow, ecol), line):
""" Token handler.
"""
# calculate new positions
oldpos = self.pos
newpos = self.lines[srow] + scol
self.pos = newpos + len(toktext)
#
# handle newlines
if toktype in [token.NEWLINE, tokenize.NL]:
# No need, for that: the css attribute "white-space: pre;" will
# take care of that.
self.out.write("\n")
return
#
# send the original whitespace, if needed
if newpos > oldpos:
self.out.write(self.raw[oldpos:newpos])
#
# skip indenting tokens
if toktype in [token.INDENT, token.DEDENT]:
self.pos = newpos
return
#
# map token type to a color group
if token.LPAR <= toktype and toktype <= token.OP:
toktype = token.OP
elif toktype == token.NAME and keyword.iskeyword(toktext):
toktype = self._KEYWORD
style = self._tags.get(toktype, self._tags[self._TEXT])
#
# send text
self.out.write('<span class="%s">' % (style, ))
self.out.write(cgi.escape(toktext))
self.out.write('</span>')
python2html = PythonParser()
|