|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.formatters.other |
|
4 ~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Other formatters: NullFormatter, RawTokenFormatter. |
|
7 |
|
8 :copyright: Copyright 2006-2009 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() |