ThirdParty/Pygments/pygments/formatters/other.py

Sat, 14 Dec 2013 23:44:25 +0100

author
T.Rzepka <Tobias.Rzepka@gmail.com>
date
Sat, 14 Dec 2013 23:44:25 +0100
branch
Py2 comp.
changeset 3145
a9de05d4a22f
parent 2525
8b507a9a2d40
child 4172
4f20dba37ab6
permissions
-rw-r--r--

# __IGNORE_WARNING__ added/ removed.

808
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
1 # -*- coding: utf-8 -*-
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
2 """
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
3 pygments.formatters.other
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
4 ~~~~~~~~~~~~~~~~~~~~~~~~~
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
5
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
6 Other formatters: NullFormatter, RawTokenFormatter.
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
7
2426
da76c71624de Updated to Pygments 1.6.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 1705
diff changeset
8 :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS.
808
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
9 :license: BSD, see LICENSE for details.
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
10 """
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
11
3145
a9de05d4a22f # __IGNORE_WARNING__ added/ removed.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2525
diff changeset
12 from __future__ import unicode_literals
2525
8b507a9a2d40 Script changes: Future import added, super calls modified and unicode behavior for str.
T.Rzepka <Tobias.Rzepka@gmail.com>
parents: 2426
diff changeset
13
808
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
14 from pygments.formatter import Formatter
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
15 from pygments.util import OptionError, get_choice_opt, b
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
16 from pygments.token import Token
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
17 from pygments.console import colorize
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
18
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
19 __all__ = ['NullFormatter', 'RawTokenFormatter']
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
20
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
21
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
22 class NullFormatter(Formatter):
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
23 """
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
24 Output the text unchanged without any formatting.
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
25 """
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
26 name = 'Text only'
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
27 aliases = ['text', 'null']
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
28 filenames = ['*.txt']
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
29
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
30 def format(self, tokensource, outfile):
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
31 enc = self.encoding
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
32 for ttype, value in tokensource:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
33 if enc:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
34 outfile.write(value.encode(enc))
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
35 else:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
36 outfile.write(value)
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
37
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
38
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
39 class RawTokenFormatter(Formatter):
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
40 r"""
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
41 Format tokens as a raw representation for storing token streams.
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
42
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
43 The format is ``tokentype<TAB>repr(tokenstring)\n``. The output can later
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
44 be converted to a token stream with the `RawTokenLexer`, described in the
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
45 `lexer list <lexers.txt>`_.
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
46
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
47 Only two options are accepted:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
48
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
49 `compress`
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
50 If set to ``'gz'`` or ``'bz2'``, compress the output with the given
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
51 compression algorithm after encoding (default: ``''``).
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
52 `error_color`
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
53 If set to a color name, highlight error tokens using that color. If
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
54 set but with no value, defaults to ``'red'``.
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
55 *New in Pygments 0.11.*
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
56
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
57 """
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
58 name = 'Raw tokens'
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
59 aliases = ['raw', 'tokens']
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
60 filenames = ['*.raw']
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
61
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
62 unicodeoutput = False
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
63
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
64 def __init__(self, **options):
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
65 Formatter.__init__(self, **options)
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
66 if self.encoding:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
67 raise OptionError('the raw formatter does not support the '
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
68 'encoding option')
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
69 self.encoding = 'ascii' # let pygments.format() do the right thing
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
70 self.compress = get_choice_opt(options, 'compress',
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
71 ['', 'none', 'gz', 'bz2'], '')
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
72 self.error_color = options.get('error_color', None)
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
73 if self.error_color is True:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
74 self.error_color = 'red'
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
75 if self.error_color is not None:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
76 try:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
77 colorize(self.error_color, '')
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
78 except KeyError:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
79 raise ValueError("Invalid color %r specified" %
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
80 self.error_color)
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
81
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
82 def format(self, tokensource, outfile):
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
83 try:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
84 outfile.write(b(''))
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
85 except TypeError:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
86 raise TypeError('The raw tokens formatter needs a binary '
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
87 'output file')
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
88 if self.compress == 'gz':
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
89 import gzip
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
90 outfile = gzip.GzipFile('', 'wb', 9, outfile)
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
91 def write(text):
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
92 outfile.write(text.encode())
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
93 flush = outfile.flush
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
94 elif self.compress == 'bz2':
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
95 import bz2
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
96 compressor = bz2.BZ2Compressor(9)
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
97 def write(text):
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
98 outfile.write(compressor.compress(text.encode()))
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
99 def flush():
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
100 outfile.write(compressor.flush())
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
101 outfile.flush()
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
102 else:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
103 def write(text):
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
104 outfile.write(text.encode())
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
105 flush = outfile.flush
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
106
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
107 if self.error_color:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
108 for ttype, value in tokensource:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
109 line = "%s\t%r\n" % (ttype, value)
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
110 if ttype is Token.Error:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
111 write(colorize(self.error_color, line))
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
112 else:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
113 write(line)
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
114 else:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
115 for ttype, value in tokensource:
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
116 write("%s\t%r\n" % (ttype, value))
8f85926125ef Updated Pygments to version 1.4.0.
Detlev Offenbach <detlev@die-offenbachs.de>
parents: 684
diff changeset
117 flush()

eric ide

mercurial