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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
# $Id$
# highlight.py - syntax highlighting functions for Myghty distribution
# Copyright (C) 2004 Michael Bayer mike_mp@zzzcomputing.com
# Original Perl code and documentation copyright (c) 1998-2003 by Jonathan Swartz.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import re, StringIO, sys, string, os
import token, tokenize, keyword
# Highlighter - highlights Myghty and Python source code
__ALL__ = ['highlight', 'PythonHighlighter', 'MyghtyHighlighter']
pystyles = {
token.ENDMARKER : 'python_operator' ,
token.NAME : 'python_name' ,
token.NUMBER : 'python_number' ,
token.STRING : 'python_literal' ,
token.NEWLINE : 'python_operator' ,
token.INDENT : 'python_operator' ,
token.DEDENT : 'python_operator' ,
token.LPAR : 'python_enclosure' ,
token.RPAR : 'python_enclosure' ,
token.LSQB : 'python_enclosure' ,
token.RSQB : 'python_enclosure' ,
token.COLON : 'python_operator' ,
token.COMMA : 'python_operator' ,
token.SEMI : 'python_operator' ,
token.PLUS : 'python_operator' ,
token.MINUS : 'python_operator' ,
token.STAR : 'python_operator' ,
token.SLASH : 'python_operator' ,
token.VBAR : 'python_operator' ,
token.AMPER : 'python_operator' ,
token.LESS : 'python_operator' ,
token.GREATER : 'python_operator' ,
token.EQUAL : 'python_operator' ,
token.DOT : 'python_operator' ,
token.PERCENT : 'python_operator' ,
token.BACKQUOTE : 'python_operator' ,
token.LBRACE : 'python_enclosure',
token.RBRACE : 'python_enclosure' ,
token.EQEQUAL : 'python_operator' ,
token.NOTEQUAL : 'python_operator' ,
token.LESSEQUAL : 'python_operator' ,
token.GREATEREQUAL : 'python_operator' ,
token.TILDE : 'python_operator' ,
token.CIRCUMFLEX : 'python_operator' ,
token.LEFTSHIFT : 'python_operator' ,
token.RIGHTSHIFT : 'python_operator' ,
token.DOUBLESTAR : 'python_operator' ,
token.PLUSEQUAL : 'python_operator' ,
token.MINEQUAL : 'python_operator' ,
token.STAREQUAL : 'python_operator' ,
token.SLASHEQUAL : 'python_operator' ,
token.PERCENTEQUAL : 'python_operator' ,
token.AMPEREQUAL : 'python_operator' ,
token.VBAREQUAL : 'python_operator' ,
token.CIRCUMFLEXEQUAL : 'python_operator' ,
token.LEFTSHIFTEQUAL : 'python_operator' ,
token.RIGHTSHIFTEQUAL : 'python_operator' ,
token.DOUBLESTAREQUAL : 'python_operator' ,
token.DOUBLESLASH : 'python_operator' ,
token.DOUBLESLASHEQUAL : 'python_operator' ,
token.OP : 'python_operator' ,
token.ERRORTOKEN : 'python_operator' ,
token.N_TOKENS : 'python_operator' ,
token.NT_OFFSET : 'python_operator' ,
tokenize.COMMENT: 'python_comment',
}
html_escapes = {
'&' : '&',
'>' : '>',
'<' : '<',
'"' : '"'
}
def html_escape(string):
#return "@" + re.sub(r"([&<>])", lambda m: html_escapes[m.group()], string) + "+"
return re.sub(r"([&<>])", lambda m: html_escapes[m.group()], string)
def highlight(source, filename = None, syntaxtype = None, html_escape = True):
if syntaxtype is not None:
highlighter = highlighters.get(syntaxtype, None)
elif filename is not None:
(root, filename) = os.path.split(filename)
highlighter = highlighters.get(filename, None)
if highlighter is None:
(root, ext) = os.path.splitext(filename)
highlighter = highlighters.get(ext, None)
else:
highlighter = None
if highlighter is None:
if html_escape:
return html_escape(source)
else:
return source
else:
return highlighter(source, html_escape = html_escape).highlight()
class Highlighter:
def __init__(self, source, output = None, html_escape = True):
self.source = source
self.pos = 0
self.html_escape = html_escape
if output is None:
self.output = StringIO.StringIO()
else:
self.output = output
def content(self):
return self.output.getvalue()
def highlight(self):raise NotImplementedError()
def colorize(self, tokens):
for pair in tokens:
if pair[1] is None:
if self.html_escape:
self.output.write(html_escape(pair[0]))
else:
self.output.write(pair[0])
else:
if self.html_escape:
self.output.write('<span class="%s">%s</span>' % (pair[1], html_escape(pair[0])))
else:
self.output.write('<span class="%s">%s</span>' % (pair[1], pair[0]))
class PythonHighlighter(Highlighter):
def _line_grid(self, str, start, end):
lines = re.findall(re.compile(r'[^\n]*\n?', re.S), str)
r = 0
for l in lines[0 : end[0] - start[0]]:
r += len(l)
r += end[1]
return (start, (start[0], r))
def highlight(self):
buf = StringIO.StringIO(self.source)
# tokenize module not too good at getting the
# whitespace at the end of a python block
trailingspace = re.search(r"\n([ \t]+$)", self.source, re.S)
if trailingspace:
trailingspace = trailingspace.group(1)
curl = -1
tokens = []
curstyle = None
line = None
for t in tokenize.generate_tokens(lambda: buf.readline()):
if t[2][0] != curl:
curl = t[2][0]
curc = 0
line = t[4]
# pick up whitespace and output
if t[2][1] > curc:
tokens.append(line[curc : t[2][1]])
curc = t[2][1]
if self.get_style(t[0], t[1]) != curstyle:
if len(tokens):
self.colorize([(string.join(tokens, ''), curstyle)])
tokens = []
curstyle = self.get_style(t[0], t[1])
(start, end) = self._line_grid(line, t[2], t[3])
tokens.append(line[start[1]:end[1]])
curc = t[3][1]
curl = t[3][0]
# any remaining content to output, output it
if len(tokens):
self.colorize([(string.join(tokens, ''), curstyle)])
if trailingspace:
self.output.write(trailingspace)
return self.content()
def get_style(self, tokenid, str):
if tokenid == token.NAME:
if keyword.iskeyword(str):
return "python_keyword"
else:
return "python_name"
elif tokenid == token.OP:
if "()[]{}".find(str) != -1:
return "python_enclosure"
else:
return "python_operator"
else:
return pystyles.get(tokenid, None)
class MyghtyHighlighter(Highlighter):
def _match(self, regexp):
match = regexp.match(self.source, self.pos)
if match:
(start, end) = match.span()
self.output.write(self.source[self.pos:start])
if start == end:
self.pos = end + 1
else:
self.pos = end
return match
else:
return None
def highlight(self):
while (self.pos < len(self.source)):
if self.match_named_block():
continue
if self.match_block():
continue
if self.match_comp_call():
continue
if self.match_comp_content_call():
continue
if self.match_substitution():
continue
if self.match_line():
continue
if self.match_text():
continue;
break
return self.content()
def pythonize(self, text):
py = PythonHighlighter(text, output = self.output)
py.highlight()
def match_text(self):
textmatch = re.compile(r"""
(.*?) # anything, followed by:
(
(?<=\n)(?=[%#]) # an eval or comment line
|
(?=</?[%&]) # a substitution or block or call start or end
# - don't consume
|
(\\\n) # an escaped newline
|
\Z # end of string
)""", re.X | re.S)
match = self._match(textmatch)
if match:
self.colorize([(match.group(1), 'text')])
if match.group(3):
self.colorize([(match.group(3), 'python_operator')])
return True
else:
return False
def match_named_block(self):
namedmatch = re.compile(r"(<%(def|method))(.*?)(>)(.*?)(</%\2>)", re.M | re.S)
match = self._match(namedmatch)
if match:
self.colorize([(match.group(1), 'deftag')])
self.colorize([(match.group(3), 'compname')])
self.colorize([(match.group(4), 'deftag')])
MyghtyHighlighter(match.group(5), self.output).highlight()
self.colorize([(match.group(6), 'deftag')])
return True
else:
return False
def match_block(self):
blockmatch = re.compile(r"(<%(\w+).*?>)(.*?)(</%\2\s*>)", re.M | re.S)
match = self._match(blockmatch)
if match:
style = {
'doc': 'doctag',
'args': 'argstag',
}.setdefault(match.group(2), "blocktag")
self.colorize([(match.group(1), style)])
if style == 'doctag':
self.colorize([(match.group(3), 'doctag_text')])
else:
self.pythonize(match.group(3))
self.colorize([(match.group(4), style)])
return True
else:
return False
def match_comp_call(self):
compmatch = re.compile(r"(<&[^|])(.*?)(,.*?)?(&>)", re.M)
match = self._match(compmatch)
if match:
self.colorize([(match.group(1), 'compcall')])
self.colorize([(match.group(2), 'compname')])
if match.group(3) is not None:
self.pythonize(match.group(3))
self.colorize([(match.group(4), 'compcall')])
return True
else:
return False
def match_substitution(self):
submatch = re.compile(r"(<%)(.*?)(%>)", re.M)
match = self._match(submatch)
if match:
self.colorize([(match.group(1), 'substitution')])
self.pythonize(match.group(2))
self.colorize([(match.group(3), 'substitution')])
return True
else:
return False
def match_comp_content_call(self):
compcontmatch = re.compile(r"(<&\|)(.*?)(,.*?)?(&>)|(</&>)", re.M | re.S)
match = self._match(compcontmatch)
if match:
if match.group(5) is not None:
self.colorize([(match.group(5), 'compcall')])
else:
self.colorize([(match.group(1), 'compcall')])
self.colorize([(match.group(2), 'compname')])
if match.group(3) is not None:
self.pythonize(match.group(3))
self.colorize([(match.group(4), 'compcall')])
return True
else:
return False
def match_line(self):
linematch = re.compile(r"(?<=^)([%#])([^\n]*)(\n|\Z)", re.M)
match = self._match(linematch)
if match:
if match.group(1) == '#':
self.colorize([(match.group(0), 'doctag')])
else:
#self.colorize([(match.group(0), 'doctag')])
self.colorize([(match.group(1), 'controlline')])
self.pythonize(match.group(2))
self.output.write(match.group(3))
return True
else:
return False
highlighters = {
'.myt': MyghtyHighlighter,
'.myc': MyghtyHighlighter,
'autohandler' : MyghtyHighlighter,
'dhandler': MyghtyHighlighter,
'.py': PythonHighlighter,
'myghty': MyghtyHighlighter,
'python' : PythonHighlighter
}
|