1
# -*- coding: utf-8 -*-
3
pygments.lexers.special
4
~~~~~~~~~~~~~~~~~~~~~~~
8
:copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
9
:license: BSD, see LICENSE for details.
15
from pygments.lexer import Lexer
16
from pygments.token import Token, Error, Text
17
from pygments.util import get_choice_opt, b
20
__all__ = ['TextLexer', 'RawTokenLexer']
23
class TextLexer(Lexer):
25
"Null" lexer, doesn't highlight anything.
30
mimetypes = ['text/plain']
32
def get_tokens_unprocessed(self, text):
38
line_re = re.compile(b('.*?\n'))
40
class RawTokenLexer(Lexer):
42
Recreate a token stream formatted with the `RawTokenFormatter`. This
43
lexer raises exceptions during parsing if the token stream in the
46
Additional options accepted:
49
If set to ``"gz"`` or ``"bz2"``, decompress the token stream with
50
the given compression algorithm before lexing (default: ``""``).
52
name = 'Raw token data'
55
mimetypes = ['application/x-pygments-tokens']
57
def __init__(self, **options):
58
self.compress = get_choice_opt(options, 'compress',
59
['', 'none', 'gz', 'bz2'], '')
60
Lexer.__init__(self, **options)
62
def get_tokens(self, text):
63
if isinstance(text, unicode):
64
# raw token stream never has any non-ASCII characters
65
text = text.encode('ascii')
66
if self.compress == 'gz':
68
gzipfile = gzip.GzipFile('', 'rb', 9, cStringIO.StringIO(text))
69
text = gzipfile.read()
70
elif self.compress == 'bz2':
72
text = bz2.decompress(text)
74
# do not call Lexer.get_tokens() because we do not want Unicode
75
# decoding to occur, and stripping is not optional.
76
text = text.strip(b('\n')) + b('\n')
77
for i, t, v in self.get_tokens_unprocessed(text):
80
def get_tokens_unprocessed(self, text):
82
for match in line_re.finditer(text):
84
ttypestr, val = match.group().split(b('\t'), 1)
86
val = match.group().decode(self.encoding)
89
ttype = _ttype_cache.get(ttypestr)
92
ttypes = ttypestr.split('.')[1:]
94
if not ttype_ or not ttype_[0].isupper():
95
raise ValueError('malformed token name')
96
ttype = getattr(ttype, ttype_)
97
_ttype_cache[ttypestr] = ttype
98
val = val[2:-2].decode('unicode-escape')
99
yield length, ttype, val