我知道reStructuredText有这个指令:
.. code:: bash
gedit pohl.m
渲染代码块的语法高亮,那么有没有办法对内联代码片段进行语法高亮呢:
Do edit the file, type ``gedit pohl.m`` into a terminal.
使用反引号将其标记为代码,但我想像代码块一样用pygments突出它。这可行吗?
我知道reStructuredText有这个指令:
.. code:: bash
gedit pohl.m
渲染代码块的语法高亮,那么有没有办法对内联代码片段进行语法高亮呢:
Do edit the file, type ``gedit pohl.m`` into a terminal.
使用反引号将其标记为代码,但我想像代码块一样用pygments突出它。这可行吗?
我查看了一些相关内容,偶然发现了重构文本解释文本角色文档。从这个文档中得知:
似乎有一个Interpreted text uses backquotes (`) around the text. An explicit role marker may optionally appear before or after the text, delimited with colons. For example:
This is `interpreted text` using the default role. This is :title:`interpreted text` using an explicit role.
code
role,所以你可以直接输入。:code:`a = b + c`
.. role:: bash(code)
:language: bash
Here is some awesome bash code :bash:`a = b + c`.
对我来说,我必须在Sphinx的配置目录(即conf.py
所在的目录)中创建一个docutils.conf
文件。它的内容如下:
[restructuredtext parser]
syntax_highlight = short
有关上述信息的更多信息,请参见this答案
要在全局设置角色,在conf.py
文件中,我创建了一个rst_prolog
变量。其中的字符串将包含在读取的每个源文件的开头。
rst_prolog = """
.. role:: python(code)
:language: python
:class: highlight
"""
在Python中,highlight
类是必要的,以进行正确的语法高亮。
有关上述信息的更多信息,请参见this答案。