Commit cd69c3fd authored by di68kap's avatar di68kap

- proper XML-inlining for syntaxtree.Node.as_xml()

parent 0dfccc2e
......@@ -26,7 +26,6 @@ parser classes are defined in the ``parse`` module.
import collections.abc
from collections import OrderedDict
import copy
from DHParser.error import Error, linebreaks, line_col
from DHParser.stringview import StringView
......@@ -518,25 +517,30 @@ class Node(collections.abc.Sized):
tail = tail.lstrip(None if density & 2 else '')
outer_tab = '' if inline else tab
inline = inline or inline_fn(self)
sep, inner_tab = ('', '') if inline else ('\n', tab)
if inline:
head = head.rstrip()
tail = tail.lstrip()
usetab, sep = '', ''
else:
usetab, sep = tab, '\n'
if self.children:
content = []
for child in self.children:
subtree = child._tree_repr(tab, open_fn, close_fn, data_fn,
density, inline, inline_fn).split('\n')
content.append((sep + inner_tab).join(s for s in subtree))
return head + outer_tab + (sep + inner_tab).join(content) + tail
density, inline, inline_fn)
subtree = [subtree] if inline else subtree.split('\n')
content.append((sep + usetab).join(s for s in subtree))
return head + usetab + (sep + usetab).join(content) + tail
res = cast(str, self.result) # safe, because if there are no children, result is a string
if density & 1 and res.find('\n') < 0: # and head[0] == "<":
# except for XML, add a gap between opening statement and content
gap = ' ' if head and head.rstrip()[-1:] != '>' else ''
gap = ' ' if not inline and head and head.rstrip()[-1:] != '>' else ''
return head.rstrip() + gap + data_fn(self.result) + tail.lstrip()
else:
return head + '\n'.join([tab + data_fn(s) for s in res.split('\n')]) + tail
return head + '\n'.join([usetab + data_fn(s) for s in res.split('\n')]) + tail
def as_sxpr(self, src: str = None,
......
......@@ -259,18 +259,32 @@ class TestSerialization:
tree = parse_sxpr('(A (B "C") (D "E"))')
xml = tree.as_xml(inline_tags={'A'})
assert xml == "<A>\n <B>C</B><D>E</D>\n</A>", xml
assert xml == "<A><B>C</B><D>E</D></A>", xml
assert tree.as_xml() != "<A>\n <B>C</B><D>E</D>\n</A>", xml
assert tree.as_xml() == "<A>\n <B>C</B>\n <D>E</D>\n</A>", xml
tree.attributes['xml:space'] = 'preserve'
xml = tree.as_xml()
assert xml == '<A xml:space="preserve">\n <B>C</B><D>E</D>\n</A>', xml
assert xml == '<A xml:space="preserve"><B>C</B><D>E</D></A>', xml
tree = parse_sxpr('(A (B (C "D") (E "F")) (G "H"))')
xml = tree.as_xml(inline_tags={'B'})
assert xml == "<A>\n <B><C>D</C><E>F</E></B>\n <G>H</G>\n</A>", xml
xml = tree.as_xml(inline_tags={'A'})
assert xml == "<A>\n <B><C>D</C><E>F</E></B><G>H</G>\n</A>", xml
assert xml == "<A><B><C>D</C><E>F</E></B><G>H</G></A>", xml
tree = parse_sxpr('(A (B (C "D\nX") (E "F")) (G " H \n Y "))')
xml = tree.as_xml()
assert xml == '<A>\n <B>\n <C>\n D\n X\n </C>\n ' \
'<E>F</E>\n </B>\n <G>\n H \n Y \n </G>\n</A>', xml
xml = tree.as_xml(inline_tags={'A'})
assert xml == '<A><B><C>D\nX</C><E>F</E></B><G> H \n Y </G></A>', xml
# def test_xml2(self):
# tree = parse_sxpr('(A (B (C "D\nX") (E "F")) (G " H \n Y "))')
# print(tree.as_xml())
# print(tree.as_xml(inline_tags={'A'}))
if __name__ == "__main__":
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment