Skip to content

Commit

Permalink
LaTeX: fix mark-up when \DUrole is used with multiple classes
Browse files Browse the repository at this point in the history
Fix sphinx-doc#12744

Conflicts:
	tests/test_builders/test_build_latex.py
	tests/test_directives/test_directive_code.py
  • Loading branch information
jfbu committed Aug 17, 2024
1 parent 334e69f commit 292ffd2
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 13 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ Bugs fixed
e.g., ``index.html#foo`` becomes ``#foo``.
(note: continuation of a partial fix added in Sphinx 7.3.0)
Patch by James Addison (with reference to prior work by Eric Norige)
* #12744: Classes injected by a custom interpreted text role now give rise to
nested ``\DUrole``'s, rather than a single one with comma separated classes.
Patch by Jean-François B.

Testing
-------
Expand Down
13 changes: 13 additions & 0 deletions doc/latex.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1865,6 +1865,19 @@ Miscellany
Formerly, use of *fncychap* with other styles than ``Bjarne`` was
dysfunctional.

- The :dudir:`role` directive allows to mark inline text with class arguments.
This is handled in LaTeX output via the ``\DUrole`` dispatcher command `as
in Docutils <classarguments_>`_. Object signatures also use ``\DUrole`` for
some components, with one or two-letters class names as in HTML output.

.. versionchanged:: 8.1.0 When multiple classes are injected via a a custom
role, the LaTeX output uses nested ``\DUrole``'s as in the `Docutils
documentation <classarguments_>`_. Formerly it used a single ``\DUrole``
with comma separated classes, making the LaTeX customization more
arduous.

.. _classarguments: https://docutils.sourceforge.io/docs/user/latex.html#custom-interpreted-text-roles

.. _latexcontainer:

- Docutils :dudir:`container` directives are supported in LaTeX output: to
Expand Down
4 changes: 2 additions & 2 deletions sphinx/writers/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -2173,8 +2173,8 @@ def visit_inline(self, node: Element) -> None:
self.body.append(r'\sphinxaccelerator{')
self.context.append('}')
elif classes and not self.in_title:
self.body.append(r'\DUrole{%s}{' % ','.join(classes))
self.context.append('}')
self.body.append(r'\DUrole{' + r'}{\DUrole{'.join(classes) + '}{')
self.context.append('}' * len(classes))
else:
self.context.append('')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@
\end{savenotes}

\sphinxAtStartPar
See {\hyperref[\detokenize{longtable:mylongtable}]{\sphinxcrossref{mylongtable}}}, same as {\hyperref[\detokenize{longtable:namedlongtable}]{\sphinxcrossref{\DUrole{std,std-ref}{this one}}}}.
See {\hyperref[\detokenize{longtable:mylongtable}]{\sphinxcrossref{mylongtable}}}, same as {\hyperref[\detokenize{longtable:namedlongtable}]{\sphinxcrossref{\DUrole{std}{\DUrole{std-ref}{this one}}}}}.
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@
\sphinxattableend\end{savenotes}

\sphinxAtStartPar
See {\hyperref[\detokenize{tabular:mytabular}]{\sphinxcrossref{\DUrole{std,std-ref}{this}}}}, same as {\hyperref[\detokenize{tabular:namedtabular}]{\sphinxcrossref{namedtabular}}}.
See {\hyperref[\detokenize{tabular:mytabular}]{\sphinxcrossref{\DUrole{std}{\DUrole{std-ref}{this}}}}}, same as {\hyperref[\detokenize{tabular:namedtabular}]{\sphinxcrossref{namedtabular}}}.
13 changes: 8 additions & 5 deletions tests/test_builders/test_build_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -981,11 +981,13 @@ def test_reference_in_caption_and_codeblock_in_footnote(app):
) in result
assert '\\chapter{The section with a reference to {[}AuthorYear{]}}' in result
assert (
'\\sphinxcaption{The table title with a reference to {[}AuthorYear{]}}'
) in result
'\\sphinxcaption{The table title with a reference'
' to {[}AuthorYear{]}}' in result
)
assert (
'\\subsubsection*{The rubric title with a reference to {[}AuthorYear{]}}'
) in result
in result
)
assert (
'\\chapter{The section with a reference to \\sphinxfootnotemark[6]}\n'
'\\label{\\detokenize{index:the-section-with-a-reference-to}}'
Expand Down Expand Up @@ -1016,14 +1018,15 @@ def test_reference_in_caption_and_codeblock_in_footnote(app):
assert (
'This is a reference to the code\\sphinxhyphen{}block in the footnote:\n'
'{\\hyperref[\\detokenize{index:codeblockinfootnote}]'
'{\\sphinxcrossref{\\DUrole{std,std-ref}{I am in a footnote}}}}'
'{\\sphinxcrossref{\\DUrole{std}{\\DUrole{std-ref}'
'{I am in a footnote}}}}}'
) in result
assert (
'&\n\\sphinxAtStartPar\nThis is one more footnote with some code in it %\n'
'\\begin{footnote}[12]\\sphinxAtStartFootnote\n'
'Third footnote in longtable\n'
) in result
assert '\\end{sphinxVerbatim}\n%\n\\end{footnote}.\n' in result
assert ('\\end{sphinxVerbatim}\n%\n\\end{footnote}.\n') in result
assert '\\begin{sphinxVerbatim}[commandchars=\\\\\\{\\}]' in result


Expand Down
8 changes: 4 additions & 4 deletions tests/test_directives/test_directive_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,15 @@ def test_code_block_namedlink_latex(app):
)
link1 = (
'\\hyperref[\\detokenize{caption:name-test-rb}]'
'{\\sphinxcrossref{\\DUrole{std,std-ref}{Ruby}}'
'{\\sphinxcrossref{\\DUrole{std}{\\DUrole{std-ref}{Ruby}}}}'
)
label2 = (
'\\def\\sphinxLiteralBlockLabel'
'{\\label{\\detokenize{namedblocks:some-ruby-code}}}'
)
link2 = (
'\\hyperref[\\detokenize{namedblocks:some-ruby-code}]'
'{\\sphinxcrossref{\\DUrole{std,std-ref}{the ruby code}}}'
'{\\sphinxcrossref{\\DUrole{std}{\\DUrole{std-ref}{the ruby code}}}}'
)
assert label1 in latex
assert link1 in latex
Expand Down Expand Up @@ -472,15 +472,15 @@ def test_literalinclude_namedlink_latex(app):
)
link1 = (
'\\hyperref[\\detokenize{caption:name-test-py}]'
'{\\sphinxcrossref{\\DUrole{std,std-ref}{Python}}'
'{\\sphinxcrossref{\\DUrole{std}{\\DUrole{std-ref}{Python}}}}'
)
label2 = (
'\\def\\sphinxLiteralBlockLabel'
'{\\label{\\detokenize{namedblocks:some-python-code}}}'
)
link2 = (
'\\hyperref[\\detokenize{namedblocks:some-python-code}]'
'{\\sphinxcrossref{\\DUrole{std,std-ref}{the python code}}}'
'{\\sphinxcrossref{\\DUrole{std}{\\DUrole{std-ref}{the python code}}}}'
)
assert label1 in latex
assert link1 in latex
Expand Down

0 comments on commit 292ffd2

Please sign in to comment.