pygmentize 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (101) hide show
  1. data/LICENSE +19 -0
  2. data/lib/pygments.rb +23 -0
  3. data/pygmentize.gemspec +11 -0
  4. data/test/pygments.rb +19 -0
  5. data/vendor/pygmentize.py +7 -0
  6. data/vendor/pygments/AUTHORS +73 -0
  7. data/vendor/pygments/LICENSE +25 -0
  8. data/vendor/pygments/__init__.py +91 -0
  9. data/vendor/pygments/__init__.pyc +0 -0
  10. data/vendor/pygments/cmdline.py +430 -0
  11. data/vendor/pygments/cmdline.pyc +0 -0
  12. data/vendor/pygments/console.py +74 -0
  13. data/vendor/pygments/console.pyc +0 -0
  14. data/vendor/pygments/filter.py +74 -0
  15. data/vendor/pygments/filter.pyc +0 -0
  16. data/vendor/pygments/filters/__init__.py +357 -0
  17. data/vendor/pygments/filters/__init__.pyc +0 -0
  18. data/vendor/pygments/formatter.py +92 -0
  19. data/vendor/pygments/formatter.pyc +0 -0
  20. data/vendor/pygments/formatters/__init__.py +68 -0
  21. data/vendor/pygments/formatters/__init__.pyc +0 -0
  22. data/vendor/pygments/formatters/_mapping.py +92 -0
  23. data/vendor/pygments/formatters/_mapping.pyc +0 -0
  24. data/vendor/pygments/formatters/bbcode.py +109 -0
  25. data/vendor/pygments/formatters/bbcode.pyc +0 -0
  26. data/vendor/pygments/formatters/html.py +723 -0
  27. data/vendor/pygments/formatters/html.pyc +0 -0
  28. data/vendor/pygments/formatters/img.py +553 -0
  29. data/vendor/pygments/formatters/img.pyc +0 -0
  30. data/vendor/pygments/formatters/latex.py +354 -0
  31. data/vendor/pygments/formatters/latex.pyc +0 -0
  32. data/vendor/pygments/formatters/other.py +117 -0
  33. data/vendor/pygments/formatters/other.pyc +0 -0
  34. data/vendor/pygments/formatters/rtf.py +136 -0
  35. data/vendor/pygments/formatters/rtf.pyc +0 -0
  36. data/vendor/pygments/formatters/svg.py +154 -0
  37. data/vendor/pygments/formatters/svg.pyc +0 -0
  38. data/vendor/pygments/formatters/terminal.py +109 -0
  39. data/vendor/pygments/formatters/terminal.pyc +0 -0
  40. data/vendor/pygments/formatters/terminal256.py +219 -0
  41. data/vendor/pygments/formatters/terminal256.pyc +0 -0
  42. data/vendor/pygments/lexer.py +660 -0
  43. data/vendor/pygments/lexer.pyc +0 -0
  44. data/vendor/pygments/lexers/__init__.py +226 -0
  45. data/vendor/pygments/lexers/__init__.pyc +0 -0
  46. data/vendor/pygments/lexers/_asybuiltins.py +1645 -0
  47. data/vendor/pygments/lexers/_clbuiltins.py +232 -0
  48. data/vendor/pygments/lexers/_luabuiltins.py +256 -0
  49. data/vendor/pygments/lexers/_mapping.py +234 -0
  50. data/vendor/pygments/lexers/_mapping.pyc +0 -0
  51. data/vendor/pygments/lexers/_phpbuiltins.py +3389 -0
  52. data/vendor/pygments/lexers/_vimbuiltins.py +3 -0
  53. data/vendor/pygments/lexers/agile.py +1485 -0
  54. data/vendor/pygments/lexers/agile.pyc +0 -0
  55. data/vendor/pygments/lexers/asm.py +353 -0
  56. data/vendor/pygments/lexers/compiled.py +2365 -0
  57. data/vendor/pygments/lexers/dotnet.py +355 -0
  58. data/vendor/pygments/lexers/functional.py +756 -0
  59. data/vendor/pygments/lexers/functional.pyc +0 -0
  60. data/vendor/pygments/lexers/math.py +461 -0
  61. data/vendor/pygments/lexers/other.py +2297 -0
  62. data/vendor/pygments/lexers/parsers.py +695 -0
  63. data/vendor/pygments/lexers/special.py +100 -0
  64. data/vendor/pygments/lexers/special.pyc +0 -0
  65. data/vendor/pygments/lexers/templates.py +1387 -0
  66. data/vendor/pygments/lexers/text.py +1586 -0
  67. data/vendor/pygments/lexers/web.py +1619 -0
  68. data/vendor/pygments/lexers/web.pyc +0 -0
  69. data/vendor/pygments/plugin.py +74 -0
  70. data/vendor/pygments/plugin.pyc +0 -0
  71. data/vendor/pygments/scanner.py +104 -0
  72. data/vendor/pygments/style.py +117 -0
  73. data/vendor/pygments/style.pyc +0 -0
  74. data/vendor/pygments/styles/__init__.py +68 -0
  75. data/vendor/pygments/styles/__init__.pyc +0 -0
  76. data/vendor/pygments/styles/autumn.py +65 -0
  77. data/vendor/pygments/styles/borland.py +51 -0
  78. data/vendor/pygments/styles/bw.py +49 -0
  79. data/vendor/pygments/styles/colorful.py +81 -0
  80. data/vendor/pygments/styles/default.py +73 -0
  81. data/vendor/pygments/styles/default.pyc +0 -0
  82. data/vendor/pygments/styles/emacs.py +72 -0
  83. data/vendor/pygments/styles/friendly.py +72 -0
  84. data/vendor/pygments/styles/fruity.py +43 -0
  85. data/vendor/pygments/styles/manni.py +75 -0
  86. data/vendor/pygments/styles/monokai.py +106 -0
  87. data/vendor/pygments/styles/murphy.py +80 -0
  88. data/vendor/pygments/styles/native.py +65 -0
  89. data/vendor/pygments/styles/pastie.py +75 -0
  90. data/vendor/pygments/styles/perldoc.py +69 -0
  91. data/vendor/pygments/styles/tango.py +141 -0
  92. data/vendor/pygments/styles/trac.py +63 -0
  93. data/vendor/pygments/styles/vim.py +63 -0
  94. data/vendor/pygments/styles/vs.py +38 -0
  95. data/vendor/pygments/token.py +198 -0
  96. data/vendor/pygments/token.pyc +0 -0
  97. data/vendor/pygments/unistring.py +130 -0
  98. data/vendor/pygments/unistring.pyc +0 -0
  99. data/vendor/pygments/util.py +226 -0
  100. data/vendor/pygments/util.pyc +0 -0
  101. metadata +166 -0
