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

milde--- via Docutils-checkins <[email protected]> Sat, 21 Jun 2025 22:10:16 +0000
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 10177
          http://sourceforge.net/p/docutils/code/10177
Author:   milde
Date:     2025-06-21 22:10:15 +0000 (Sat, 21 Jun 2025)
Log Message:
-----------
Don't report an error for duplicate targets with identical refname.

Widen the existing exception for external targets with duplicate names
(no warning/error, if both refer to the same URI) to indirect targets
(no warning/error, if both refer to the same refname).

Modified Paths:
--------------
    trunk/docutils/HISTORY.rst
    trunk/docutils/docs/ref/rst/restructuredtext.rst
    trunk/docutils/docutils/nodes.py
    trunk/docutils/test/test_parsers/test_rst/test_targets.py

Modified: trunk/docutils/HISTORY.rst
===================================================================
--- trunk/docutils/HISTORY.rst	2025-06-18 14:03:25 UTC (rev 10176)
+++ trunk/docutils/HISTORY.rst	2025-06-21 22:10:15 UTC (rev 10177)
@@ -17,8 +17,13 @@
 Release 0.22rc5 (unpublished)
 =============================
 
-* docutils/docutils/parsers/rst/states.py
+* docutils/nodes.py
 
+  - Don't invalidate indirect targets with duplicate name, if they refer to
+    the same refname (similar to external targets refering to the same URI).
+
+* docutils/parsers/rst/states.py
+
   - "Downgrade" targets generated from hyperlink references with embedded
     URI or alias from explicit to implicit (cf. bug #502).
 

Modified: trunk/docutils/docs/ref/rst/restructuredtext.rst
===================================================================
--- trunk/docutils/docs/ref/rst/restructuredtext.rst	2025-06-18 14:03:25 UTC (rev 10176)
+++ trunk/docutils/docs/ref/rst/restructuredtext.rst	2025-06-21 22:10:15 UTC (rev 10177)
@@ -2478,9 +2478,9 @@
 3. Duplicate explicit hyperlink targets are removed, and level-2
    (warning) system messages are inserted.
 
-4. Exception: duplicate `external hyperlink targets`_ (identical
-   reference names and referenced URIs) do not conflict, and are not
-   removed.
+4. Exception: duplicate external or indirect hyperlink targets
+   (identical reference names and referenced URIs or hyperlink
+   references) do not conflict, one is removed.
 
 System messages are inserted where target links have been removed.
 See "Error Handling" in `PEP 258`_.

Modified: trunk/docutils/docutils/nodes.py
===================================================================
--- trunk/docutils/docutils/nodes.py	2025-06-18 14:03:25 UTC (rev 10176)
+++ trunk/docutils/docutils/nodes.py	2025-06-21 22:10:15 UTC (rev 10177)
@@ -1904,7 +1904,7 @@
         ====  ========  ========  ========  =======  ====  ========  =====
 
         .. [#] Do not clear the name-to-id map or invalidate the old target if
-           both old and new targets are external and refer to identical URIs.
+           both old and new targets refer to identical URIs or reference names.
            The new target is invalidated regardless.
         """
         for name in tuple(node['names']):
@@ -1929,11 +1929,13 @@
 
         self.nametypes[name] = old_explicit or explicit
 
-        if (old_id is not None and 'refuri' in node
-            and node['refuri'] == old_node.get('refuri')):
-            # external targets with same URI -> keep old target
+        if old_id is not None and (
+            'refname' in node and node['refname'] == old_node.get('refname')
+            or 'refuri' in node and node['refuri'] == old_node.get('refuri')
+            ):
+            # indirect targets with same reference -> keep old target
             level = 1
-            ref = node["refuri"]
+            ref = node.get('refuri') or node.get('refname')
             s = f'Duplicate name "{name}" for external target "{ref}".'
             dupname(node, name)
         elif explicit:

Modified: trunk/docutils/test/test_parsers/test_rst/test_targets.py
===================================================================
--- trunk/docutils/test/test_parsers/test_rst/test_targets.py	2025-06-18 14:03:25 UTC (rev 10176)
+++ trunk/docutils/test/test_parsers/test_rst/test_targets.py	2025-06-21 22:10:15 UTC (rev 10177)
@@ -249,6 +249,33 @@
     <target dupnames="example" ids="example-1" refuri="example.rst">
 """],
 ["""\
+Duplicate indirect _`targets` (same refname):
+
+.. _link: targets_
+
+.. _link: targets_
+
+do not conflict. The reference name can be used in a link_.
+""",
+"""\
+<document source="test data">
+    <paragraph>
+        Duplicate indirect \n\
+        <target ids="targets" names="targets">
+            targets
+         (same refname):
+    <target ids="link" names="link" refname="targets">
+    <system_message backrefs="link-1" level="1" line="5" source="test data" type="INFO">
+        <paragraph>
+            Duplicate name "link" for external target "targets".
+    <target dupnames="link" ids="link-1" refname="targets">
+    <paragraph>
+        do not conflict. The reference name can be used in a \n\
+        <reference name="link" refname="link">
+            link
+        .
+"""],
+["""\
 Duplicate implicit targets.
 
 Title

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