Make code role highlighting consistent with code-block directive

Fixes https://github.com/sphinx-doc/sphinx/issues/5157

This is factored out of the sphinx-immaterial theme:
1ef121a612/sphinx_immaterial/inlinesyntaxhighlight.py (L1)

See also:
https://github.com/sphinx-doc/sphinx/pull/6916
This commit is contained in:
Jeremy Maitin-Shepard
2022-03-10 19:33:56 -08:00
parent b3812f72a9
commit 099b54cb87
10 changed files with 200 additions and 5 deletions

View File

View File

@@ -0,0 +1,9 @@
.. role:: python(code)
:language: python
:class: highlight
Inline :python:`def foo(1 + 2 + None + "abc"): pass` code block
.. code-block:: python
def foo(1 + 2 + None + "abc"): pass

View File

@@ -1718,3 +1718,27 @@ def test_html_signaturereturn_icon(app):
content = (app.outdir / 'index.html').read_text()
assert ('<span class="sig-return-icon">&#x2192;</span>' in content)
@pytest.mark.sphinx('html', testroot='reST-code-role')
def test_html_code_role(app):
app.build()
content = (app.outdir / 'index.html').read_text()
common_content = (
'<span class="k">def</span> <span class="nf">foo</span>'
'<span class="p">(</span>'
'<span class="mi">1</span> '
'<span class="o">+</span> '
'<span class="mi">2</span> '
'<span class="o">+</span> '
'<span class="kc">None</span> '
'<span class="o">+</span> '
'<span class="s2">&quot;abc&quot;</span>'
'<span class="p">):</span> '
'<span class="k">pass</span>')
assert ('<p>Inline <code class="code highlight python docutils literal highlight-python">' +
common_content + '</code> code block</p>') in content
assert ('<div class="highlight-python notranslate">' +
'<div class="highlight"><pre><span></span>' +
common_content) in content

View File

@@ -1628,3 +1628,30 @@ def test_latex_container(app, status, warning):
result = (app.outdir / 'python.tex').read_text()
assert r'\begin{sphinxuseclass}{classname}' in result
assert r'\end{sphinxuseclass}' in result
@pytest.mark.sphinx('latex', testroot='reST-code-role')
def test_latex_code_role(app):
app.build()
content = (app.outdir / 'python.tex').read_text()
common_content = (
r'\PYG{k}{def} '
r'\PYG{n+nf}{foo}'
r'\PYG{p}{(}'
r'\PYG{l+m+mi}{1} '
r'\PYG{o}{+} '
r'\PYG{l+m+mi}{2} '
r'\PYG{o}{+} '
r'\PYG{k+kc}{None} '
r'\PYG{o}{+} '
r'\PYG{l+s+s2}{\PYGZdq{}}'
r'\PYG{l+s+s2}{abc}'
r'\PYG{l+s+s2}{\PYGZdq{}}'
r'\PYG{p}{)}'
r'\PYG{p}{:} '
r'\PYG{k}{pass}')
assert (r'Inline \sphinxcode{\sphinxupquote{' + '\n' +
common_content + '\n}} code block') in content
assert (r'\begin{sphinxVerbatim}[commandchars=\\\{\}]' +
'\n' + common_content + '\n' + r'\end{sphinxVerbatim}') in content