@@ -0,0 +1,354 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ pygments.formatters.latex
4
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
5
+
6
+ Formatter for LaTeX fancyvrb output.
7
+
8
+ :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
9
+ :license: BSD, see LICENSE for details.
10
+ """
11
+
12
+ from pygments.formatter import Formatter
13
+ from pygments.token import Token, STANDARD_TYPES
14
+ from pygments.util import get_bool_opt, get_int_opt, StringIO
15
+
16
+
17
+ __all__ = ['LatexFormatter']
18
+
19
+
20
+ def escape_tex(text, commandprefix):
21
+ return text.replace('\\', '\x00'). \
22
+ replace('{', '\x01'). \
23
+ replace('}', '\x02'). \
24
+ replace('^', '\x03'). \
25
+ replace('_', '\x04'). \
26
+ replace('\x00', r'\%sZbs{}' % commandprefix). \
27
+ replace('\x01', r'\%sZob{}' % commandprefix). \
28
+ replace('\x02', r'\%sZcb{}' % commandprefix). \
29
+ replace('\x03', r'\%sZca{}' % commandprefix). \
30
+ replace('\x04', r'\%sZus{}' % commandprefix)
31
+
32
+
33
+ DOC_TEMPLATE = r'''
34
+ \documentclass{%(docclass)s}
35
+ \usepackage{fancyvrb}
36
+ \usepackage{color}
37
+ \usepackage[%(encoding)s]{inputenc}
38
+ %(preamble)s
39
+
40
+ %(styledefs)s
41
+
42
+ \begin{document}
43
+
44
+ \section*{%(title)s}
45
+
46
+ %(code)s
47
+ \end{document}
48
+ '''
49
+
50
+ ## Small explanation of the mess below :)
51
+ #
52
+ # The previous version of the LaTeX formatter just assigned a command to
53
+ # each token type defined in the current style. That obviously is
54
+ # problematic if the highlighted code is produced for a different style
55
+ # than the style commands themselves.
56
+ #
57
+ # This version works much like the HTML formatter which assigns multiple
58
+ # CSS classes to each <span> tag, from the most specific to the least
59
+ # specific token type, thus falling back to the parent token type if one
60
+ # is not defined. Here, the classes are there too and use the same short
61
+ # forms given in token.STANDARD_TYPES.
62
+ #
63
+ # Highlighted code now only uses one custom command, which by default is
64
+ # \PY and selectable by the commandprefix option (and in addition the
65
+ # escapes \PYZat, \PYZlb and \PYZrb which haven't been renamed for
66
+ # backwards compatibility purposes).
67
+ #
68
+ # \PY has two arguments: the classes, separated by +, and the text to
69
+ # render in that style. The classes are resolved into the respective
70
+ # style commands by magic, which serves to ignore unknown classes.
71
+ #
72
+ # The magic macros are:
73
+ # * \PY@it, \PY@bf, etc. are unconditionally wrapped around the text
74
+ # to render in \PY@do. Their definition determines the style.
75
+ # * \PY@reset resets \PY@it etc. to do nothing.
76
+ # * \PY@toks parses the list of classes, using magic inspired by the
77
+ # keyval package (but modified to use plusses instead of commas
78
+ # because fancyvrb redefines commas inside its environments).
79
+ # * \PY@tok processes one class, calling the \PY@tok@classname command
80
+ # if it exists.
81
+ # * \PY@tok@classname sets the \PY@it etc. to reflect the chosen style
82
+ # for its class.
83
+ # * \PY resets the style, parses the classnames and then calls \PY@do.
84
+
85
+ STYLE_TEMPLATE = r'''
86
+ \makeatletter
87
+ \def\%(cp)s@reset{\let\%(cp)s@it=\relax \let\%(cp)s@bf=\relax%%
88
+ \let\%(cp)s@ul=\relax \let\%(cp)s@tc=\relax%%
89
+ \let\%(cp)s@bc=\relax \let\%(cp)s@ff=\relax}
90
+ \def\%(cp)s@tok#1{\csname %(cp)s@tok@#1\endcsname}
91
+ \def\%(cp)s@toks#1+{\ifx\relax#1\empty\else%%
92
+ \%(cp)s@tok{#1}\expandafter\%(cp)s@toks\fi}
93
+ \def\%(cp)s@do#1{\%(cp)s@bc{\%(cp)s@tc{\%(cp)s@ul{%%
94
+ \%(cp)s@it{\%(cp)s@bf{\%(cp)s@ff{#1}}}}}}}
95
+ \def\%(cp)s#1#2{\%(cp)s@reset\%(cp)s@toks#1+\relax+\%(cp)s@do{#2}}
96
+
97
+ %(styles)s
98
+
99
+ \def\%(cp)sZbs{\char`\\}
100
+ \def\%(cp)sZus{\char`\_}
101
+ \def\%(cp)sZob{\char`\{}
102
+ \def\%(cp)sZcb{\char`\}}
103
+ \def\%(cp)sZca{\char`\^}
104
+ %% for compatibility with earlier versions
105
+ \def\%(cp)sZat{@}
106
+ \def\%(cp)sZlb{[}
107
+ \def\%(cp)sZrb{]}
108
+ \makeatother
109
+ '''
110
+
111
+
112
+ def _get_ttype_name(ttype):
113
+ fname = STANDARD_TYPES.get(ttype)
114
+ if fname:
115
+ return fname
116
+ aname = ''
117
+ while fname is None:
118
+ aname = ttype[-1] + aname
119
+ ttype = ttype.parent
120
+ fname = STANDARD_TYPES.get(ttype)
121
+ return fname + aname
122
+
123
+
124
+ class LatexFormatter(Formatter):
125
+ r"""
126
+ Format tokens as LaTeX code. This needs the `fancyvrb` and `color`
127
+ standard packages.
128
+
129
+ Without the `full` option, code is formatted as one ``Verbatim``
130
+ environment, like this:
131
+
132
+ .. sourcecode:: latex
133
+
134
+ \begin{Verbatim}[commandchars=@\[\]]
135
+ @PY[k][def ]@PY[n+nf][foo](@PY[n][bar]):
136
+ @PY[k][pass]
137
+ \end{Verbatim}
138
+
139
+ The special command used here (``@PY``) and all the other macros it needs
140
+ are output by the `get_style_defs` method.
141
+
142
+ With the `full` option, a complete LaTeX document is output, including
143
+ the command definitions in the preamble.
144
+
145
+ The `get_style_defs()` method of a `LatexFormatter` returns a string
146
+ containing ``\def`` commands defining the macros needed inside the
147
+ ``Verbatim`` environments.
148
+
149
+ Additional options accepted:
150
+
151
+ `style`
152
+ The style to use, can be a string or a Style subclass (default:
153
+ ``'default'``).
154
+
155
+ `full`
156
+ Tells the formatter to output a "full" document, i.e. a complete
157
+ self-contained document (default: ``False``).
158
+
159
+ `title`
160
+ If `full` is true, the title that should be used to caption the
161
+ document (default: ``''``).
162
+
163
+ `docclass`
164
+ If the `full` option is enabled, this is the document class to use
165
+ (default: ``'article'``).
166
+
167
+ `preamble`
168
+ If the `full` option is enabled, this can be further preamble commands,
169
+ e.g. ``\usepackage`` (default: ``''``).
170
+
171
+ `linenos`
172
+ If set to ``True``, output line numbers (default: ``False``).
173
+
174
+ `linenostart`
175
+ The line number for the first line (default: ``1``).
176
+
177
+ `linenostep`
178
+ If set to a number n > 1, only every nth line number is printed.
179
+
180
+ `verboptions`
181
+ Additional options given to the Verbatim environment (see the *fancyvrb*
182
+ docs for possible values) (default: ``''``).
183
+
184
+ `commandprefix`
185
+ The LaTeX commands used to produce colored output are constructed
186
+ using this prefix and some letters (default: ``'PY'``).
187
+ *New in Pygments 0.7.*
188
+
189
+ *New in Pygments 0.10:* the default is now ``'PY'`` instead of ``'C'``.
190
+
191
+ `texcomments`
192
+ If set to ``True``, enables LaTeX comment lines. That is, LaTex markup
193
+ in comment tokens is not escaped so that LaTeX can render it (default:
194
+ ``False``). *New in Pygments 1.2.*
195
+
196
+ `mathescape`
197
+ If set to ``True``, enables LaTeX math mode escape in comments. That
198
+ is, ``'$...$'`` inside a comment will trigger math mode (default:
199
+ ``False``). *New in Pygments 1.2.*
200
+ """
201
+ name = 'LaTeX'
202
+ aliases = ['latex', 'tex']
203
+ filenames = ['*.tex']
204
+
205
+ def __init__(self, **options):
206
+ Formatter.__init__(self, **options)
207
+ self.docclass = options.get('docclass', 'article')
208
+ self.preamble = options.get('preamble', '')
209
+ self.linenos = get_bool_opt(options, 'linenos', False)
210
+ self.linenostart = abs(get_int_opt(options, 'linenostart', 1))
211
+ self.linenostep = abs(get_int_opt(options, 'linenostep', 1))
212
+ self.verboptions = options.get('verboptions', '')
213
+ self.nobackground = get_bool_opt(options, 'nobackground', False)
214
+ self.commandprefix = options.get('commandprefix', 'PY')
215
+ self.texcomments = get_bool_opt(options, 'texcomments', False)
216
+ self.mathescape = get_bool_opt(options, 'mathescape', False)
217
+
218
+ self._create_stylesheet()
219
+
220
+
221
+ def _create_stylesheet(self):
222
+ t2n = self.ttype2name = {Token: ''}
223
+ c2d = self.cmd2def = {}
224
+ cp = self.commandprefix
225
+
226
+ def rgbcolor(col):
227
+ if col:
228
+ return ','.join(['%.2f' %(int(col[i] + col[i + 1], 16) / 255.0)
229
+ for i in (0, 2, 4)])
230
+ else:
231
+ return '1,1,1'
232
+
233
+ for ttype, ndef in self.style:
234
+ name = _get_ttype_name(ttype)
235
+ cmndef = ''
236
+ if ndef['bold']:
237
+ cmndef += r'\let\$$@bf=\textbf'
238
+ if ndef['italic']:
239
+ cmndef += r'\let\$$@it=\textit'
240
+ if ndef['underline']:
241
+ cmndef += r'\let\$$@ul=\underline'
242
+ if ndef['roman']:
243
+ cmndef += r'\let\$$@ff=\textrm'
244
+ if ndef['sans']:
245
+ cmndef += r'\let\$$@ff=\textsf'
246
+ if ndef['mono']:
247
+ cmndef += r'\let\$$@ff=\textsf'
248
+ if ndef['color']:
249
+ cmndef += (r'\def\$$@tc##1{\textcolor[rgb]{%s}{##1}}' %
250
+ rgbcolor(ndef['color']))
251
+ if ndef['border']:
252
+ cmndef += (r'\def\$$@bc##1{\fcolorbox[rgb]{%s}{%s}{##1}}' %
253
+ (rgbcolor(ndef['border']),
254
+ rgbcolor(ndef['bgcolor'])))
255
+ elif ndef['bgcolor']:
256
+ cmndef += (r'\def\$$@bc##1{\colorbox[rgb]{%s}{##1}}' %
257
+ rgbcolor(ndef['bgcolor']))
258
+ if cmndef == '':
259
+ continue
260
+ cmndef = cmndef.replace('$$', cp)
261
+ t2n[ttype] = name
262
+ c2d[name] = cmndef
263
+
264
+ def get_style_defs(self, arg=''):
265
+ """
266
+ Return the command sequences needed to define the commands
267
+ used to format text in the verbatim environment. ``arg`` is ignored.
268
+ """
269
+ cp = self.commandprefix
270
+ styles = []
271
+ for name, definition in self.cmd2def.iteritems():
272
+ styles.append(r'\def\%s@tok@%s{%s}' % (cp, name, definition))
273
+ return STYLE_TEMPLATE % {'cp': self.commandprefix,
274
+ 'styles': '\n'.join(styles)}
275
+
276
+ def format_unencoded(self, tokensource, outfile):
277
+ # TODO: add support for background colors
278
+ t2n = self.ttype2name
279
+ cp = self.commandprefix
280
+
281
+ if self.full:
282
+ realoutfile = outfile
283
+ outfile = StringIO()
284
+
285
+ outfile.write(r'\begin{Verbatim}[commandchars=\\\{\}')
286
+ if self.linenos:
287
+ start, step = self.linenostart, self.linenostep
288
+ outfile.write(',numbers=left' +
289
+ (start and ',firstnumber=%d' % start or '') +
290
+ (step and ',stepnumber=%d' % step or ''))
291
+ if self.mathescape or self.texcomments:
292
+ outfile.write(r',codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8}')
293
+ if self.verboptions:
294
+ outfile.write(',' + self.verboptions)
295
+ outfile.write(']\n')
296
+
297
+ for ttype, value in tokensource:
298
+ if ttype in Token.Comment:
299
+ if self.texcomments:
300
+ # Try to guess comment starting lexeme and escape it ...
301
+ start = value[0:1]
302
+ for i in xrange(1, len(value)):
303
+ if start[0] != value[i]:
304
+ break
305
+ start += value[i]
306
+
307
+ value = value[len(start):]
308
+ start = escape_tex(start, self.commandprefix)
309
+
310
+ # ... but do not escape inside comment.
311
+ value = start + value
312
+ elif self.mathescape:
313
+ # Only escape parts not inside a math environment.
314
+ parts = value.split('$')
315
+ in_math = False
316
+ for i, part in enumerate(parts):
317
+ if not in_math:
318
+ parts[i] = escape_tex(part, self.commandprefix)
319
+ in_math = not in_math
320
+ value = '$'.join(parts)
321
+ else:
322
+ value = escape_tex(value, self.commandprefix)
323
+ else:
324
+ value = escape_tex(value, self.commandprefix)
325
+ styles = []
326
+ while ttype is not Token:
327
+ try:
328
+ styles.append(t2n[ttype])
329
+ except KeyError:
330
+ # not in current style
331
+ styles.append(_get_ttype_name(ttype))
332
+ ttype = ttype.parent
333
+ styleval = '+'.join(reversed(styles))
334
+ if styleval:
335
+ spl = value.split('\n')
336
+ for line in spl[:-1]:
337
+ if line:
338
+ outfile.write("\\%s{%s}{%s}" % (cp, styleval, line))
339
+ outfile.write('\n')
340
+ if spl[-1]:
341
+ outfile.write("\\%s{%s}{%s}" % (cp, styleval, spl[-1]))
342
+ else:
343
+ outfile.write(value)
344
+
345
+ outfile.write('\\end{Verbatim}\n')
346
+
347
+ if self.full:
348
+ realoutfile.write(DOC_TEMPLATE %
349
+ dict(docclass = self.docclass,
350
+ preamble = self.preamble,
351
+ title = self.title,
352
+ encoding = self.encoding or 'latin1',
353
+ styledefs = self.get_style_defs(),
354
+ code = outfile.getvalue()))
@@ -0,0 +1,117 @@
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ pygments.formatters.other
4
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
5
+
6
+ Other formatters: NullFormatter, RawTokenFormatter.
7
+
8
+ :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
9
+ :license: BSD, see LICENSE for details.
10
+ """
11
+
12
+ from pygments.formatter import Formatter
13
+ from pygments.util import OptionError, get_choice_opt, b
14
+ from pygments.token import Token
15
+ from pygments.console import colorize
16
+
17
+ __all__ = ['NullFormatter', 'RawTokenFormatter']
18
+
19
+
20
+ class NullFormatter(Formatter):
21
+ """
22
+ Output the text unchanged without any formatting.
23
+ """
24
+ name = 'Text only'
25
+ aliases = ['text', 'null']
26
+ filenames = ['*.txt']
27
+
28
+ def format(self, tokensource, outfile):
29
+ enc = self.encoding
30
+ for ttype, value in tokensource:
31
+ if enc:
32
+ outfile.write(value.encode(enc))
33
+ else:
34
+ outfile.write(value)
35
+
36
+
37
+ class RawTokenFormatter(Formatter):
38
+ r"""
39
+ Format tokens as a raw representation for storing token streams.
40
+
41
+ The format is ``tokentype<TAB>repr(tokenstring)\n``. The output can later
42
+ be converted to a token stream with the `RawTokenLexer`, described in the
43
+ `lexer list <lexers.txt>`_.
44
+
45
+ Only two options are accepted:
46
+
47
+ `compress`
48
+ If set to ``'gz'`` or ``'bz2'``, compress the output with the given
49
+ compression algorithm after encoding (default: ``''``).
50
+ `error_color`
51
+ If set to a color name, highlight error tokens using that color. If
52
+ set but with no value, defaults to ``'red'``.
53
+ *New in Pygments 0.11.*
54
+
55
+ """
56
+ name = 'Raw tokens'
57
+ aliases = ['raw', 'tokens']
58
+ filenames = ['*.raw']
59
+
60
+ unicodeoutput = False
61
+
62
+ def __init__(self, **options):
63
+ Formatter.__init__(self, **options)
64
+ if self.encoding:
65
+ raise OptionError('the raw formatter does not support the '
66
+ 'encoding option')
67
+ self.encoding = 'ascii' # let pygments.format() do the right thing
68
+ self.compress = get_choice_opt(options, 'compress',
69
+ ['', 'none', 'gz', 'bz2'], '')
70
+ self.error_color = options.get('error_color', None)
71
+ if self.error_color is True:
72
+ self.error_color = 'red'
73
+ if self.error_color is not None:
74
+ try:
75
+ colorize(self.error_color, '')
76
+ except KeyError:
77
+ raise ValueError("Invalid color %r specified" %
78
+ self.error_color)
79
+
80
+ def format(self, tokensource, outfile):
81
+ try:
82
+ outfile.write(b(''))
83
+ except TypeError:
84
+ raise TypeError('The raw tokens formatter needs a binary '
85
+ 'output file')
86
+ if self.compress == 'gz':
87
+ import gzip
88
+ outfile = gzip.GzipFile('', 'wb', 9, outfile)
89
+ def write(text):
90
+ outfile.write(text.encode())
91
+ flush = outfile.flush
92
+ elif self.compress == 'bz2':
93
+ import bz2
94
+ compressor = bz2.BZ2Compressor(9)
95
+ def write(text):
96
+ outfile.write(compressor.compress(text.encode()))
97
+ def flush():
98
+ outfile.write(compressor.flush())
99
+ outfile.flush()
100
+ else:
101
+ def write(text):
102
+ outfile.write(text.encode())
103
+ flush = outfile.flush
104
+
105
+ lasttype = None
106
+ lastval = u''
107
+ if self.error_color:
108
+ for ttype, value in tokensource:
109
+ line = "%s\t%r\n" % (ttype, value)
110
+ if ttype is Token.Error:
111
+ write(colorize(self.error_color, line))
112
+ else:
113
+ write(line)
114
+ else:
115
+ for ttype, value in tokensource:
116
+ write("%s\t%r\n" % (ttype, value))
117
+ flush()