Skip to content

Commit 10db540

Browse files
authored
Allow sections in object description directives (#10919)
1 parent 0fd4539 commit 10db540

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

CHANGES

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Features added
2121
Patch by Martin Liska.
2222
* #10881: autosectionlabel: Record the generated section label to the debug log.
2323
* #10268: Correctly URI-escape image filenames.
24+
* #10887: domains: Allow sections in all the content of all object description
25+
directives (e.g. :rst:dir:`py:function`). Patch by Adam Turner
2426

2527
Bugs fixed
2628
----------

sphinx/directives/__init__.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from sphinx.util import docutils
1313
from sphinx.util.docfields import DocFieldTransformer, Field, TypedField
1414
from sphinx.util.docutils import SphinxDirective
15+
from sphinx.util.nodes import nested_parse_with_titles
1516
from sphinx.util.typing import OptionSpec
1617

1718
if TYPE_CHECKING:
@@ -259,7 +260,7 @@ def run(self) -> List[Node]:
259260
# needed for association of version{added,changed} directives
260261
self.env.temp_data['object'] = self.names[0]
261262
self.before_content()
262-
self.state.nested_parse(self.content, self.content_offset, contentnode)
263+
nested_parse_with_titles(self.state, self.content, contentnode)
263264
self.transform_content(contentnode)
264265
self.env.app.emit('object-description-transform',
265266
self.domain, self.objtype, contentnode)

tests/roots/test-object-description-sections/conf.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.. py:function:: func()
2+
3+
Overview
4+
--------
5+
6+
Lorem ipsum dolar sit amet
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""Test object description directives."""
2+
3+
import pytest
4+
from docutils import nodes
5+
6+
from sphinx import addnodes
7+
from sphinx.io import create_publisher
8+
from sphinx.util.docutils import sphinx_domains
9+
10+
11+
def _doctree_for_test(builder, docname: str) -> nodes.document:
12+
builder.env.prepare_settings(docname)
13+
publisher = create_publisher(builder.app, 'restructuredtext')
14+
with sphinx_domains(builder.env):
15+
publisher.set_source(source_path=builder.env.doc2path(docname))
16+
publisher.publish()
17+
return publisher.document
18+
19+
20+
@pytest.mark.sphinx('text', testroot='object-description-sections')
21+
def test_object_description_sections(app):
22+
doctree = _doctree_for_test(app.builder, 'index')
23+
# <document>
24+
# <index>
25+
# <desc>
26+
# <desc_signature>
27+
# <desc_name>
28+
# func
29+
# <desc_parameterlist>
30+
# <desc_content>
31+
# <section>
32+
# <title>
33+
# Overview
34+
# <paragraph>
35+
# Lorem ipsum dolar sit amet
36+
37+
assert isinstance(doctree[0], addnodes.index)
38+
assert isinstance(doctree[1], addnodes.desc)
39+
assert isinstance(doctree[1][0], addnodes.desc_signature)
40+
assert isinstance(doctree[1][1], addnodes.desc_content)
41+
assert isinstance(doctree[1][1][0], nodes.section)
42+
assert isinstance(doctree[1][1][0][0], nodes.title)
43+
assert doctree[1][1][0][0][0] == 'Overview'
44+
assert isinstance(doctree[1][1][0][1], nodes.paragraph)
45+
assert doctree[1][1][0][1][0] == 'Lorem ipsum dolar sit amet'

0 commit comments

Comments
 (0)