SF.net SVN: docutils:[10223] trunk/docutils
milde--- via Docutils-checkins <[email protected]> Thu, 28 Aug 2025 16:15:22 +0000
| Newsgroups | gmane.text.docutils.cvs |
|---|---|
| Message-ID | <[email protected]> |
Revision: 10223
http://sourceforge.net/p/docutils/code/10223
Author: milde
Date: 2025-08-28 16:15:22 +0000 (Thu, 28 Aug 2025)
Log Message:
-----------
New attribute to store the parent state machine of nested state machines.
Allows, e.g., passing an updated "current node" to the parent state machine(s)
to fix issues with nested parse with section support (cf. [bugs:#511]).
Modified Paths:
--------------
trunk/docutils/HISTORY.rst
trunk/docutils/docutils/parsers/rst/states.py
trunk/docutils/docutils/statemachine.py
trunk/docutils/test/test_parsers/test_rst/test_nested_parsing.py
Modified: trunk/docutils/HISTORY.rst
===================================================================
--- trunk/docutils/HISTORY.rst 2025-08-26 08:06:11 UTC (rev 10222)
+++ trunk/docutils/HISTORY.rst 2025-08-28 16:15:22 UTC (rev 10223)
@@ -32,11 +32,19 @@
- Ensure new "current node" is valid when switching section level
(cf. bugs #508 and #509).
- Use a `separate title style hierarchy for nested parsing`__.
+ - Set `parent_state_machine` attribute when creating nested state machines.
+ Allows passing an updated "current node" to the parent state machine,
+ e.g. for changing the section level in a directive.
- Better error messages for grid table markup errors (bug #504),
based on patch #214 by Jynn Nelson.
__ RELEASE-NOTES.html#nested-parsing
+* docutils/statemachine.py
+
+ - New attribute `StateMachine.parent_state_machine` to store the
+ parent state machine of nested state machines.
+
* docutils/transforms/references.py
- Better error reports for hyperlinks with embedded URI or alias.
Modified: trunk/docutils/docutils/parsers/rst/states.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/states.py 2025-08-26 08:06:11 UTC (rev 10222)
+++ trunk/docutils/docutils/parsers/rst/states.py 2025-08-28 16:15:22 UTC (rev 10223)
@@ -316,8 +316,10 @@
except IndexError:
pass
if not state_machine:
- state_machine = state_machine_class(debug=self.debug,
- **state_machine_kwargs)
+ state_machine = state_machine_class(
+ debug=self.debug,
+ parent_state_machine=self.state_machine,
+ **state_machine_kwargs)
# run the statemachine and populate `node`:
block_length = len(block)
state_machine.run(block, input_offset, memo=self.memo,
@@ -357,8 +359,10 @@
if state_machine_kwargs is None:
state_machine_kwargs = self.nested_sm_kwargs.copy()
state_machine_kwargs['initial_state'] = initial_state
- state_machine = state_machine_class(debug=self.debug,
- **state_machine_kwargs)
+ state_machine = state_machine_class(
+ debug=self.debug,
+ parent_state_machine=self.state_machine,
+ **state_machine_kwargs)
if blank_finish_state is None:
blank_finish_state = initial_state
state_machine.states[blank_finish_state].blank_finish = blank_finish
Modified: trunk/docutils/docutils/statemachine.py
===================================================================
--- trunk/docutils/docutils/statemachine.py 2025-08-26 08:06:11 UTC (rev 10222)
+++ trunk/docutils/docutils/statemachine.py 2025-08-28 16:15:22 UTC (rev 10223)
@@ -130,7 +130,8 @@
results of processing in a list.
"""
- def __init__(self, state_classes, initial_state, debug=False) -> None:
+ def __init__(self, state_classes, initial_state,
+ debug=False, parent_state_machine=None) -> None:
"""
Initialize a `StateMachine` object; add state objects.
@@ -139,8 +140,8 @@
- `state_classes`: a list of `State` (sub)classes.
- `initial_state`: a string, the class name of the initial state.
- `debug`: a boolean; produce verbose output if true (nonzero).
+ - `parent_state_machine`: the parent of a nested state machine.
"""
-
self.input_lines = None
"""`StringList` of input lines (without newlines).
Filled by `self.run()`."""
@@ -157,6 +158,9 @@
self.debug = debug
"""Debugging mode on/off."""
+ self.parent_state_machine = parent_state_machine
+ """The instance of the parent state machine or None."""
+
self.initial_state = initial_state
"""The name of the initial state (key to `self.states`)."""
Modified: trunk/docutils/test/test_parsers/test_rst/test_nested_parsing.py
===================================================================
--- trunk/docutils/test/test_parsers/test_rst/test_nested_parsing.py 2025-08-26 08:06:11 UTC (rev 10222)
+++ trunk/docutils/test/test_parsers/test_rst/test_nested_parsing.py 2025-08-28 16:15:22 UTC (rev 10223)
@@ -51,7 +51,6 @@
self.state.nested_parse(self.content, input_offset=0,
node=node, match_titles=match_titles)
# Append and move the "insertion point" to the last nested section.
- # TODO: this fails in some cases, see tests below.
self.state_machine.node += node.children
# print(self.state_machine, self.state_machine.node[-1].shortrepr())
try:
@@ -59,6 +58,14 @@
self.state_machine.node = self.state_machine.node[-1]
except IndexError:
pass
+ # pass on the new "current node" to parent state machines
+ sm = self.state_machine
+ try:
+ while True:
+ sm = sm.parent_state_machine
+ sm.node = self.state_machine.node
+ except AttributeError:
+ pass
return [] # node already attached to document
@@ -203,7 +210,7 @@
This paragraph belongs to the last nested section.
"""],
["""\
-.. note:: A preceding directive foils the "insertion point move".
+.. note:: A preceding directive must not foil the "insertion point move".
.. nested::
@@ -212,13 +219,13 @@
nested1.1
---------
-TODO: This paragraph belongs to the last nested section.
+This paragraph belongs to the last nested section.
""",
"""\
<document source="test data">
<note>
<paragraph>
- A preceding directive foils the "insertion point move".
+ A preceding directive must not foil the "insertion point move".
<section ids="nested1" names="nested1">
<title>
nested1
@@ -225,12 +232,24 @@
<section ids="nested1-1" names="nested1.1">
<title>
nested1.1
+ <paragraph>
+ This paragraph belongs to the last nested section.
+"""],
+["""\
+.. nested::
+
+ Keep the "current node", if the nested parse does not
+ contain a section.
+
+This paragraph belongs to the document.
+""",
+"""\
+<document source="test data">
<paragraph>
- TODO: This paragraph belongs to the last nested section.
- <system_message level="2" line="10" source="test data" type="WARNING">
- <paragraph>
- Element <document source="test data"> invalid:
- Child element <paragraph> not allowed at this position.
+ Keep the "current node", if the nested parse does not
+ contain a section.
+ <paragraph>
+ This paragraph belongs to the document.
"""],
# base node == current node
["""\
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.