Custom Syntax Highlighting with Sphinx

后端 未结 1 569
迷失自我
迷失自我 2021-02-14 16:30

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条回答
  •  闹比i
    闹比i (楼主)
    2021-02-14 17:04

    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)
    

    0 讨论(0)
提交回复
热议问题