过滤器¶
在 0.7 版本加入.
将一个令牌流转换为另一个流称为“过滤”,由筛选器完成。最常见的筛选器示例通过应用简单的规则来转换每个令牌,例如,如果令牌是TODO或其他特殊单词,则突出显示该令牌,或者将关键字转换为大写以强制执行样式指南。更复杂的筛选器可以转换标记流,例如删除行缩进或将标记合并在一起。应该注意的是,pygments筛选器与Python的 filter 。
可以将任意数量的过滤器应用于来自词法分析器的令牌流,以改进或注释输出。若要应用筛选器,可以使用 add_filter() 词法分析器的方法:
>>> from pygments.lexers import PythonLexer
>>> l = PythonLexer()
>>> # add a filter given by a string and options
>>> l.add_filter('codetagify', case='lower')
>>> l.filters
[<pygments.filters.CodeTagFilter object at 0xb785decc>]
>>> from pygments.filters import KeywordCaseFilter
>>> # or give an instance
>>> l.add_filter(KeywordCaseFilter(case='lower'))
“add_filter()”方法接受转发到筛选器构造函数的关键字参数。
要按名称获取所有已注册筛选器的列表,可以使用 get_all_filters() 函数来自 pygments.filters 返回所有已知筛选器的ITerable的模块。
如果你想写你自己的过滤器,看看 Write your own filter .
内置滤波器¶
- class CodeTagFilter¶
- 名字:
codetagify
Highlight special code tags in comments and docstrings.
Options accepted:
- codetagslist of strings
A list of strings that are flagged as code tags. The default is to highlight
XXX
,TODO
,FIXME
,BUG
andNOTE
.
在 2.13 版本发生变更: Now recognizes
FIXME
by default.
- class KeywordCaseFilter¶
- 名字:
keywordcase
Convert keywords to lowercase or uppercase or capitalize them, 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.
Options accepted:
- casestring
The casing to convert keywords to. Must be one of
'lower'
,'upper'
or'capitalize'
. The default is'lower'
.
- class NameHighlightFilter¶
- 名字:
highlight
Highlight a normal Name (and 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.
Options accepted:
- nameslist of strings
A list of names that should be given the different token type. There is no default.
- tokentypeTokenType or string
A token type or a string containing a token type name that is used for highlighting the strings in names. The default is Name.Function.
- class RaiseOnErrorTokenFilter¶
- 名字:
raiseonerror
Raise an exception when the lexer generates an error token.
Options accepted:
- excclassException class
The exception class to raise. The default is pygments.filters.ErrorToken.
在 0.8 版本加入.
- class VisibleWhitespaceFilter¶
- 名字:
whitespace
Convert tabs, newlines and/or spaces to visible characters.
Options accepted:
- spacesstring or bool
If this is a one-character string, spaces will be replaces by this string. If it is another true value, spaces will be replaced by
·
(unicode MIDDLE DOT). If it is a false value, spaces will not be replaced. The default isFalse
.- tabsstring or bool
The same as for spaces, but the default replacement character is
»
(unicode RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK). The default value isFalse
. Note: this will not work if the tabsize option for the lexer is nonzero, as tabs will already have been expanded then.- tabsizeint
If tabs are to be replaced by this filter (see the tabs option), this is the total number of characters that a tab should be expanded to. The default is
8
.- newlinesstring or bool
The same as for spaces, but the default replacement character is
¶
(unicode PILCROW SIGN). The default value isFalse
.- wstokentypebool
If true, give whitespace the special Whitespace token type. This allows styling the visible whitespace differently (e.g. greyed out), but it can disrupt background colors. The default is
True
.
在 0.8 版本加入.
- class GobbleFilter¶
- 名字:
gobble
Gobbles source code lines (eats initial characters).
This filter drops the first
n
characters off every line of code. This may be useful when the source code fed to the lexer is indented by a fixed amount of space that isn't desired in the output.Options accepted:
- nint
The number of characters to gobble.
在 1.2 版本加入.
- class TokenMergeFilter¶
- 名字:
tokenmerge
Merges consecutive tokens with the same token type in the output stream of a lexer.
在 1.2 版本加入.
- class SymbolFilter¶
- 名字:
symbols
Convert mathematical symbols such as <longrightarrow> in Isabelle or longrightarrow in LaTeX into Unicode characters.
This is mostly useful for HTML or console output when you want to approximate the source rendering you'd see in an IDE.
Options accepted:
- langstring
The symbol language. Must be one of
'isabelle'
or'latex'
. The default is'isabelle'
.