SF.net SVN: docutils:[10131 ] trunk/docutils/docutils

milde--- via Docutils-checkins <[email protected]> Fri, 16 May 2025 12:39:28 +0000
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 10131
          http://sourceforge.net/p/docutils/code/10131
Author:   milde
Date:     2025-05-16 12:39:28 +0000 (Fri, 16 May 2025)
Log Message:
-----------
Refactor section parsing change.

Make the auxilliary function collection section parents a method
of nodes.Element.
Change it to collect only section elements
(starting with `self` if it is a section element).

Modified Paths:
--------------
    trunk/docutils/docutils/nodes.py
    trunk/docutils/docutils/parsers/rst/states.py

Modified: trunk/docutils/docutils/nodes.py
===================================================================
--- trunk/docutils/docutils/nodes.py	2025-05-14 09:18:09 UTC (rev 10130)
+++ trunk/docutils/docutils/nodes.py	2025-05-16 12:39:28 UTC (rev 10131)
@@ -821,6 +821,27 @@
             return None
         return self.parent[i-1] if i > 0 else None
 
+    def section_hierarchy(self) -> list[section]:
+        """Return the element's section hierarchy.
+
+        Return a list of all <section> elements containing `self`
+        (including `self` if it is a <section>).
+
+        List item ``[i]`` is the parent <section> of level i+1
+        (1: section, 2: subsection, 3: subsubsection, ...).
+        The length of the list is the element's section level.
+
+        Provisional. May be changed or removed without warning.
+        """
+        sections = []
+        node = self
+        while node is not None:
+            if isinstance(node, section):
+                sections.append(node)
+            node = node.parent
+        sections.reverse()
+        return sections
+
     def is_not_default(self, key: str) -> bool:
         if self[key] == [] and key in self.list_attributes:
             return False

Modified: trunk/docutils/docutils/parsers/rst/states.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/states.py	2025-05-14 09:18:09 UTC (rev 10130)
+++ trunk/docutils/docutils/parsers/rst/states.py	2025-05-16 12:39:28 UTC (rev 10131)
@@ -326,11 +326,13 @@
         Check for a valid subsection header.  Update section data in `memo`.
 
         When a new section is reached that isn't a subsection of the current
-        section, set `self.parent` to the new section's parent section.
+        section, set `self.parent` to the new section's parent section
+        (or the document if the new section is a top-level section).
         """
         title_styles = self.memo.title_styles
-        section_parents = get_section_parents(self.parent)
-        mylevel = len(section_parents)
+        parent_sections = self.parent.section_hierarchy()
+        # current section level: (0 document, 1 section, 2 subsection, ...)
+        mylevel = len(parent_sections)
         # Determine the level of the new section:
         try:  # check for existing title style
             level = title_styles.index(style) + 1
@@ -349,7 +351,7 @@
         self.memo.section_level = level
         if level <= mylevel:
             # new section is sibling or higher up in the section hierarchy
-            self.parent = section_parents[level-1]
+            self.parent = parent_sections[level-1].parent
         return True
 
     def title_inconsistent(self, sourcetext, lineno):
@@ -360,7 +362,7 @@
         return error
 
     def new_subsection(self, title, lineno, messages):
-        """Append new subsection to document tree. On return, check level."""
+        """Append new subsection to document tree."""
         section_node = nodes.section()
         self.parent += section_node
         textnodes, title_messages = self.inline_text(title, lineno)
@@ -3127,24 +3129,3 @@
                  OptionList, LineBlock, ExtensionOptions, Explicit, Text,
                  Definition, Line, SubstitutionDef, RFC2822Body, RFC2822List)
 """Standard set of State classes used to start `RSTStateMachine`."""
-
-
-# Auxiliary functions
-# ===================
-
-def get_section_parents(node: nodes.Element) -> list[nodes.section]:
-    """Return list of the the current node's parent sections.
-
-    List <section> elements that are parents of the current node.
-    The length of this list is the current section level.
-
-    Provisional. May be changed or removed without warning.
-    """
-    section_parents = []
-    parent = node.parent
-    while parent is not None:
-        if isinstance(parent, (nodes.section, nodes.document)):
-            section_parents.append(parent)
-        parent = parent.parent
-    section_parents.reverse()
-    return section_parents

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.