Use a "property" for parsers.rst.states.RSTState.parent.

Using a property attribute instead of runtime initialization ensures
the "insertion point"/"current node" is synchronised across the
statemachine and all state instances.

This may allow support for nested parsing with document-wide section title styles.

git-svn-id: http://svn.code.sf.net/p/docutils/code/trunk@10221 929543f6-e4f2-0310-98a6-ba3bd3dd1d04
This commit is contained in:
milde
2025-08-25 08:33:52 +00:00
parent b48050c4e8
commit 2899f85f4a

View File

@@ -232,11 +232,18 @@ class RSTState(StateWS):
self.document = memo.document
self.inliner = memo.inliner
self.reporter = self.document.reporter
self.parent = self.state_machine.node
# enable the reporter to determine source and source-line
if not hasattr(self.reporter, 'get_source_and_line'):
self.reporter.get_source_and_line = self.state_machine.get_source_and_line # noqa:E501
@property
def parent(self) -> nodes.Element | None:
return self.state_machine.node
@parent.setter
def parent(self, value: nodes.Element):
self.state_machine.node = value
def goto_line(self, abs_line_offset) -> None:
"""
Jump to input line `abs_line_offset`, ignoring jumps past the end.
@@ -425,15 +432,7 @@ class RSTState(StateWS):
section_node += title_messages
self.document.note_implicit_target(section_node, section_node)
# Update state:
self.state_machine.node = section_node
# Also update the ".parent" attribute in all states.
# This is a bit violent, but the state classes copy their .parent from
# state_machine.node on creation, so we need to update them. We could
# also remove RSTState.parent entirely and replace references to it
# with statemachine.node, but that might break code downstream of
# docutils.
for s in self.state_machine.states.values():
s.parent = section_node
self.parent = section_node
def paragraph(self, lines, lineno):
"""