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

milde--- via Docutils-checkins <[email protected]> Fri, 25 Apr 2025 13:13:48 +0000
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 10104
          http://sourceforge.net/p/docutils/code/10104
Author:   milde
Date:     2025-04-25 13:13:48 +0000 (Fri, 25 Apr 2025)
Log Message:
-----------
Do not insert <system_message> elements if this results in an invalid doctree.

Add a validity test to fix `nodes.document.set_duplicate_name_id`
and revert addition of <system_message> elements for duplicate explicit targets
if it fails.

Adapt tests.

Revert the no longer required workaround in [r7638].

Cf. [bugs:#489]

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

Modified: trunk/docutils/HISTORY.rst
===================================================================
--- trunk/docutils/HISTORY.rst	2025-04-25 12:08:45 UTC (rev 10103)
+++ trunk/docutils/HISTORY.rst	2025-04-25 13:13:48 UTC (rev 10104)
@@ -73,6 +73,8 @@
   - Removed `Element.set_class()`.
   - Downgrade "duplicate ID" message level from SERIOUS to ERROR.
   - Fix recursion in `Element.get_language_code()`.
+  - Do not insert <system_message> elements for duplicate explicit targets
+    if this results in an invalid doctree (cf. bug #489).
 
 * docutils/parsers/docutils_xml.py
 

Modified: trunk/docutils/docutils/nodes.py
===================================================================
--- trunk/docutils/docutils/nodes.py	2025-04-25 12:08:45 UTC (rev 10103)
+++ trunk/docutils/docutils/nodes.py	2025-04-25 13:13:48 UTC (rev 10104)
@@ -1923,7 +1923,13 @@
                     level, 'Duplicate explicit target name: "%s".' % name,
                     backrefs=[id], base_node=node)
                 if msgnode is not None:
+                    # append <system_message> if valid at this place
                     msgnode += msg
+                    try:
+                        msgnode.validate(recursive=False)
+                    except ValidationError:
+                        msgnode.pop()
+                        msg.parent = None
                 dupname(node, name)
             else:  # new explicit, old implicit -> silently overwrite
                 self.nameids[name] = id

Modified: trunk/docutils/docutils/parsers/rst/states.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/states.py	2025-04-25 12:08:45 UTC (rev 10103)
+++ trunk/docutils/docutils/parsers/rst/states.py	2025-04-25 13:13:48 UTC (rev 10104)
@@ -1636,7 +1636,7 @@
 
     def nest_line_block_lines(self, block) -> None:
         for index in range(1, len(block)):
-            if getattr(block[index], 'indent', None) is None:
+            if block[index].indent is None:
                 block[index].indent = block[index - 1].indent
         self.nest_line_block_segment(block)
 

Modified: trunk/docutils/test/test_parsers/test_rst/test_line_blocks.py
===================================================================
--- trunk/docutils/test/test_parsers/test_rst/test_line_blocks.py	2025-04-25 12:08:45 UTC (rev 10103)
+++ trunk/docutils/test/test_parsers/test_rst/test_line_blocks.py	2025-04-25 13:13:48 UTC (rev 10104)
@@ -322,7 +322,7 @@
             * is not recognized.
 """],
 ["""\
-System messages can appear in place of lines:
+System messages are no longer inserted between <line>s:
 
 | `uff <test1>`_
 | `uff <test2>`_
@@ -330,15 +330,12 @@
 """\
 <document source="test data">
     <paragraph>
-        System messages can appear in place of lines:
+        System messages are no longer inserted between <line>s:
     <line_block>
         <line>
             <reference name="uff" refuri="test1">
                 uff
             <target dupnames="uff" ids="uff" refuri="test1">
-        <system_message backrefs="uff-1" level="2" line="3" source="test data" type="WARNING">
-            <paragraph>
-                Duplicate explicit target name: "uff".
         <line>
             <reference name="uff" refuri="test2">
                 uff

Modified: trunk/docutils/test/test_parsers/test_rst/test_targets.py
===================================================================
--- trunk/docutils/test/test_parsers/test_rst/test_targets.py	2025-04-25 12:08:45 UTC (rev 10103)
+++ trunk/docutils/test/test_parsers/test_rst/test_targets.py	2025-04-25 13:13:48 UTC (rev 10104)
@@ -345,6 +345,8 @@
 .. rubric:: this is a title too
    :name: title
 
+The system message is left dangling
+(to be handled by the "universal.Messages" transform).
 """,
 """\
 <document source="test data">
@@ -355,9 +357,9 @@
         First.
     <rubric dupnames="title" ids="title-1">
         this is a title too
-        <system_message backrefs="title-1" level="2" line="9" source="test data" type="WARNING">
-            <paragraph>
-                Duplicate explicit target name: "title".
+    <paragraph>
+        The system message is left dangling
+        (to be handled by the "universal.Messages" transform).
 """],
 ["""\
 Duplicate targets:
@@ -377,8 +379,14 @@
 
 .. _target: Explicit_external_target
 
+| Do not insert <system_message> element for duplicate
+| _`target`, if this results in an invalid doctree.
+
 .. rubric:: directive with target
    :name: Target
+
+:field list: with
+:_`target`: in a field name
 """,
 """\
 <document source="test data">
@@ -413,11 +421,29 @@
             <paragraph>
                 Duplicate explicit target name: "target".
         <target dupnames="target" ids="target-4" refuri="Explicit_external_target">
-        <rubric dupnames="target" ids="target-5">
+        <line_block>
+            <line>
+                Do not insert <system_message> element for duplicate
+            <line>
+                <target dupnames="target" ids="target-5">
+                    target
+                , if this results in an invalid doctree.
+        <rubric dupnames="target" ids="target-6">
             directive with target
-            <system_message backrefs="target-5" level="2" line="19" source="test data" type="WARNING">
-                <paragraph>
-                    Duplicate explicit target name: "target".
+        <field_list>
+            <field>
+                <field_name>
+                    field list
+                <field_body>
+                    <paragraph>
+                        with
+            <field>
+                <field_name>
+                    <target dupnames="target" ids="target-7">
+                        target
+                <field_body>
+                    <paragraph>
+                        in a field name
 """],
 ["""\
 .. _unescaped colon at end:: no good

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