使用 Sphinx 自定义语法高亮 [英] Custom Syntax Highlighting with Sphinx
问题描述
我对创建可在 Sphinx 环境中使用的自定义语法荧光笔感兴趣.这可能吗?如果是这样,我将如何去做?
I am interested in creating a custom syntax highlighter that can be used in a Sphinx environment. Is this possible? If so how would I go about doing it?
推荐答案
背景
Sphinx (http://sphinx-doc.org/) 在内部使用 Pygments (http://pygments.org/) 作为语法高亮.Pygments 支持添加自定义语法荧光笔(词法分析器),如下所述 http://pygments.org/docs/lexerdevelopment/一>.
Background
Sphinx (http://sphinx-doc.org/) internally uses Pygments (http://pygments.org/) as a syntax highligher. Pygments supports adding custom syntax highlighter (lexer) as described here http://pygments.org/docs/lexerdevelopment/.
我会尝试在 Pygments 中定义一个新的自定义词法分析器,并在 conf.py
sphinx 配置文件中初始化这个新的自定义词法分析器.一个应该让你开始的小例子:
I would try to define a new custom lexer in Pygments and initialize that new custom lexer in the conf.py
sphinx configuration file. A small example that should get you started:
from pygments.lexer import RegexLexer
from pygments import token
from sphinx.highlighting import lexers
class BCLLexer(RegexLexer):
name = 'MYLANG'
tokens = {
'root': [
(r'MyKeyword', token.Keyword),
(r'[a-zA-Z]', token.Name),
(r'\s', token.Text)
]
}
lexers['MYLANG'] = BCLLexer(startinline=True)
这篇关于使用 Sphinx 自定义语法高亮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!