##// END OF EJS Templates
Some cleanup of Pycolorize....
Matthias Bussonnier -
Show More
@@ -29,7 +29,7 b' scan Python source code and re-emit it with no changes to its original'
29 formatting (which is the hard part).
29 formatting (which is the hard part).
30 """
30 """
31
31
32 __all__ = ['ANSICodeColors','Parser']
32 __all__ = ['ANSICodeColors', 'Parser']
33
33
34 _scheme_default = 'Linux'
34 _scheme_default = 'Linux'
35
35
@@ -43,7 +43,7 b' import tokenize'
43
43
44 generate_tokens = tokenize.generate_tokens
44 generate_tokens = tokenize.generate_tokens
45
45
46 from IPython.utils.coloransi import TermColors, InputTermColors ,ColorScheme, ColorSchemeTable
46 from IPython.utils.coloransi import TermColors, InputTermColors,ColorScheme, ColorSchemeTable
47 from .colorable import Colorable
47 from .colorable import Colorable
48 from io import StringIO
48 from io import StringIO
49
49
@@ -185,8 +185,11 b' class Parser(Colorable):'
185
185
186 super(Parser, self).__init__(parent=parent)
186 super(Parser, self).__init__(parent=parent)
187
187
188 self.color_table = color_table and color_table or ANSICodeColors
188 self.color_table = color_table if color_table else ANSICodeColors
189 self.out = out
189 self.out = out
190 self.pos = None
191 self.lines = None
192 self.raw = None
190 if not style:
193 if not style:
191 self.style = self.default_style
194 self.style = self.default_style
192 else:
195 else:
@@ -231,9 +234,8 b' class Parser(Colorable):'
231 error = False
234 error = False
232 self.out.write(raw)
235 self.out.write(raw)
233 if string_output:
236 if string_output:
234 return raw,error
237 return raw, error
235 else:
238 return None, error
236 return None,error
237
239
238 # local shorthands
240 # local shorthands
239 colors = self.color_table[self.style].colors
241 colors = self.color_table[self.style].colors
@@ -247,9 +249,10 b' class Parser(Colorable):'
247 pos = 0
249 pos = 0
248 raw_find = self.raw.find
250 raw_find = self.raw.find
249 lines_append = self.lines.append
251 lines_append = self.lines.append
250 while 1:
252 while True:
251 pos = raw_find('\n', pos) + 1
253 pos = raw_find('\n', pos) + 1
252 if not pos: break
254 if not pos:
255 break
253 lines_append(pos)
256 lines_append(pos)
254 lines_append(len(self.raw))
257 lines_append(len(self.raw))
255
258
@@ -277,11 +280,11 b' class Parser(Colorable):'
277 return (output, error)
280 return (output, error)
278 return (None, error)
281 return (None, error)
279
282
280 def _inner_call_(self, toktype, toktext, start_pos, end_pos, line):
283
284 def _inner_call_(self, toktype, toktext, start_pos):
281 """like call but write to a temporary buffer"""
285 """like call but write to a temporary buffer"""
282 buff = StringIO()
286 buff = StringIO()
283 (srow,scol) = start_pos
287 srow, scol = start_pos
284 (erow,ecol) = end_pos
285 colors = self.colors
288 colors = self.colors
286 owrite = buff.write
289 owrite = buff.write
287
290
@@ -310,8 +313,6 b' class Parser(Colorable):'
310 toktype = _KEYWORD
313 toktype = _KEYWORD
311 color = colors.get(toktype, colors[_TEXT])
314 color = colors.get(toktype, colors[_TEXT])
312
315
313 #print '<%s>' % toktext, # dbg
314
315 # Triple quoted strings must be handled carefully so that backtracking
316 # Triple quoted strings must be handled carefully so that backtracking
316 # in pagers works correctly. We need color terminators on _each_ line.
317 # in pagers works correctly. We need color terminators on _each_ line.
317 if linesep in toktext:
318 if linesep in toktext:
@@ -327,5 +328,4 b' class Parser(Colorable):'
327 def __call__(self, toktype, toktext, start_pos, end_pos, line):
328 def __call__(self, toktype, toktext, start_pos, end_pos, line):
328 """ Token handler, with syntax highlighting."""
329 """ Token handler, with syntax highlighting."""
329 self.out.write(
330 self.out.write(
330 self._inner_call_(toktype, toktext, start_pos, end_pos, line))
331 self._inner_call_(toktype, toktext, start_pos))
331
General Comments 0
You need to be logged in to leave comments. Login now