3 pygments.formatters.other |
3 pygments.formatters.other |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~ |
4 ~~~~~~~~~~~~~~~~~~~~~~~~~ |
5 |
5 |
6 Other formatters: NullFormatter, RawTokenFormatter. |
6 Other formatters: NullFormatter, RawTokenFormatter. |
7 |
7 |
8 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS. |
8 :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS. |
9 :license: BSD, see LICENSE for details. |
9 :license: BSD, see LICENSE for details. |
10 """ |
10 """ |
11 |
11 |
12 from pygments.formatter import Formatter |
12 from pygments.formatter import Formatter |
13 from pygments.util import OptionError, get_choice_opt |
13 from pygments.util import get_choice_opt |
14 from pygments.token import Token |
14 from pygments.token import Token |
15 from pygments.console import colorize |
15 from pygments.console import colorize |
16 |
16 |
17 __all__ = ['NullFormatter', 'RawTokenFormatter', 'TestcaseFormatter'] |
17 __all__ = ['NullFormatter', 'RawTokenFormatter', 'TestcaseFormatter'] |
18 |
18 |
85 raise TypeError('The raw tokens formatter needs a binary ' |
85 raise TypeError('The raw tokens formatter needs a binary ' |
86 'output file') |
86 'output file') |
87 if self.compress == 'gz': |
87 if self.compress == 'gz': |
88 import gzip |
88 import gzip |
89 outfile = gzip.GzipFile('', 'wb', 9, outfile) |
89 outfile = gzip.GzipFile('', 'wb', 9, outfile) |
|
90 |
90 def write(text): |
91 def write(text): |
91 outfile.write(text.encode()) |
92 outfile.write(text.encode()) |
92 flush = outfile.flush |
93 flush = outfile.flush |
93 elif self.compress == 'bz2': |
94 elif self.compress == 'bz2': |
94 import bz2 |
95 import bz2 |
95 compressor = bz2.BZ2Compressor(9) |
96 compressor = bz2.BZ2Compressor(9) |
|
97 |
96 def write(text): |
98 def write(text): |
97 outfile.write(compressor.compress(text.encode())) |
99 outfile.write(compressor.compress(text.encode())) |
|
100 |
98 def flush(): |
101 def flush(): |
99 outfile.write(compressor.flush()) |
102 outfile.write(compressor.flush()) |
100 outfile.flush() |
103 outfile.flush() |
101 else: |
104 else: |
102 def write(text): |
105 def write(text): |
113 else: |
116 else: |
114 for ttype, value in tokensource: |
117 for ttype, value in tokensource: |
115 write("%s\t%r\n" % (ttype, value)) |
118 write("%s\t%r\n" % (ttype, value)) |
116 flush() |
119 flush() |
117 |
120 |
|
121 |
118 TESTCASE_BEFORE = u'''\ |
122 TESTCASE_BEFORE = u'''\ |
119 def testNeedsName(self): |
123 def testNeedsName(lexer): |
120 fragment = %r |
124 fragment = %r |
121 tokens = [ |
125 tokens = [ |
122 ''' |
126 ''' |
123 TESTCASE_AFTER = u'''\ |
127 TESTCASE_AFTER = u'''\ |
124 ] |
128 ] |
125 self.assertEqual(tokens, list(self.lexer.get_tokens(fragment))) |
129 assert list(lexer.get_tokens(fragment)) == tokens |
126 ''' |
130 ''' |
127 |
131 |
128 |
132 |
129 class TestcaseFormatter(Formatter): |
133 class TestcaseFormatter(Formatter): |
130 """ |
134 """ |