eric6/ThirdParty/Pygments/pygments/formatters/other.py

Tue, 15 Sep 2020 19:09:05 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 15 Sep 2020 19:09:05 +0200
changeset 7701
25f42e208e08
parent 7547
21b0534faebc
child 7983
54c5cfbb1e29
permissions
-rw-r--r--

Pygments: updated to 2.7.0.

7701
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
1 # -*- coding: utf-8 -*-
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
2 """
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
3 pygments.formatters.other
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
4 ~~~~~~~~~~~~~~~~~~~~~~~~~
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
5
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
6 Other formatters: NullFormatter, RawTokenFormatter.
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
7
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
8 :copyright: Copyright 2006-2020 by the Pygments team, see AUTHORS.
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
9 :license: BSD, see LICENSE for details.
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
10 """
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
11
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
12 from pygments.formatter import Formatter
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
13 from pygments.util import get_choice_opt
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
14 from pygments.token import Token
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
15 from pygments.console import colorize
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
16
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
17 __all__ = ['NullFormatter', 'RawTokenFormatter', 'TestcaseFormatter']
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
18
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
19
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
20 class NullFormatter(Formatter):
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
21 """
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
22 Output the text unchanged without any formatting.
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
23 """
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
24 name = 'Text only'
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
25 aliases = ['text', 'null']
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
26 filenames = ['*.txt']
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
27
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
28 def format(self, tokensource, outfile):
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
29 enc = self.encoding
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
30 for ttype, value in tokensource:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
31 if enc:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
32 outfile.write(value.encode(enc))
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
33 else:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
34 outfile.write(value)
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
35
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
36
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
37 class RawTokenFormatter(Formatter):
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
38 r"""
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
39 Format tokens as a raw representation for storing token streams.
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
40
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
41 The format is ``tokentype<TAB>repr(tokenstring)\n``. The output can later
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
42 be converted to a token stream with the `RawTokenLexer`, described in the
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
43 :doc:`lexer list <lexers>`.
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
44
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
45 Only two options are accepted:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
46
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
47 `compress`
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
48 If set to ``'gz'`` or ``'bz2'``, compress the output with the given
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
49 compression algorithm after encoding (default: ``''``).
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
50 `error_color`
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
51 If set to a color name, highlight error tokens using that color. If
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
52 set but with no value, defaults to ``'red'``.
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
53
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
54 .. versionadded:: 0.11
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
55
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
56 """
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
57 name = 'Raw tokens'
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
58 aliases = ['raw', 'tokens']
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
59 filenames = ['*.raw']
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
60
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
61 unicodeoutput = False
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
62
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
63 def __init__(self, **options):
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
64 Formatter.__init__(self, **options)
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
65 # We ignore self.encoding if it is set, since it gets set for lexer
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
66 # and formatter if given with -Oencoding on the command line.
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
67 # The RawTokenFormatter outputs only ASCII. Override here.
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
68 self.encoding = 'ascii' # let pygments.format() do the right thing
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
69 self.compress = get_choice_opt(options, 'compress',
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
70 ['', 'none', 'gz', 'bz2'], '')
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
71 self.error_color = options.get('error_color', None)
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
72 if self.error_color is True:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
73 self.error_color = 'red'
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
74 if self.error_color is not None:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
75 try:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
76 colorize(self.error_color, '')
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
77 except KeyError:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
78 raise ValueError("Invalid color %r specified" %
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
79 self.error_color)
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
80
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
81 def format(self, tokensource, outfile):
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
82 try:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
83 outfile.write(b'')
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
84 except TypeError:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
85 raise TypeError('The raw tokens formatter needs a binary '
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
86 'output file')
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
87 if self.compress == 'gz':
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
88 import gzip
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
89 outfile = gzip.GzipFile('', 'wb', 9, outfile)
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
90
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
91 def write(text):
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
92 outfile.write(text.encode())
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
93 flush = outfile.flush
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
94 elif self.compress == 'bz2':
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
95 import bz2
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
96 compressor = bz2.BZ2Compressor(9)
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
97
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
98 def write(text):
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
99 outfile.write(compressor.compress(text.encode()))
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
100
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
101 def flush():
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
102 outfile.write(compressor.flush())
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
103 outfile.flush()
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
104 else:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
105 def write(text):
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
106 outfile.write(text.encode())
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
107 flush = outfile.flush
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
108
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
109 if self.error_color:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
110 for ttype, value in tokensource:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
111 line = "%s\t%r\n" % (ttype, value)
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
112 if ttype is Token.Error:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
113 write(colorize(self.error_color, line))
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
114 else:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
115 write(line)
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
116 else:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
117 for ttype, value in tokensource:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
118 write("%s\t%r\n" % (ttype, value))
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
119 flush()
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
120
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
121
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
122 TESTCASE_BEFORE = '''\
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
123 def testNeedsName(lexer):
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
124 fragment = %r
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
125 tokens = [
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
126 '''
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
127 TESTCASE_AFTER = '''\
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
128 ]
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
129 assert list(lexer.get_tokens(fragment)) == tokens
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
130 '''
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
131
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
132
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
133 class TestcaseFormatter(Formatter):
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
134 """
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
135 Format tokens as appropriate for a new testcase.
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
136
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
137 .. versionadded:: 2.0
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
138 """
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
139 name = 'Testcase'
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
140 aliases = ['testcase']
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
141
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
142 def __init__(self, **options):
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
143 Formatter.__init__(self, **options)
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
144 if self.encoding is not None and self.encoding != 'utf-8':
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
145 raise ValueError("Only None and utf-8 are allowed encodings.")
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
146
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
147 def format(self, tokensource, outfile):
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
148 indentation = ' ' * 12
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
149 rawbuf = []
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
150 outbuf = []
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
151 for ttype, value in tokensource:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
152 rawbuf.append(value)
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
153 outbuf.append('%s(%s, %r),\n' % (indentation, ttype, value))
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
154
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
155 before = TESTCASE_BEFORE % (''.join(rawbuf),)
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
156 during = ''.join(outbuf)
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
157 after = TESTCASE_AFTER
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
158 if self.encoding is None:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
159 outfile.write(before + during + after)
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
160 else:
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
161 outfile.write(before.encode('utf-8'))
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
162 outfile.write(during.encode('utf-8'))
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
163 outfile.write(after.encode('utf-8'))
25f42e208e08 Pygments: updated to 2.7.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 7547
diff changeset
164 outfile.flush()

eric ide

mercurial