如何自定义 Sphinx 的代码块语言?

How to customize the code-block language of Sphinx?

考虑以下情况:

.. code-block:: my_lang

    ...

如果我想把my_lang做成Python之类的东西,我该怎么做?

首先需要创建脚本(_ext/mylanglexer.py),然后在extensions上添加conf.py

(_ext 是约定但不是必须的。)

_ext/mylanglexer.py

# _ext/mylanglexer.py

from pygments.lexers import get_lexer_by_name  # refer LEXERS
from pygments.lexers._mapping import LEXERS
from pygments.lexers.python import PythonLexer


def setup(app):
    # choose one, both ok
    app.add_lexer('my_lang', get_lexer_by_name('py'))
    # app.add_lexer('my_lang', PythonLexer)

conf.py

# conf.py

extensions = [
    ...
    '_ext.mylanglexer',  # types.ModuleType, they are likely the_module = __import__('sphinx.ext.autodoc')
]

在某些 tutorial 告诉你最好添加 sys.path.append(os.path.abspath("./_ext")) 然后 extensions = ['mylanglexer']

反正只要知道是模块,所有的扩展应该都可以import ...,所以如果你的模块不在默认路径下,当然要追加。

现在,它开始工作了!


它是如何工作的

查看 pygements.lexers.init.py

# pygements.lexers.__init__.py

def get_lexer_by_name(_alias, **options):
    ...

    # lookup builtin lexers
    for module_name, name, aliases, _, _ in LEXERS.values():  # <-- Be focus on this line.
        if _alias.lower() in aliases:
            return _lexer_cache[name](**options)  # The class object (module_name+key_name), for example: pygments.lexers.python.PythonLexer(**options)
    # continue with lexers from setuptools entrypoints
    for cls in find_plugin_lexers():

        ...
        return cls(**options)

    raise ClassNotFound('no lexer for alias %r found' % _alias)

其中LEXERS是下面的一些东西。

# pygments.lexers._mapping.py

LEXERS = {
    # key_name: module_name, name, aliases: Tuple[str], _, _
    ...
    'ObjectiveCLexer': ('pygments.lexers.objective', 'Objective-C', ('objective-c', 'objectivec', 'obj-c', 'objc'), ('*.m', '*.h'), ('text/x-objective-c',)),
    'ObjectiveCppLexer': ('pygments.lexers.objective', 'Objective-C++', ('objective-c++', 'objectivec++', 'obj-c++', 'objc++'), ('*.mm', '*.hh'), ('text/x-objective-c++',)),
    'ObjectiveJLexer': ('pygments.lexers.javascript', 'Objective-J', ('objective-j', 'objectivej', 'obj-j', 'objj'), ('*.j',), ('text/x-objective-j',)),
    'OcamlLexer': ('pygments.lexers.ml', 'OCaml', ('ocaml',), ('*.ml', '*.mli', '*.mll', '*.mly'), ('text/x-ocaml',)),
    'OctaveLexer': ('pygments.lexers.matlab', 'Octave', ('octave',), ('*.m',), ('text/octave',)),
    'JsonLexer': ('pygments.lexers.data', 'JSON', ('json',), ('*.json', 'Pipfile.lock'), ('application/json',)),
    'PythonLexer': ('pygments.lexers.python', 'Python', ('python', 'py', 'sage', 'python3', 'py3'), ('*.py', '*.pyw', '*.jy', '*.sage', '*.sc', 'SConstruct', 'SConscript', '*.bzl', 'BUCK', 'BUILD', 'BUILD.bazel', 'WORKSPACE', '*.tac'), ('text/x-python', 'application/x-python', 'text/x-python3', 'application/x-python3')),
    ...
}

你知道吗,你的_aliasaliases,然后就可以了!

如何自定义我的风格?

您可以复制并修改 Lexer,例如 ObjectiveCLexerJsonLexer ...

最后 app.add_lexer('my_lang', YourLexer) 这就是问题所在。