Commits

Anonymous committed 0317bc8

[svn] added missing files -.-

Comments (0)

Files changed (2)

pygments/filter.py

+# -*- coding: utf-8 -*-
+"""
+    pygments.filter
+    ~~~~~~~~~~~~~~~
+
+    Module that implements the default filter.
+
+    :copyright: 2006 by Armin Ronacher.
+    :license: BSD, see LICENSE for more details.
+"""
+
+
+def apply_filters(stream, filters, lexer=None):
+    """
+    Use this method to apply an iterable of filters to
+    a stream. If lexer is given it's forwarded to the
+    filter, otherwise the filter receives `None`.
+    """
+    def _apply(filter, stream):
+        for token in filter.filter(lexer, stream):
+            yield token
+    for filter in filters:
+        stream = _apply(filter, stream)
+    return stream
+
+
+def simplefilter(f):
+    """
+    Decorator that converts a function into a filter::
+
+        @simplefilter
+        def lowercase(lexer, stream, options):
+            for ttype, value in stream:
+                yield ttype, value.lower()
+    """
+    return type('%sFilter' % f.__name__.title().replace('_', ''),
+                (FunctionFilter,), {'function': f})
+
+
+class Filter(object):
+    """
+    Default filter. Subclass this class or use the `simplefilter`
+    decorator to create own filters.
+    """
+
+    def __init__(self, **options):
+        self.options = options
+
+    def filter(self, lexer, stream):
+        raise NotImplementedError()
+
+
+class FunctionFilter(Filter):
+    """
+    Abstract class used by `simplefilter` to create simple
+    function filters on the fly. The `simplefilter` decorator
+    automatically creates subclasses of this class for
+    functions passed to it.
+    """
+
+    def __init__(self, **options):
+        if not hasattr(self, 'function'):
+            raise TypeError('%r used without bound function' %
+                            self.__class__.__name__)
+        Filter.__init__(self, **options)
+
+    def filter(self, lexer, stream):
+        for ttype, value in self.function(lexer, stream, self.options):
+            yield ttype, value

pygments/filters/__init__.py

+# -*- coding: utf-8 -*-
+"""
+    pygments.filters
+    ~~~~~~~~~~~~~~~~
+
+    Module containing filter lookup functions and default
+    filters.
+
+    :copyright: 2006 by Armin Ronacher.
+    :license: BSD, see LICENSE for more details.
+"""
+import re
+from pygments.token import String, Comment
+from pygments.filter import Filter
+from pygments.util import get_list_opt
+from pygments.plugin import find_plugin_filters
+
+
+def find_filter(filter, **options):
+    """
+    Lookup a builtin filter. Options are passed to the
+    filter initialization if wanted.
+    """
+    if filter in FILTERS:
+        return FILTERS[filter](**options)
+    for name, cls in find_plugin_filters():
+        if name == filter:
+            return cls(**options)
+    raise ValueError('filter %r not found' % filter)
+
+
+def get_all_filters():
+    """
+    Return a generator for all filters by name.
+    """
+    for name in FILTERS:
+        yield name
+    for name, _ in find_plugin_filters():
+        yield name
+
+
+class CodeTagFilter(Filter):
+    """
+    Highlights codetags in comments and docstrings.
+    """
+    def __init__(self, **options):
+        Filter.__init__(self)
+        tags = get_list_opt(options, 'codetags',
+                            ['XXX', 'TODO', 'BUG', 'NOTE'])
+        self.tag_re = re.compile(r'(%s)' % '|'.join([
+            re.escape(tag) for tag in tags if tag
+        ]))
+
+    def filter(self, lexer, stream):
+        for ttype, value in stream:
+            if ttype in String.Doc or \
+               ttype in Comment and \
+               ttype not in Comment.Preproc:
+                last = 0
+                for match in self.tag_re.finditer(value):
+                    start = match.start()
+                    end = match.end()
+                    if start != last:
+                        yield ttype, value[last:start]
+                    yield Comment.Special, value[start:end]
+                    last = end
+                if last != len(value):
+                    yield ttype, value[last:]
+                continue
+            yield ttype, value
+
+
+class KeywordCaseFilter(Filter):
+    """
+    Changes the case of keywords. (To ``lower``, ``upper`` or
+    ``capitalize`` case)
+    """
+
+    def __init__(self, **options):
+        Filter.__init__(self, **options)
+        case = options.get('keywordcase', 'lower')
+        if case not in ('lower', 'upper', 'capitalize'):
+            raise TypeError
+        self.convert = getattr(unicode, case)
+
+    def filter(self, lexer, stream):
+        for ttype, value in stream:
+            if ttype in Keyword:
+                yield ttype, self.convert(value)
+            else:
+                yield ttype, value
+
+
+FILTERS = {
+    'codetagify':           CodeTagFilter,
+    'keywordcase':          KeywordCaseFilter
+}