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

milde--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9702
          http://sourceforge.net/p/docutils/code/9702
Author:   milde
Date:     2024-05-14 12:28:18 +0000 (Tue, 14 May 2024)
Log Message:
-----------
Fix for `misc.Transitions`.

Report an error if a <transition> element follows a <meta> or
<decoration> element as this is invalid according to ``docutils.dtd``.

Modified Paths:
--------------
    trunk/docutils/HISTORY.txt
    trunk/docutils/docutils/transforms/misc.py
    trunk/docutils/test/test_transforms/test_transitions.py

Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt	2024-05-12 15:57:27 UTC (rev 9701)
+++ trunk/docutils/HISTORY.txt	2024-05-14 12:28:18 UTC (rev 9702)
@@ -40,6 +40,12 @@
 
   - Update `DocInfo` to work with corrected element categories.
 
+* docutils/transforms/misc.py:
+
+  - Fix for `misc.Transitions`: report an error if a <transition> element
+    follows a <meta> or <decoration> element as this is invalid
+    according to ``docutils.dtd``.
+
 * docutils/writers/manpage.py
 
   - Remove code for unused emdash bullets.

Modified: trunk/docutils/docutils/transforms/misc.py
===================================================================
--- trunk/docutils/docutils/transforms/misc.py	2024-05-12 15:57:27 UTC (rev 9701)
+++ trunk/docutils/docutils/transforms/misc.py	2024-05-14 12:28:18 UTC (rev 9702)
@@ -71,8 +71,8 @@
 
     """
     Move transitions at the end of sections up the tree.  Complain
-    on transitions after a title, at the beginning or end of the
-    document, and after another transition.
+    on transitions after a title, subtitle, meta, or decoration element,
+    at the beginning or end of the document, and after another transition.
 
     For example, transform this::
 
@@ -99,24 +99,20 @@
 
     def visit_transition(self, node):
         index = node.parent.index(node)
-        error = None
-        if (index == 0
-            or isinstance(node.parent[0], nodes.title)
-            and (index == 1
-                 or isinstance(node.parent[1], nodes.subtitle)
-                 and index == 2)):
-            assert (isinstance(node.parent, nodes.document)
-                    or isinstance(node.parent, nodes.section))
-            error = self.document.reporter.error(
-                'Document or section may not begin with a transition.',
-                source=node.source, line=node.line)
-        elif isinstance(node.parent[index - 1], nodes.transition):
-            error = self.document.reporter.error(
-                'At least one body element must separate transitions; '
-                'adjacent transitions are not allowed.',
-                source=node.source, line=node.line)
-        if error:
+        previous_sibling = node.previous_sibling()
+        msg = ''
+        assert isinstance(node.parent, (nodes.document, nodes.section))
+        if index == 0 or isinstance(previous_sibling, (nodes.title,
+                                                       nodes.subtitle,
+                                                       nodes.meta,
+                                                       nodes.decoration)):
+            msg = 'Document or section may not begin with a transition.'
+        elif isinstance(previous_sibling, nodes.transition):
+            msg = ('At least one body element must separate transitions; '
+                   'adjacent transitions are not allowed.')
+        if msg:
             # Insert before node and update index.
+            error = self.document.reporter.error(msg, base_node=node)
             node.parent.insert(index, error)
             index += 1
         assert index < len(node.parent)

Modified: trunk/docutils/test/test_transforms/test_transitions.py
===================================================================
--- trunk/docutils/test/test_transforms/test_transitions.py	2024-05-12 15:57:27 UTC (rev 9701)
+++ trunk/docutils/test/test_transforms/test_transitions.py	2024-05-14 12:28:18 UTC (rev 9702)
@@ -245,6 +245,43 @@
         Document beginning with a transition.
 """],
 ["""\
+.. meta:: :keywords: transition test
+
+----------
+
+Document beginning with a transition (meta elements don't count).
+""",
+"""\
+<document source="test data">
+    <meta content="transition test" name="keywords">
+    <system_message level="3" line="3" source="test data" type="ERROR">
+        <paragraph>
+            Document or section may not begin with a transition.
+    <transition>
+    <paragraph>
+        Document beginning with a transition (meta elements don't count).
+"""],
+["""\
+.. header:: a header
+
+----------
+
+Document beginning with a transition (decoration elements don't count).
+""",
+"""\
+<document source="test data">
+    <decoration>
+        <header>
+            <paragraph>
+                a header
+    <system_message level="3" line="3" source="test data" type="ERROR">
+        <paragraph>
+            Document or section may not begin with a transition.
+    <transition>
+    <paragraph>
+        Document beginning with a transition (decoration elements don't count).
+"""],
+["""\
 Section 1
 =========
 

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.