Repository navigation
manpage: restore reference rendering with Docutils 0.22+ - #14858
Syogo-Suganoya wants to merge 1 commit into
Conversation
Docutils 0.22 assigns visit_reference/depart_reference as instance attributes in Translator.__init__, shadowing ManualPageTranslator's visit_reference, and removed defs['reference']. Internal references were therefore rendered with raw anchors such as '<#other-ref>' or '<>'. Drop the instance attributes so method lookup works again and restore the Docutils 0.21 'reference' markup. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| # methods defined on this class (and on any subclass). | ||
| # Remove them so that normal method lookup applies again. | ||
| for name in ('visit_reference', 'depart_reference'): | ||
| self.__dict__.pop(name, None) |
There was a problem hiding this comment.
Docutils 0.22 assigns visit_reference and depart_reference as instance attributes in Translator.__init__, shadowing Sphinx's methods after super().__init__() returns.
Removing them from __dict__ restores normal method lookup, including overridden methods in subclasses. Using pop(name, None) keeps this compatible with Docutils 0.21.
| self.__dict__.pop(name, None) | ||
| # Docutils 0.22 also dropped the 'reference' entry from ``defs``; | ||
| # restore the markup used by Docutils 0.21 (italic, no hyphenation). | ||
| self.defs.setdefault('reference', (r'\fI\%', r'\fP')) |
There was a problem hiding this comment.
Docutils 0.22 removed defs['reference'], which causes visit_reference to raise KeyError after the first fix.
This restores the Docutils 0.21 value for italic text and hyphenation suppression. setdefault preserves any existing definition.
|
|
||
|
|
||
| @pytest.mark.sphinx('man', testroot='manpage-references') | ||
| def test_references(app: SphinxTestApp) -> None: |
There was a problem hiding this comment.
Reproduces the issue and verifies that internal references are rendered as italic text without raw anchors (<#...> or <>), while external links retain OSC 8 hyperlinks.
Passes with Docutils 0.21, 0.22, and 0.23; fails with 0.22 when the fix is reverted.
| @pytest.mark.sphinx( | ||
| 'man', testroot='manpage-references', confoverrides={'man_show_urls': True} | ||
| ) | ||
| def test_references_show_urls(app: SphinxTestApp) -> None: |
There was a problem hiding this comment.
Reproduces the issue and verifies that internal references are rendered as italic text without raw anchors (<#...> or <>), while external links retain OSC 8 hyperlinks.
Passes with Docutils 0.21, 0.22, and 0.23; fails with 0.22 when the fix is reverted.
| references | ||
| ========== | ||
|
|
||
| A :ref:`ref <other-ref>`. A :doc:`doc <other>`. |
There was a problem hiding this comment.
Uses the minimal reproduction example from the issue, with an external link added to compare internal and external reference behavior.
Purpose
Problem
References such as
:ref:and:doc:are incorrectly rendered in man page output with Docutils 0.22 and later. For example, they appear asref <#other-ref>ordoc <>instead of being rendered correctly.Cause
Docutils 0.22 introduced two changes that caused this regression:
manpage.Translator.__init__started assigningvisit_referenceanddepart_referenceas instance attributes.ManualPageTranslator.visit_referencewas never called._visit_reference_no_macrowas used, outputting the rawrefuriinside angle brackets.defs['reference']was removed.visit_referencewould raise aKeyError: 'reference'because the definition was no longer available.Fix
visit_referenceanddepart_referencefromself.__dict__inManualPageTranslator.__init__, allowing normal method lookup to resolve to the class implementation.defs['reference']definition when it is missing, using the same value as Docutils 0.21:(r'\fI\%', r'\fP').Verification
uv run pytest tests/test_builders/test_build_manpage.py: 8 passed on all three versions.ruff check,ruff format --check, andmypy: passed.Screenshots
Rendered HTML comparison using mandoc
The following image combines the Before (left) and After (right) results.
References
Closes #14427
AI Disclosure
This PR was developed with Claude Code (Claude Opus 5.5). It was used to:
sphinx/writers/manpage.py.tests/roots/test-manpage-references/and two regression tests.CHANGES.rst.I reviewed the changes and am responsible for understanding and verifying the submitted implementation.