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

milde--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9981
          http://sourceforge.net/p/docutils/code/9981
Author:   milde
Date:     2024-11-14 16:01:33 +0000 (Thu, 14 Nov 2024)
Log Message:
-----------
XML parser: register substitution definitions.

Call `nodes.note_substitution_def()` for every `<substitution_definition>`
to update the list of substitution definitions.
(The `references.Substitution` transform relies on this list.)

Modified Paths:
--------------
    trunk/docutils/docutils/parsers/docutils_xml.py
    trunk/docutils/test/test_parsers/test_docutils_xml/test_misc.py

Modified: trunk/docutils/docutils/parsers/docutils_xml.py
===================================================================
--- trunk/docutils/docutils/parsers/docutils_xml.py	2024-11-12 10:34:13 UTC (rev 9980)
+++ trunk/docutils/docutils/parsers/docutils_xml.py	2024-11-14 16:01:33 UTC (rev 9981)
@@ -14,9 +14,10 @@
 
 """A Docutils-XML parser.
 
-   Provisional: The API is not fixed yet.
-   Defined objects may be renamed or changed in any Docutils release
-   without prior notice.
+   Provisional:
+     The API is not fixed yet.
+     Defined objects may be renamed or changed
+     in any Docutils release without prior notice.
 """
 
 import re
@@ -133,9 +134,7 @@
         node = nodeclass()
 
     node.line = int(element.get('source line'))
-    if isinstance(node, nodes.decoration):
-        document.decoration = node
-    elif isinstance(node, Unknown):
+    if isinstance(node, Unknown):
         node.tagname = element.tag
         document.reporter.warning(
             f'Unknown element type <{element.tag}>.',
@@ -151,8 +150,15 @@
             if key in node.list_attributes:
                 value = value.split()
             node.attributes[key] = value  # node becomes invalid!
+
+    # Bookkeeping (register some elements/attributes in document-wide lists)
+    if isinstance(node, nodes.decoration):
+        document.decoration = node
+    elif isinstance(node, nodes.substitution_definition):
+        document.note_substitution_def(node, ' '.join(node['names']), document)
     if node['ids']:  # register, check for duplicates
         document.set_id(node)
+    # TODO: anything missing?
 
     # Append content:
     # update "unindent" flag: change line indentation?

Modified: trunk/docutils/test/test_parsers/test_docutils_xml/test_misc.py
===================================================================
--- trunk/docutils/test/test_parsers/test_docutils_xml/test_misc.py	2024-11-12 10:34:13 UTC (rev 9980)
+++ trunk/docutils/test/test_parsers/test_docutils_xml/test_misc.py	2024-11-14 16:01:33 UTC (rev 9981)
@@ -105,6 +105,27 @@
 """],
 ])
 
+totest['substitutions'] = ({},  # resolve substitutions
+[
+["""\
+<document source="test data">
+    <substitution_definition ltrim="0" names="holla">Mara</substitution_definition>
+    <paragraph>Das ist
+    <substitution_reference refname="Holla">Holla</substitution_reference>
+    die Waldfee</paragraph>
+</document>
+""",
+"""\
+<document source="test data">
+    <substitution_definition ltrim="0" names="holla">
+        Mara
+    <paragraph>
+        Das ist
+        Mara
+        \n\
+        die Waldfee
+"""],
+])
 
 if __name__ == '__main__':
     unittest.main()

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.