OLD | NEW |
| (Empty) |
1 # Copyright (c) 2001-2004 Twisted Matrix Laboratories. | |
2 # See LICENSE for details. | |
3 | |
4 # | |
5 import tokenize, cgi, keyword | |
6 import reflect | |
7 | |
8 class TokenPrinter: | |
9 | |
10 currentCol, currentLine = 0, 1 | |
11 lastIdentifier = parameters = 0 | |
12 | |
13 def __init__(self, writer): | |
14 self.writer = writer | |
15 | |
16 def printtoken(self, type, token, (srow, scol), (erow, ecol), line): | |
17 #print "printtoken(%r,%r,%r,(%r,%r),(%r,%r),%r), row=%r,col=%r" % ( | |
18 # self, type, token, srow,scol, erow,ecol, line, | |
19 # self.currentLine, self.currentCol) | |
20 if self.currentLine < srow: | |
21 self.writer('\n'*(srow-self.currentLine)) | |
22 self.currentLine, self.currentCol = srow, 0 | |
23 self.writer(' '*(scol-self.currentCol)) | |
24 if self.lastIdentifier: | |
25 type = "identifier" | |
26 self.parameters = 1 | |
27 elif type == tokenize.NAME: | |
28 if keyword.iskeyword(token): | |
29 type = 'keyword' | |
30 else: | |
31 if self.parameters: | |
32 type = 'parameter' | |
33 else: | |
34 type = 'variable' | |
35 else: | |
36 type = tokenize.tok_name.get(type).lower() | |
37 self.writer(token, type) | |
38 self.currentCol = ecol | |
39 self.currentLine += token.count('\n') | |
40 if self.currentLine != erow: | |
41 self.currentCol = 0 | |
42 self.lastIdentifier = token in ('def', 'class') | |
43 if token == ':': | |
44 self.parameters = 0 | |
45 | |
46 | |
47 class HTMLWriter: | |
48 | |
49 noSpan = [] | |
50 | |
51 def __init__(self, writer): | |
52 self.writer = writer | |
53 noSpan = [] | |
54 reflect.accumulateClassList(self.__class__, "noSpan", noSpan) | |
55 self.noSpan = noSpan | |
56 | |
57 def write(self, token, type=None): | |
58 token = cgi.escape(token) | |
59 if (type is None) or (type in self.noSpan): | |
60 self.writer(token) | |
61 else: | |
62 self.writer('<span class="py-src-%s">%s</span>' % | |
63 (type, token)) | |
64 | |
65 | |
66 class SmallerHTMLWriter(HTMLWriter): | |
67 """HTMLWriter that doesn't generate spans for some junk. | |
68 | |
69 Results in much smaller HTML output. | |
70 """ | |
71 noSpan = ["endmarker", "indent", "dedent", "op", "newline", "nl"] | |
72 | |
73 def filter(inp, out, writer=HTMLWriter): | |
74 out.write('<pre>\n') | |
75 printer = TokenPrinter(writer(out.write).write).printtoken | |
76 try: | |
77 tokenize.tokenize(inp.readline, printer) | |
78 except tokenize.TokenError: | |
79 pass | |
80 out.write('</pre>\n') | |
81 | |
82 def main(): | |
83 import sys | |
84 filter(open(sys.argv[1]), sys.stdout) | |
85 | |
86 if __name__ == '__main__': | |
87 main() | |
OLD | NEW |