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

milde--- via Docutils-checkins <[email protected]> Sun, 02 Nov 2025 17:51:22 +0000
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 10256
          http://sourceforge.net/p/docutils/code/10256
Author:   milde
Date:     2025-11-02 17:51:21 +0000 (Sun, 02 Nov 2025)
Log Message:
-----------
Relax test for valid parents of topics or sidebars.

Raise error if the parent element belongs to a not-allowed parent class
instead of when the parent element is not in a supported parent class.
The difference is with uncategorized parent classes (like `node.Element`
or `sphinx.addnodes.only`). This allows for auxiliary wrappers during
the parsing step and fixes issues with the Sphinx "only" directive.

cf. https://github.com/sphinx-doc/sphinx/issues/14002

Modified Paths:
--------------
    trunk/docutils/HISTORY.rst
    trunk/docutils/RELEASE-NOTES.rst
    trunk/docutils/docutils/parsers/rst/directives/body.py
    trunk/docutils/docutils/parsers/rst/directives/parts.py
    trunk/docutils/docutils/writers/html5_polyglot/responsive.css
    trunk/docutils/test/test_parsers/test_rst/test_directives/test_sidebars.py

Modified: trunk/docutils/HISTORY.rst
===================================================================
--- trunk/docutils/HISTORY.rst	2025-11-02 17:51:07 UTC (rev 10255)
+++ trunk/docutils/HISTORY.rst	2025-11-02 17:51:21 UTC (rev 10256)
@@ -17,6 +17,13 @@
 Release 0.22.3b1.dev (unpublished)
 ==================================
 
+* docutils/parsers/rst/directives/body.py,
+  docutils/parsers/rst/directives/parts.py
+
+  - Ignore auxiliary elements like `sphinx.addnodes.only`
+    when testing for invalid parents of topics or sidebars
+    (cf. https://github.com/sphinx-doc/sphinx/issues/14002).
+
 * docutils/parsers/rst/states.py
 
   - Ignore combining characters when extracting a grid table block

Modified: trunk/docutils/RELEASE-NOTES.rst
===================================================================
--- trunk/docutils/RELEASE-NOTES.rst	2025-11-02 17:51:07 UTC (rev 10255)
+++ trunk/docutils/RELEASE-NOTES.rst	2025-11-02 17:51:21 UTC (rev 10256)
@@ -267,10 +267,13 @@
 ==================================
 
 Rst parser:
-  Allow for combining characters in grid tables.
-  Fixes bugs #128 and #512.
+  - Allow for combining characters in grid tables.
+    Fixes bugs #128 and #512.
 
+  - Ignore auxiliary elements like `sphinx.addnodes.only`
+    when testing for invalid parents of topics or sidebars.
 
+
 Release 0.22.2 (2025-09-20)
 ===========================
 

Modified: trunk/docutils/docutils/parsers/rst/directives/body.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/directives/body.py	2025-11-02 17:51:07 UTC (rev 10255)
+++ trunk/docutils/docutils/parsers/rst/directives/body.py	2025-11-02 17:51:21 UTC (rev 10256)
@@ -29,9 +29,18 @@
     node_class = None
     """Node class to be used (must be set in subclasses)."""
 
+    invalid_parents = (nodes.SubStructural, nodes.Bibliographic,
+                       nodes.Decorative, nodes.Body, nodes.Part, nodes.topic)
+    """
+    Node categories where topics and sidebars are invalid children.
+
+    Sidebars are only valid in <document> and <section> elements,
+    topics also in <sidebar> elements. However, during parsing,
+    there may be wrapper nodes (like `sphinx.addnodes.only`).
+    """
+
     def run(self):
-        if not isinstance(self.state_machine.node,
-                          (nodes.document, nodes.section, nodes.sidebar)):
+        if isinstance(self.state_machine.node, self.invalid_parents):
             raise self.error('The "%s" directive may not be used within '
                              'topics or body elements.' % self.name)
         self.assert_has_content()

Modified: trunk/docutils/docutils/parsers/rst/directives/parts.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/directives/parts.py	2025-11-02 17:51:07 UTC (rev 10255)
+++ trunk/docutils/docutils/parsers/rst/directives/parts.py	2025-11-02 17:51:21 UTC (rev 10256)
@@ -12,6 +12,7 @@
 from docutils.transforms import parts
 from docutils.parsers.rst import Directive
 from docutils.parsers.rst import directives
+from docutils.parsers.rst.directives.body import BasePseudoSection
 
 
 class Contents(Directive):
@@ -43,8 +44,8 @@
                    'class': directives.class_option}
 
     def run(self):
-        if not isinstance(self.state_machine.node,
-                          (nodes.document, nodes.section, nodes.sidebar)):
+        if isinstance(self.state_machine.node,
+                      BasePseudoSection.invalid_parents):
             raise self.error('The "%s" directive may not be used within '
                              'topics or body elements.' % self.name)
         document = self.state_machine.document

Modified: trunk/docutils/docutils/writers/html5_polyglot/responsive.css
===================================================================
--- trunk/docutils/docutils/writers/html5_polyglot/responsive.css	2025-11-02 17:51:07 UTC (rev 10255)
+++ trunk/docutils/docutils/writers/html5_polyglot/responsive.css	2025-11-02 17:51:21 UTC (rev 10256)
@@ -72,7 +72,12 @@
 pre, dd, dl.option-list {
   margin-left: calc(2% + 1em);
 }
+/* but not in a table cell */
+td pre, td dl.option-list {
+  margin-left: 0;
+}
 
+
 /* Object styling */
 /* ============== */
 
@@ -201,6 +206,8 @@
   text-align: left;
   vertical-align: baseline;
 }
+td *:first-child { margin-top: 0; }
+td *:last-child { margin-bottom: 0; }
 table.booktabs { /* "booktabs" style (no vertical lines) */
   border-top: 2px solid;
   border-bottom: 2px solid;

Modified: trunk/docutils/test/test_parsers/test_rst/test_directives/test_sidebars.py
===================================================================
--- trunk/docutils/test/test_parsers/test_rst/test_directives/test_sidebars.py	2025-11-02 17:51:07 UTC (rev 10255)
+++ trunk/docutils/test/test_parsers/test_rst/test_directives/test_sidebars.py	2025-11-02 17:51:21 UTC (rev 10256)
@@ -23,6 +23,8 @@
 
 
 class ParserTestCase(unittest.TestCase):
+    maxDiff = None
+
     def test_parser(self):
         parser = Parser()
         settings = get_default_settings(Parser)

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