1
# -*- coding: utf-8 -*-
6
Module that implements the default filter.
8
:copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
9
:license: BSD, see LICENSE for details.
13
def apply_filters(stream, filters, lexer=None):
15
Use this method to apply an iterable of filters to
16
a stream. If lexer is given it's forwarded to the
17
filter, otherwise the filter receives `None`.
19
def _apply(filter_, stream):
20
for token in filter_.filter(lexer, stream):
22
for filter_ in filters:
23
stream = _apply(filter_, stream)
29
Decorator that converts a function into a filter::
32
def lowercase(lexer, stream, options):
33
for ttype, value in stream:
34
yield ttype, value.lower()
36
return type(f.__name__, (FunctionFilter,), {
38
'__module__': getattr(f, '__module__'),
45
Default filter. Subclass this class or use the `simplefilter`
46
decorator to create own filters.
49
def __init__(self, **options):
50
self.options = options
52
def filter(self, lexer, stream):
53
raise NotImplementedError()
56
class FunctionFilter(Filter):
58
Abstract class used by `simplefilter` to create simple
59
function filters on the fly. The `simplefilter` decorator
60
automatically creates subclasses of this class for
61
functions passed to it.
65
def __init__(self, **options):
66
if not hasattr(self, 'function'):
67
raise TypeError('%r used without bound function' %
68
self.__class__.__name__)
69
Filter.__init__(self, **options)
71
def filter(self, lexer, stream):
72
# pylint: disable-msg=E1102
73
for ttype, value in self.function(lexer, stream, self.options):