1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """
19 Original code from active state recipe
20 'Colorize Python source using the built-in tokenizer'
21
22 ------------------------------------
23 MoinMoin - Python Source Parser
24 ------------------------------------
25
26 This code is part of MoinMoin (http://moin.sourceforge.net/) and converts
27 Python source code to HTML markup, rendering comments, keywords, operators,
28 numeric and string literals in different colors.
29
30 It shows how to use the built-in keyword, token and tokenize modules
31 to scan Python source code and re-emit it with no changes to its
32 original formatting (which is the hard part).
33 """
34
35 import keyword, token, tokenize
36 from cStringIO import StringIO
37
38 _KEYWORD = token.NT_OFFSET + 1
39 _TEXT = token.NT_OFFSET + 2
40
42 """Python Source Parser used to get HTML colored python source
43 """
45 """Store the source text"""
46 self.raw = raw.expandtabs().strip()
47 self.out = out
48 self.tags = tags
49
72
73 - def __call__(self, toktype, toktext, (srow,scol), (erow,ecol), line):
74 """Token handler"""
75
76 oldpos = self.pos
77 newpos = self.lines[srow] + scol
78 self.pos = newpos + len(toktext)
79
80 if toktype in [token.NEWLINE, tokenize.NL]:
81 self.out.write('\n')
82 return
83
84 if newpos > oldpos:
85 self.out.write(self.raw[oldpos:newpos])
86
87 if toktype in [token.INDENT, token.DEDENT]:
88 self.pos = newpos
89 return
90
91 if token.LPAR <= toktype and toktype <= token.OP:
92 toktype = 'OP'
93 elif toktype == token.NAME and keyword.iskeyword(toktext):
94 toktype = 'KEYWORD'
95 else:
96 toktype = tokenize.tok_name[toktype]
97
98 open_tag = self.tags.get('OPEN_'+toktype, self.tags['OPEN_TEXT'])
99 close_tag = self.tags.get('CLOSE_'+toktype, self.tags['CLOSE_TEXT'])
100
101 self.out.write(open_tag)
102 self.out.write(html_escape(toktext))
103 self.out.write(close_tag)
104
105
106 from logilab.mtconverter.transform import Transform
107 from logilab.mtconverter import html_escape
108
110 """Colorize Python source files"""
111
112 name = "python_to_html"
113 inputs = ("text/x-python",)
114 output = "text/html"
115
116 config = {
117 'OPEN_NUMBER': '<span style="color: #0080C0;">',
118 'CLOSE_NUMBER': '</span>',
119 'OPEN_OP': '<span style="color: #0000C0;">',
120 'CLOSE_OP': '</span>',
121 'OPEN_STRING': '<span style="color: #004080;">',
122 'CLOSE_STRING': '</span>',
123 'OPEN_COMMENT': '<span style="color: #008000;">',
124 'CLOSE_COMMENT': '</span>',
125 'OPEN_NAME': '<span style="color: #000000;">',
126 'CLOSE_NAME': '</span>',
127 'OPEN_ERRORTOKEN': '<span style="color: #FF8080;">',
128 'CLOSE_ERRORTOKEN': '</span>',
129 'OPEN_KEYWORD': '<span style="color: #C00000;">',
130 'CLOSE_KEYWORD': '</span>',
131 'OPEN_TEXT': '',
132 'CLOSE_TEXT': '',
133 }
134
136 dest = StringIO()
137 Parser(trdata.data, self.config, dest).format()
138 return dest.getvalue()
139