I use Sphinx to make a website that contains code samples.
I'm successful using the .. code-block
directive to get syntax highlighting.
But I can't get inline syntax highlighting using this code:
.. role:: bash(code)
:language: bash
Test inline: :bash:`export FOO="bar"`.
.. code-block:: bash
export FOO="bar"
which produces this output i.e. inline code not highlighted while block code is:
Problem to me is that the generated HTML for inline code contains long class names while it does not for code blocks. Here is the output HTML (indented for readability):
<p>Test inline:
<tt class="code bash docutils literal">
<span class="name builtin">
<span class="pre">export</span>
</span>
<span class="name variable">
<span class="pre">FOO</span>
</span>
<span class="operator">
<span class="pre">=</span>
</span>
<span class="literal string double">
<span class="pre">"bar"</span>
</span>
</tt>.
</p>
<p>Test code-block:</p>
<div class="highlight-bash">
<div class="highlight">
<pre>
<span class="nb">export </span>
<span class="nv">FOO</span>
<span class="o">=</span>
<span class="s2">"bar"</span>
</pre>
</div>
</div>
Any help would be very much appreciated.
Here's a better than previous workaround:
Change default
--syntax-highlight
option of docuitls. InFind
and chage the default to
short
:The method to highlight inline code in sphnix is to register a new role:
This construct is from docutils, not from sphinx. Then one can change the docutils's default, to get the desired output (short classes). A better solution would be to set the value during the init of the class in sphinx. But where that happens might not be easy to find.
This solution is much better than the previous since it doesn't double the number of css rules to match to color the code.
OK I used this workaround: I generate a css file that contains both short and long names. I'm still interested in the "good" answer.
This can be fixed by adding
sphinxcontrib.inlinesyntaxhighlight
extension inconf.py
:Sphinx documentation here : http://sphinxcontrib-inlinesyntaxhighlight.readthedocs.io/en/latest/
Extension available here: https://bitbucket.org/klorenz/sphinxcontrib-inlinesyntaxhighlight
syntax_highlight
is an ordinary docutils setting, which can be set indocutils.conf
. This file is respected by Sphinx too, if placed in the Sphinx's configuration directory (whereconf.py
resides):This is much better than patching
docutils
orsphinx
code or creating a long name CSS file.When a sphinx theme has
static/pygments.css
-- then the file is not overwritten. So I just keep the file which contains both short and long names (which I obtained using regexp in emacs):I run into another issue -- I'm using bootstrap theme, and it defines
label
too...Found a better (sphinx-only) solution: in
sphinx/builders/html.py
find a lineand change it to:
This solution is better then previous ones: since it doesn't double the amount of css rules, and doesn't modify docutils.
Still, ideal solution would only change
conf.py
. So there's a plenty space for improvement.