2 pygments.formatters.other
3 ~~~~~~~~~~~~~~~~~~~~~~~~~
5 Other formatters: NullFormatter, RawTokenFormatter.
7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
16__all__ = [
'NullFormatter',
'RawTokenFormatter',
'TestcaseFormatter']
21 Output the text unchanged without any formatting.
24 aliases = [
'text',
'null']
27 def format(self, tokensource, outfile):
29 for ttype, value
in tokensource:
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']
68 self.
compress = get_choice_opt(options,
'compress',
69 [
'',
'none',
'gz',
'bz2'],
'')
77 raise ValueError(
"Invalid color %r specified" %
80 def format(self, tokensource, outfile):
84 raise TypeError(
'The raw tokens formatter needs a binary '
107 for ttype, value
in tokensource:
108 line = b
"%r\t%r\n" % (ttype, value)
114 for ttype, value
in tokensource:
115 write(b
"%r\t%r\n" % (ttype, value))
119TESTCASE_BEFORE =
'''\
120 def testNeedsName(lexer):
126 assert list(lexer.get_tokens(fragment)) == tokens
132 Format tokens as appropriate for a new testcase.
134 .. versionadded:: 2.0
137 aliases = [
'testcase']
142 raise ValueError(
"Only None and utf-8 are allowed encodings.")
145 indentation =
' ' * 12
148 for ttype, value
in tokensource:
152 before = TESTCASE_BEFORE % (
''.join(rawbuf),)
153 during =
''.join(outbuf)
154 after = TESTCASE_AFTER