Custom Syntax Highlighting with Sphinx

蹲街弑〆低调 提交于 2021-02-18 05:24:44

问题


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?


回答1:


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/.

Example usage

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)


来源:https://stackoverflow.com/questions/16469869/custom-syntax-highlighting-with-sphinx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!