2 pygments.formatters.other
3 ~~~~~~~~~~~~~~~~~~~~~~~~~
5 Other formatters: NullFormatter, RawTokenFormatter.
7 :copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
11 from pip._vendor.pygments.formatter import Formatter
12 from pip._vendor.pygments.util import get_choice_opt
13 from pip._vendor.pygments.token import Token
14 from pip._vendor.pygments.console import colorize
16 __all__ = ['NullFormatter', 'RawTokenFormatter', 'TestcaseFormatter']
19 class NullFormatter(Formatter):
21 Output the text unchanged without any formatting.
24 aliases = ['text', 'null']
27 def format(self, tokensource, outfile):
29 for ttype, value in tokensource:
31 outfile.write(value.encode(enc))
36 class RawTokenFormatter(Formatter):
38 Format tokens as a raw representation for storing token streams.
40 The format is ``tokentype<TAB>repr(tokenstring)\n``. The output can later
41 be converted to a token stream with the `RawTokenLexer`, described in the
42 :doc:`lexer list <lexers>`.
44 Only two options are accepted:
47 If set to ``'gz'`` or ``'bz2'``, compress the output with the given
48 compression algorithm after encoding (default: ``''``).
50 If set to a color name, highlight error tokens using that color. If
51 set but with no value, defaults to ``'red'``.
53 .. versionadded:: 0.11
57 aliases = ['raw', 'tokens']
62 def __init__(self, **options):
63 Formatter.__init__(self, **options)
64 # We ignore self.encoding if it is set, since it gets set for lexer
65 # and formatter if given with -Oencoding on the command line.
66 # The RawTokenFormatter outputs only ASCII. Override here.
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:
75 colorize(self.error_color, '')
77 raise ValueError("Invalid color %r specified" %
80 def format(self, tokensource, outfile):
84 raise TypeError('The raw tokens formatter needs a binary '
86 if self.compress == 'gz':
88 outfile = gzip.GzipFile('', 'wb', 9, outfile)
92 elif self.compress == 'bz2':
94 compressor = bz2.BZ2Compressor(9)
97 outfile.write(compressor.compress(text))
100 outfile.write(compressor.flush())
103 write = outfile.write
104 flush = outfile.flush
107 for ttype, value in tokensource:
108 line = b"%r\t%r\n" % (ttype, value)
109 if ttype is Token.Error:
110 write(colorize(self.error_color, line))
114 for ttype, value in tokensource:
115 write(b"%r\t%r\n" % (ttype, value))
119 TESTCASE_BEFORE = '''\
120 def testNeedsName(lexer):
124 TESTCASE_AFTER = '''\
126 assert list(lexer.get_tokens(fragment)) == tokens
130 class TestcaseFormatter(Formatter):
132 Format tokens as appropriate for a new testcase.
134 .. versionadded:: 2.0
137 aliases = ['testcase']
139 def __init__(self, **options):
140 Formatter.__init__(self, **options)
141 if self.encoding is not None and self.encoding != 'utf-8':
142 raise ValueError("Only None and utf-8 are allowed encodings.")
144 def format(self, tokensource, outfile):
145 indentation = ' ' * 12
148 for ttype, value in tokensource:
150 outbuf.append('%s(%s, %r),\n' % (indentation, ttype, value))
152 before = TESTCASE_BEFORE % (''.join(rawbuf),)
153 during = ''.join(outbuf)
154 after = TESTCASE_AFTER
155 if self.encoding is None:
156 outfile.write(before + during + after)
158 outfile.write(before.encode('utf-8'))
159 outfile.write(during.encode('utf-8'))
160 outfile.write(after.encode('utf-8'))