diff --git a/CHANGES.rst b/CHANGES.rst index ad6d698341a..24308770dbf 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -12,6 +12,9 @@ Bugs fixed does not match the language name, such as Chinese (which reuses the English stemmer) and Dutch (which uses the Dutch Porter stemmer). Patch by Hugo van Kemenade +* #12871: gettext: Extract indented doctest blocks when ``doctest-block`` is + enabled in :confval:`gettext_additional_targets`. + Patch by Jared Dillard Release 9.1.0 (released Dec 31, 2025) diff --git a/sphinx/util/nodes.py b/sphinx/util/nodes.py index 90c44c6a9c1..7a566389541 100644 --- a/sphinx/util/nodes.py +++ b/sphinx/util/nodes.py @@ -177,9 +177,12 @@ def apply_source_workaround(node: Element) -> None: ) node.source, node.line = node.parent.source, node.parent.line - # workaround: literal_block under bullet list - # See: https://github.com/sphinx-doc/sphinx/issues/4913 - if isinstance(node, nodes.literal_block) and node.source is None: + # Work around Docutils not setting ``source`` on: + # - literal blocks under bullet lists (#4913) + # - doctest blocks under block quotes (#12871) + if isinstance(node, (nodes.literal_block, nodes.doctest_block)) and ( + node.source is None + ): with contextlib.suppress(ValueError): node.source = get_node_source(node) diff --git a/tests/roots/test-gettext-doctest-block/conf.py b/tests/roots/test-gettext-doctest-block/conf.py new file mode 100644 index 00000000000..04a8923174a --- /dev/null +++ b/tests/roots/test-gettext-doctest-block/conf.py @@ -0,0 +1 @@ +project = 'test-gettext-doctest-block' diff --git a/tests/roots/test-gettext-doctest-block/index.rst b/tests/roots/test-gettext-doctest-block/index.rst new file mode 100644 index 00000000000..250ee82f7b8 --- /dev/null +++ b/tests/roots/test-gettext-doctest-block/index.rst @@ -0,0 +1,5 @@ +Indented doctest block +====================== + + >>> 2 + 2 + 4 diff --git a/tests/test_builders/test_build_gettext.py b/tests/test_builders/test_build_gettext.py index 30798cc5070..8ef5a1762cb 100644 --- a/tests/test_builders/test_build_gettext.py +++ b/tests/test_builders/test_build_gettext.py @@ -325,6 +325,22 @@ def test_gettext_literalblock_additional(app: SphinxTestApp) -> None: ] +@pytest.mark.sphinx( + 'gettext', + testroot='gettext-doctest-block', + confoverrides={'gettext_additional_targets': ['doctest-block']}, +) +def test_gettext_indented_doctest_block(app: SphinxTestApp) -> None: + app.build(force_all=True) + + pot = (app.outdir / 'index.pot').read_text(encoding='utf8') + + assert get_msgids(pot) == [ + 'Indented doctest block', + '>>> 2 + 2\\n4', + ] + + @pytest.mark.sphinx('gettext', testroot='intl', srcdir='gettext') def test_gettext_trailing_backslashes(app: SphinxTestApp) -> None: app.build(force_all=True)