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

milde--- via Docutils-checkins <[email protected]> Fri, 15 Aug 2025 17:38:10 +0000
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 10203
          http://sourceforge.net/p/docutils/code/10203
Author:   milde
Date:     2025-08-15 17:38:10 +0000 (Fri, 15 Aug 2025)
Log Message:
-----------
Look for valid parent before appending a section element.

Don't append a `<section>` to an `<admonition>` or other body element,
only to <section>` and `<document>`.
Exception: the base node of a nested parser may become the
new section's parent, if it has no parent (detached node).
It is up to the users (or extension implementing a directive) to
prevent or handle these cases.

Modified Paths:
--------------
    trunk/docutils/HISTORY.rst
    trunk/docutils/docutils/parsers/rst/states.py
    trunk/docutils/test/test_parsers/test_rst/test_misc.py

Modified: trunk/docutils/HISTORY.rst
===================================================================
--- trunk/docutils/HISTORY.rst	2025-08-15 17:37:57 UTC (rev 10202)
+++ trunk/docutils/HISTORY.rst	2025-08-15 17:38:10 UTC (rev 10203)
@@ -21,6 +21,11 @@
 
   - More consistent and concise command line help.
 
+* docutils/nodes.py
+
+  - `nodes.Element.section_hierarchy()` now returns only elements
+    with non-empty `.parent`.
+
 * docutils/parsers/rst/states.py
 
   - Relax "section title" system message from SEVERE to ERROR.

Modified: trunk/docutils/docutils/parsers/rst/states.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/states.py	2025-08-15 17:37:57 UTC (rev 10202)
+++ trunk/docutils/docutils/parsers/rst/states.py	2025-08-15 17:38:10 UTC (rev 10203)
@@ -385,7 +385,12 @@
         if newlevel > len(title_styles):
             title_styles.append(style)
         self.memo.section_level = newlevel
-        if newlevel <= oldlevel:
+        if newlevel > oldlevel:
+            # new section is a subsection: get the current section or base node
+            while self.parent.parent and not isinstance(
+                      self.parent, (nodes.section, nodes.document)):
+                self.parent = self.parent.parent
+        else:
             # new section is sibling or higher up in the section hierarchy
             self.parent = parent_sections[newlevel-1].parent
         return True

Modified: trunk/docutils/test/test_parsers/test_rst/test_misc.py
===================================================================
--- trunk/docutils/test/test_parsers/test_rst/test_misc.py	2025-08-15 17:37:57 UTC (rev 10202)
+++ trunk/docutils/test/test_parsers/test_rst/test_misc.py	2025-08-15 17:38:10 UTC (rev 10203)
@@ -138,7 +138,6 @@
                          self.document[-1].pformat())
 
         # new (2nd-level) section title
-        # TODO: don't append <section> to <paragraph>!
         title = self.title_markup('sub 2', '~')
         self.state.nested_parse(title, 0, node=paragraph, match_titles=True)
         self.assertEqual('<section>\n'
@@ -147,9 +146,9 @@
                          '            sub\n'
                          '    <paragraph>\n'
                          '        base node\n'
-                         '        <section ids="sub-2" names="sub\\ 2">\n'
-                         '            <title>\n'
-                         '                sub 2\n',
+                         '    <section ids="sub-2" names="sub\\ 2">\n'
+                         '        <title>\n'
+                         '            sub 2\n',
                          section.pformat())
 
 

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