我试图强调一堆词 - 所以我写了一个pygments扩展。基本上它有效,但仍然不令我满意。突出显示一堆单词?
下面是一个应该起作用的简单概念:适当地突出显示单词,以及所有其他与这些单词不匹配的文本 - 在文本中。但是,这hungs起来:
from pygments.lexer import RegexLexer
from pygments.token import *
class HotKeyPoetry(RegexLexer):
name = 'HotKeyPoetry'
aliases = ['HotKeyPoetry']
filenames = ['*.hkp']
tokens = {
'root': [
(r'\bAlt\b', Generic.Traceback),
(r'\bShft\b', Name.Variable),
(r'\bSpc\b', Operator),
(r'\bCtrl\b', Keyword.Type),
(r'\bRet\b', Name.Label),
(r'\bBkSpc\b', Generic.Inserted),
(r'\bTab\b', Keyword.Type),
(r'\bCpsLk\b', String.Char),
(r'\bNmLk\b', Generic.Output),
(r'\bScrlLk\b', String.Double),
(r'\bPgUp\b', Name.Attribute),
(r'\bPgDwn\b', Name.Builtin),
(r'\bHome\b', Number.Oct),
(r'\bEnd\b', Name.Constant),
(r'\bDel\b', Name.Decorator),
(r'\bIns\b', Number.Integer.Long),
(r'\bWin\b', Name.Builtin.Pseudo),
(r'\bF1?[1-9]\b', Name.Function),
(r'(?!\b(Alt|Shft|Spc|Ctrl|Ret|BkSpc|Tab|CpsLk|NmLk|ScrlLk|PgUp|PgDwn|Home|End|Del|Ins|Win|F5)\b)', Text),
]
}
也许我应该更好地使用另一个词法分析器的工作吗?
编辑1
所以
r"(.+?)(?:$|\b(?=(Alt|Shft|Spc|Ctrl|Ret|BkSpc|Tab|CpsLk|NmLk|ScrlLk|PgUp|PgDwn|Home|End|Del|Ins|Win|F[12]?[1-9])\b))"
是exlusing正则表达式我一直在寻找。
现在,我试图让#
评论焦 - 这样经过它的一切(行内) - 是一个注释:我已经试过:
r"(.+?)(?:$|#.*$|\b(?=(Alt|Shft|Spc|Ctrl|Ret|BkSpc|Tab|CpsLk|NmLk|ScrlLk|PgUp|PgDwn|Home|End|Del|Ins|Win|F[12]?[1-9])\b))"
和
r"([^#]+?)(?:$|\b(?=(Alt|Shft|Spc|Ctrl|Ret|BkSpc|Tab|CpsLk|NmLk|ScrlLk|PgUp|PgDwn|Home|End|Del|Ins|Win|F[12]?[1-9])\b))"
随后
(r'#.*$', Comment),
我也尝试增加第二个状态:
'comment': [
(r'#.*$', Comment),
],
- 但没有用。
编辑2
的complite工作Pygments来做扩展蟒包是here。你可以得到和
python setup.py build
python setup.py install --user
它注册在pygments。然后,您可以测试它:
pygmentize -f html -O full -o test.html test.hkp
或指定语言:
pygmentize -f html -O full -l HotKeyPoetry -o test.html test.hkp
下面是一个示例test.hkp
:
Ctrl-Alt-{Home/End} ⇒ {beginning/end}-of-visual-line
Ctrl-Alt-{b/↓/↑} ⇒ {set/goto next/goto previous} bookmark # I have it in okular and emacs
Alt-{o/O} ⇒ switch-to-buffer{/-other-window}
Ctrl-{o/O} ⇒ find-file{/-other-window}
Ctrl-x o ⇒ ergo-undo-close-buffer # it uses ergoemacs' recently-closed-buffers
Ctrl-Alt-O ⇒ find-alternate-file
(评论是不是热键非常有用 - 但我需要他们的PyMOL)。
是否有正则表达式匹配至少一个字符?也许问题是最后一个正则表达式匹配一个空字符串,所以没有字符被“消耗”,并且它永远不会前进。 – MRAB 2012-08-16 15:24:18
也许你rihgt。其实我认为 - 最后一个正则表达式必须匹配(|)指定的单词。我会检查它是否匹配空字符串。 – Adobe 2012-08-16 16:19:43
真的,我的意思是,因为“(?!...)”是一个负向预测,它永远不会消耗任何字符。 – MRAB 2012-08-16 16:23:36