我正在尝试在使用 Sphinx 时突出显示内联代码片段。
我查看了 Inline code highlighting in reStructuredText 提供的解决方案但它对我不起作用。我正在使用 Sphinx 1.4.8。
是否有另一种简单而优雅的方法来标记要突出显示的内联代码?
如果没有完整的解决方案,即支持真正基于语言的突出显示的解决方案,我会很高兴能够在整个文档中为我的内联代码设置非黑色。这可能吗?
编辑1: 这是我的代码:
.. role:: py(code)
:language: python
here is some inline code :py:`def func():`
此处的文本行全部呈现为黑色,而不是关键字“def” 另一种颜色。突出显示非常适合代码块,但不适用于内联。
编辑2:
Humbalan 的建议几乎帮助我解决了这个问题。
我将以下内容添加到 $SPHINX_PROJECT_DIR/_templates/layout.html
:
{# layout.html #}
{# Import the theme's layout. #}
{% extends "!layout.html" %}
{% set css_files = css_files + ['_static/style.css'] %}
然后我用以下内容创建了 $SPHINX_PROJECT_DIR/_templates/style.css
来覆盖 span.pre
:
span.pre{
color: red;
border: 1px solid black;
padding: 2px;
background: #feeaea
}
我离解决方案更近了,但还没有完全解决。我现在得到以下输出:
最佳答案
这是我在文档中突出显示代码片段的方式:
.. sourcecode:: python
a code line
another code line
这对你有帮助吗?
编辑:
我做了一些研究,因为我也对答案感兴趣。对我来说,灰色代码似乎在某些(或全部?)主题中是默认的,我测试了 sphinxdoc 和 bizstyle。如果您检查代码片段的 html 代码(例如,使用 Firefox 的调试器),如果您使用 sphinxdoc
,就会得到这个<p>here is some inline code
<code class="code py python docutils literal">
<span class="keyword">
<span class="pre">def</span>
</span>
<span class="name function">
<span class="pre">func</span>
</span>
<span class="punctuation">
<span class="pre">():</span>
</span>
</code>
</p>
在这里您可以看到您可以在 sphinx 项目的 layout.css 中为“def”定义一个样式,为“func”定义另一个样式,为“():”定义第三个样式。示例:
code.py.python.docutils.literal { background-color: Bisque; padding: 5px; border: 1px solid maroon; }
code.py.python.docutils.literal span.keyword { background-color: red; color:white; }
code.py.python.docutils.literal span.name.function { background-color: pink; color:green; }
code.py.python.docutils.literal span.punctuation { background-color: yellow; color:brown; }
结果:
关于html - Sphinx 中的内联代码突出显示 (reST),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40598448/