SF.net SVN: docutils:[9743 ] trunk/docutils
milde--- via Docutils-checkins <[email protected]>
| Newsgroups | gmane.text.docutils.cvs |
|---|---|
| Message-ID | <[email protected]> |
Revision: 9743
http://sourceforge.net/p/docutils/code/9743
Author: milde
Date: 2024-06-06 14:03:21 +0000 (Thu, 06 Jun 2024)
Log Message:
-----------
xml-parser: Unindent continuation lines. Fixes xml round trip.
The XML writer supports formatting XML with newlines and indentation
(see the corresponding configuration settings).
In most reStructuredText markup blocks, indentation has a semantic
importance and is removed before passing the block for further parsing.
As a result, only a limited set of elements may have text with
spaces after a newline.
Remove indentation of continuation lines unless the containing element
is a `FixedTextElement`, `literal`, or `Unknown` instance.
Modified Paths:
--------------
trunk/docutils/docutils/parsers/docutils_xml.py
trunk/docutils/test/test_parsers/test_docutils_xml/test_parse.py
Modified: trunk/docutils/docutils/parsers/docutils_xml.py
===================================================================
--- trunk/docutils/docutils/parsers/docutils_xml.py 2024-06-06 14:03:12 UTC (rev 9742)
+++ trunk/docutils/docutils/parsers/docutils_xml.py 2024-06-06 14:03:21 UTC (rev 9743)
@@ -19,6 +19,7 @@
without prior notice.
"""
+import re
import xml.etree.ElementTree as ET
from docutils import frontend, nodes, parsers, utils
@@ -97,10 +98,16 @@
return element2node(root, document)
-def element2node(element, document=None):
+def element2node(element, document=None, unindent=True):
"""
Convert an `etree` element and its children to Docutils doctree nodes.
+ :element: `xml.etree` element
+ :document: see `parse_element()`
+ :unindent: Remove formatting indentation of follow-up text lines?
+ Cf. `append_text()`.
+ TODO: do we need an "unindent" configuration setting?
+
Return a `docutils.nodes.Element` instance.
Internal.
@@ -141,21 +148,28 @@
value = value.split()
node.attributes[key] = value # node becomes invalid!
- # Append text (wrapped in a `nodes.Text` instance)
- append_text(node, element.text)
-
- # Append children and their tailing text
+ # Append content:
+ # update "unindent" flag: change line indentation?
+ unindent = unindent and not isinstance(
+ node, (nodes.FixedTextElement, nodes.literal, Unknown))
+ # (leading) text
+ append_text(node, element.text, unindent)
+ # children and their tailing text
for child in element:
- node.append(element2node(child, document))
+ node.append(element2node(child, document, unindent))
# Text after a child node
- append_text(node, child.tail)
+ append_text(node, child.tail, unindent)
return node
-def append_text(node, text):
+def append_text(node, text, unindent):
+ # Format `text`, wrap in a TextElement and append to `node`.
+ # Skip if `text` is empty or just formatting whitespace.
if not text:
return
+ if unindent:
+ text = re.sub('\n +', '\n', text)
if isinstance(node, nodes.TextElement):
node.append(nodes.Text(text))
elif text.strip():
Modified: trunk/docutils/test/test_parsers/test_docutils_xml/test_parse.py
===================================================================
--- trunk/docutils/test/test_parsers/test_docutils_xml/test_parse.py 2024-06-06 14:03:12 UTC (rev 9742)
+++ trunk/docutils/test/test_parsers/test_docutils_xml/test_parse.py 2024-06-06 14:03:21 UTC (rev 9743)
@@ -59,12 +59,11 @@
with two lines.</paragraph>
</document>
""",
-# TODO: unindent second line
"""\
<document source="test sample">
<paragraph>
A paragraph
- with two lines.
+ with two lines.
"""],
["""\
<document>
@@ -111,6 +110,25 @@
strong
week
"""],
+["""\
+<document source="test data">
+ <literal_block xml:space="preserve"> Inline element
+with <strong> space at start,
+ in the middle</strong>
+ and after end.</literal_block>
+</document>
+""",
+"""\
+<document source="test data">
+ <literal_block xml:space="preserve">
+ Inline element
+ with \n\
+ <strong>
+ space at start,
+ in the middle
+ \n\
+ and after end.
+"""],
]
totest['attributes'] = [
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.