12
12
from sphinx .util .docutils import SphinxDirective
13
13
14
14
if TYPE_CHECKING :
15
- from docutils .parsers .rst .states import RSTState
16
- from docutils .statemachine import StringList
17
-
18
15
from sphinx .application import Sphinx
19
16
from sphinx .util .typing import ExtensionMetadata , OptionSpec
20
17
from sphinx .writers .html5 import HTML5Translator
21
18
22
19
23
- class collapsible (nodes .General , nodes .Element ):
20
+ class collapsible (nodes .Structural , nodes .Element ):
24
21
"""Node for collapsible content.
25
22
26
23
This is used by the :rst:dir:`collapse` directive.
@@ -75,28 +72,22 @@ def run(self) -> list[nodes.Node]:
75
72
node .document = self .state .document
76
73
self .set_source_info (node )
77
74
78
- if len ( self .arguments ) > 0 :
75
+ if self .arguments :
79
76
# 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 )
82
79
node .append (summary (trimmed_summary , '' , * textnodes ))
83
80
node += messages
84
81
else :
85
82
label = 'Collapsed Content:'
86
83
node .append (summary (label , label ))
87
84
88
- return self ._parse_content ( self . state , node , self . content , self . content_offset )
85
+ return self .parse_content_to_nodes ( allow_section_headings = True )
89
86
90
87
@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 ()
100
91
101
92
# Find minimum indentation of any non-blank lines after the first.
102
93
# If your indent is larger than a million spaces, there's a problem…
@@ -112,58 +103,9 @@ def _prepare_argument_string(s: str) -> str:
112
103
113
104
return '\n ' .join (lines [:1 ] + [line [margin :] for line in lines [1 :]])
114
105
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.
167
109
HTML_5_BUILDERS = frozenset ({'html' , 'dirhtml' })
168
110
169
111
0 commit comments