Skip to content

manpage: restore reference rendering with Docutils 0.22+ - #14858

Open
Syogo-Suganoya wants to merge 1 commit into
sphinx-doc:masterfrom
Syogo-Suganoya:fix/manpage-ref-doc-rendering
Open

Syogo-Suganoya wants to merge 1 commit into
sphinx-doc:masterfrom
Syogo-Suganoya:fix/manpage-ref-doc-rendering

Conversation

@Syogo-Suganoya

Copy link
Copy Markdown

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 as ref <#other-ref> or doc <> instead of being rendered correctly.

Cause

Docutils 0.22 introduced two changes that caused this regression:

  1. manpage.Translator.__init__ started assigning visit_reference and depart_reference as instance attributes.

    • Instance attributes take precedence over class methods, so Sphinx's ManualPageTranslator.visit_reference was never called.
    • Instead, Docutils's _visit_reference_no_macro was used, outputting the raw refuri inside angle brackets.
  2. defs['reference'] was removed.

    • Even after fixing the first issue, Sphinx's visit_reference would raise a KeyError: 'reference' because the definition was no longer available.

Fix

  • Remove visit_reference and depart_reference from self.__dict__ in ManualPageTranslator.__init__, allowing normal method lookup to resolve to the class implementation.
  • Restore the defs['reference'] definition when it is missing, using the same value as Docutils 0.21: (r'\fI\%', r'\fP').

Verification

  • Verified that the generated roff output is identical to Docutils 0.21 when using Docutils 0.21.2, 0.22.4, and 0.23.
  • uv run pytest tests/test_builders/test_build_manpage.py: 8 passed on all three versions.
    • Confirmed that both newly added tests fail when the fix is reverted.
  • Full test suite: 2408 passed, 24 skipped.
  • ruff check, ruff format --check, and mypy: passed.

Screenshots

Before (master) After (this branch)
01-before-master 02-after-fix

Rendered HTML comparison using mandoc

The following image combines the Before (left) and After (right) results.

03-rendered-mandoc-html

References

Closes #14427

AI Disclosure

This PR was developed with Claude Code (Claude Opus 5.5). It was used to:

  • Reproduce the issue and investigate its root cause by examining the Docutils source code.
  • Implement the fix in sphinx/writers/manpage.py.
  • Add the test root at tests/roots/test-manpage-references/ and two regression tests.
  • Update CHANGES.rst.
  • Run tests against Docutils 0.21, 0.22, and 0.23, as well as the full test suite, Ruff, and mypy.
  • Generate the comparison screenshots.

I reviewed the changes and am responsible for understanding and verifying the submitted implementation.

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]>
Comment thread sphinx/writers/manpage.py
# 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)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread sphinx/writers/manpage.py
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'))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>`.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uses the minimal reproduction example from the issue, with an external link added to compare internal and external reference behavior.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bad references in man pages

1 participant