Skip to content

Commit fee0d0f

Browse files
committed
Use parse_content_to_nodes
1 parent 7f720eb commit fee0d0f

File tree

1 file changed

+11
-69
lines changed

1 file changed

+11
-69
lines changed

sphinx/ext/collapse.py

Lines changed: 11 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,12 @@
1212
from sphinx.util.docutils import SphinxDirective
1313

1414
if TYPE_CHECKING:
15-
from docutils.parsers.rst.states import RSTState
16-
from docutils.statemachine import StringList
17-
1815
from sphinx.application import Sphinx
1916
from sphinx.util.typing import ExtensionMetadata, OptionSpec
2017
from sphinx.writers.html5 import HTML5Translator
2118

2219

23-
class collapsible(nodes.General, nodes.Element):
20+
class collapsible(nodes.Structural, nodes.Element):
2421
"""Node for collapsible content.
2522
2623
This is used by the :rst:dir:`collapse` directive.
@@ -75,28 +72,22 @@ def run(self) -> list[nodes.Node]:
7572
node.document = self.state.document
7673
self.set_source_info(node)
7774

78-
if len(self.arguments) > 0:
75+
if self.arguments:
7976
# parse the argument as reST
80-
trimmed_summary = self._prepare_argument_string(self.arguments[0].strip())
81-
textnodes, messages = self.state.inline_text(trimmed_summary, self.lineno)
77+
trimmed_summary = self._dedent_string(self.arguments[0].strip())
78+
textnodes, messages = self.parse_inline(trimmed_summary, lineno=self.lineno)
8279
node.append(summary(trimmed_summary, '', *textnodes))
8380
node += messages
8481
else:
8582
label = 'Collapsed Content:'
8683
node.append(summary(label, label))
8784

88-
return self._parse_content(self.state, node, self.content, self.content_offset)
85+
return self.parse_content_to_nodes(allow_section_headings=True)
8986

9087
@staticmethod
91-
def _prepare_argument_string(s: str) -> str:
92-
"""Prepare a directive argument string.
93-
94-
Remove common leading indentation, where the indentation of the first
95-
line is ignored.
96-
97-
Return a list of lines usable for inserting into a docutils StringList.
98-
"""
99-
lines = s.expandtabs().splitlines()
88+
def _dedent_string(s: str) -> str:
89+
"""Remove common leading indentation."""
90+
lines = s.expandtabs(4).splitlines()
10091

10192
# Find minimum indentation of any non-blank lines after the first.
10293
# If your indent is larger than a million spaces, there's a problem…
@@ -112,58 +103,9 @@ def _prepare_argument_string(s: str) -> str:
112103

113104
return '\n'.join(lines[:1] + [line[margin:] for line in lines[1:]])
114105

115-
@staticmethod
116-
def _parse_content(
117-
state: RSTState,
118-
node: nodes.Element,
119-
content: StringList,
120-
offset: int,
121-
) -> list[nodes.Node]:
122-
# Same as util.nested_parse_with_titles but try to handle nested
123-
# sections which should be raised higher up the doctree.
124-
memo = state.memo
125-
surrounding_title_styles = memo.title_styles
126-
surrounding_section_level = memo.section_level
127-
memo.title_styles = []
128-
memo.section_level = 0
129-
try:
130-
state.nested_parse(content, offset, node, match_titles=True)
131-
title_styles = memo.title_styles
132-
if (
133-
not surrounding_title_styles
134-
or not title_styles
135-
or title_styles[0] not in surrounding_title_styles
136-
or not state.parent
137-
):
138-
# No nested sections so no special handling needed.
139-
return [node]
140-
# Calculate the depths of the current and nested sections.
141-
current_depth = 0
142-
parent = state.parent
143-
while parent:
144-
current_depth += 1
145-
parent = parent.parent
146-
current_depth -= 2
147-
title_style = title_styles[0]
148-
nested_depth = len(surrounding_title_styles)
149-
if title_style in surrounding_title_styles:
150-
nested_depth = surrounding_title_styles.index(title_style)
151-
# Use these depths to determine where the nested sections should
152-
# be placed in the doctree.
153-
n_sects_to_raise = current_depth - nested_depth + 1
154-
parent = state.parent
155-
for _i in range(n_sects_to_raise):
156-
if parent.parent:
157-
parent = parent.parent
158-
parent.append(node)
159-
return []
160-
finally:
161-
memo.title_styles = surrounding_title_styles
162-
memo.section_level = surrounding_section_level
163-
164-
165-
# This constant can be modified by programmers that create their own
166-
# HTML builders outside the Sphinx core.
106+
107+
#: This constant can be modified by programmers that create their own
108+
#: HTML builders outside the Sphinx core.
167109
HTML_5_BUILDERS = frozenset({'html', 'dirhtml'})
168110

169111

0 commit comments

Comments
 (0)