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

milde--- via Docutils-checkins <[email protected]>
Newsgroups gmane.text.docutils.cvs
Message-ID <[email protected]>
Revision: 9746
          http://sourceforge.net/p/docutils/code/9746
Author:   milde
Date:     2024-06-06 14:03:57 +0000 (Thu, 06 Jun 2024)
Log Message:
-----------
Better error reporting when parsing included (sub)documents.

Pass the included file's path to the parser when the "include"
directive is used with :parser: option.
Enables system messages with correct source/line indication.

Test with Docutils-XML file.

Modified Paths:
--------------
    trunk/docutils/HISTORY.txt
    trunk/docutils/docutils/parsers/rst/directives/misc.py
    trunk/docutils/test/test_parsers/test_rst/test_directives/test_include.py

Added Paths:
-----------
    trunk/docutils/test/data/duplicate-id.xml

Modified: trunk/docutils/HISTORY.txt
===================================================================
--- trunk/docutils/HISTORY.txt	2024-06-06 14:03:45 UTC (rev 9745)
+++ trunk/docutils/HISTORY.txt	2024-06-06 14:03:57 UTC (rev 9746)
@@ -45,6 +45,12 @@
 
   - New method `Parser.finish_parse()` to clean up (before validating).
 
+* docutils/parsers/rst/directives/misc.py
+
+  - Pass the included file's path to the parser when the
+    "include" directive is used with :parser: option.
+    Enables system messages with correct source/line indication.
+
 * docutils/transforms/frontmatter.py
 
   - Update `DocInfo` to work with corrected element categories.

Modified: trunk/docutils/docutils/parsers/rst/directives/misc.py
===================================================================
--- trunk/docutils/docutils/parsers/rst/directives/misc.py	2024-06-06 14:03:45 UTC (rev 9745)
+++ trunk/docutils/docutils/parsers/rst/directives/misc.py	2024-06-06 14:03:57 UTC (rev 9746)
@@ -200,13 +200,18 @@
 
         if 'parser' in self.options:
             # parse into a dummy document and return created nodes
-            document = utils.new_document(path, settings)
+            _settings = settings.copy()
+            _settings._source = path
+            document = utils.new_document(path, _settings)
             document.include_log = include_log + [(path, clip_options)]
             parser = self.options['parser']()
             parser.parse('\n'.join(include_lines), document)
+            self.state.document.parse_messages.extend(document.parse_messages)
             # clean up doctree and complete parsing
             document.transformer.populate_from_components((parser,))
             document.transformer.apply_transforms()
+            self.state.document.transform_messages.extend(
+                document.transform_messages)
             return document.children
 
         # Include as rST source:

Added: trunk/docutils/test/data/duplicate-id.xml
===================================================================
--- trunk/docutils/test/data/duplicate-id.xml	                        (rev 0)
+++ trunk/docutils/test/data/duplicate-id.xml	2024-06-06 14:03:57 UTC (rev 9746)
@@ -0,0 +1,5 @@
+<section>
+    <title ids="s4">nice heading</title>
+    <paragraph>Text with <strong ids="s4">strong
+    statement</strong> and more text.</paragraph>
+</section>


Property changes on: trunk/docutils/test/data/duplicate-id.xml
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:keywords
## -0,0 +1 ##
+Author Date Id Revision
\ No newline at end of property
Modified: trunk/docutils/test/test_parsers/test_rst/test_directives/test_include.py
===================================================================
--- trunk/docutils/test/test_parsers/test_rst/test_directives/test_include.py	2024-06-06 14:03:45 UTC (rev 9745)
+++ trunk/docutils/test/test_parsers/test_rst/test_directives/test_include.py	2024-06-06 14:03:57 UTC (rev 9746)
@@ -90,9 +90,10 @@
 include16 = mydir('includes/include16.txt')
 include_literal = mydir('include_literal.txt')
 include_md = mydir('include.md')
-include = os.path.join(TEST_ROOT, 'data/include.txt')
-latin2 = os.path.join(TEST_ROOT, 'data/latin2.txt')
-utf_16_file = os.path.join(TEST_ROOT, 'data/utf-16-le-sig.txt')
+include_xml = TEST_ROOT/'data/duplicate-id.xml'
+include = TEST_ROOT/'data/include.txt'
+latin2 = TEST_ROOT/'data/latin2.txt'
+utf_16_file = TEST_ROOT/'data/utf-16-le-sig.txt'
 utf_16_error_str = ("UnicodeDecodeError: 'ascii' codec can't decode byte 0xff "
                     "in position 0: ordinal not in range(128)")
 rst_states_dir = os.path.dirname(parsers.rst.states.__file__)
@@ -1491,6 +1492,32 @@
         File "include15.txt": example of rekursive inclusion.
 """],
 [f"""\
+Include Docutils XML file:
+
+.. include:: {include_xml}
+   :parser: xml
+
+The duplicate id is reported and would be appended
+by the "universal.Messages" transform.
+""",
+"""\
+<document source="test data">
+    <paragraph>
+        Include Docutils XML file:
+    <section>
+        <title ids="s4">
+            nice heading
+        <paragraph>
+            Text with \n\
+            <strong ids="s4">
+                strong
+                statement
+             and more text.
+    <paragraph>
+        The duplicate id is reported and would be appended
+        by the "universal.Messages" transform.
+"""],
+[f"""\
 No circular inclusion.
 
 .. list-table::

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.