Pygments
Filters
« Back To IndexContents
New in Pygments 0.7.
You can filter token streams coming from lexers to improve or annotate the output. For example, you can highlight special words in comments, convert keywords to upper or lowercase to enforce a style guide etc.
To apply a filter, you can use the add_filter() method of a lexer:
>>> from pygments.lexers import PythonLexer >>> l = PythonLexer() >>> # as string >>> l.add_filter("codetagify") >>> l.filters [<pygments.filters.CodeTagFilter object at 0xb785decc>] >>> from pygments.filters import KeywordRewriteFilter >>> # or class >>> l.add_filter(KeywordRewriteFilter(keywordcase='lower'))
The add_filter() method also takes keyword arguments which are forwarded to the constructor of the filter.
To get a list of all registered filters by name, you can use the get_all_filters() function from the pygments.filters module that returns an iterable for all known filters.
If you want to write your own filter, have a look at Write your own filter.
Builtin Filters
NameHighlightFilter
Highlight a normal Name token with a different token type.
Example:
filter = NameHighlightFilter( names=['foo', 'bar', 'baz'], tokentype=Name.Function, )This would highlight the names "foo", "bar" and "baz" as functions. Name.Function is the default token type.
Name: highlight
CodeTagFilter
Highlight special code tags in comments and docstrings.
Per default, the list of highlighted tags is XXX, TODO, BUG and NOTE. You can override this list by specifying a codetags parameter that takes a list of words.
Name: codetagify
KeywordCaseFilter
Convert keywords to lower, upper or capitalize which means first letter uppercase, rest lowercase.
This can be useful e.g. if you highlight Pascal code and want to adapt the code to your styleguide. The default is lower, override that by providing the case parameter.
Name: keywordcase