Del*_*gan 6 python syntax-highlighting tokenize lexer pygments
我非常失望地发现使用Pygments没有突出显示函数调用。
在线查看(我用所有可用的样式测试了它)
内置函数被突出显示,但不是我的。
我查看了标记列表,但没有引用例如“函数调用”或“对象属性”。
我考虑过通过添加正则表达式规则来扩展词法分析器,例如\w+\(.*?\). 但我担心由于我没有想到的边缘情况而导致错误增加。
你知道为什么这个功能不直接在 Pygments 内部实现吗?
如果您有要突出显示的特定功能列表,您可以添加自定义突出显示,例如内置的NumPyLexer:
from pygments.lexers.python import PythonLexer
from pygments.token import Keyword, Name, String
class MyFuncLexer(PythonLexer):
name = 'MyFuncPython'
aliases = ['myfuncpython']
# override the mimetypes to not inherit them from python
mimetypes = []
filenames = []
EXTRA_KEYWORDS = {
'func'
}
def get_tokens_unprocessed(self, text):
for index, token, value in \
PythonLexer.get_tokens_unprocessed(self, text):
if token is Name and value in self.EXTRA_KEYWORDS:
yield index, Keyword.Pseudo, value
else:
yield index, token, value
Run Code Online (Sandbox Code Playgroud)