SF.net SVN: docutils:[9736 ] trunk/docutils/docutils/p arsers/docutils_xml.py

milde--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9736
          http://sourceforge.net/p/docutils/code/9736
Author:   milde
Date:     2024-06-06 14:01:54 +0000 (Thu, 06 Jun 2024)
Log Message:
-----------
xml-parser: Use "XMLPullParser" to add source line number to elements.

Use an "XMLPullParser" to register source line numbers in the parsed elements.
Transfer them to the internal "line" attribute of the document-tree nodes.

(An attempt to create doctree node based on "start"/"end" events became
too complicated, because tailing text is not yet in `element.tail`
when an element's "end" event is reported.

Modified Paths:
--------------
    trunk/docutils/docutils/parsers/docutils_xml.py

Modified: trunk/docutils/docutils/parsers/docutils_xml.py
===================================================================
--- trunk/docutils/docutils/parsers/docutils_xml.py	2024-06-06 14:01:46 UTC (rev 9735)
+++ trunk/docutils/docutils/parsers/docutils_xml.py	2024-06-06 14:01:54 UTC (rev 9736)
@@ -73,7 +73,15 @@
 
     Provisional.
     """
-    return element2node(ET.fromstring(inputstring), document)
+    root = None
+    parser = ET.XMLPullParser(events=('start',))
+    for i, line in enumerate(inputstring.splitlines(keepends=True)):
+        parser.feed(line)
+        for event, element in parser.read_events():
+            if root is None:
+                root = element
+            element.attrib['source line'] = str(i+1)
+    return element2node(root, document)
 
 
 def element2node(element, document=None):
@@ -95,9 +103,11 @@
     else:
         node = nodeclass()
 
+    node.line = int(element.get('source line'))
+
     # Attributes: convert and add to `node.attributes`.
     for key, value in element.items():
-        if key.startswith('{'):
+        if key.startswith('{') or key == 'source line':
             continue  # skip duplicate attributes with namespace URL
         node.attributes[key] = nodes.ATTRIBUTE_VALIDATORS[key](value)
 

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.