10.12., 9:00 - 11:00: Due to updates GitLab may be unavailable for some minutes between 09:00 and 11:00.

Commit 85e9b79a authored by eckhart's avatar eckhart

- syntaxtree.Node.as_xml: support '_inline' pseudo attribute

parent 8b148543
...@@ -496,12 +496,15 @@ class Node(collections.abc.Sized): ...@@ -496,12 +496,15 @@ class Node(collections.abc.Sized):
tail = tail.lstrip(None if density & 2 else '') tail = tail.lstrip(None if density & 2 else '')
sep, inner_tab = ('', '') if hasattr(self, '_xml_attr') and '_inline' in self.attributes \
else ('\n', tab)
if self.children: if self.children:
content = [] content = []
for child in self.children: for child in self.children:
subtree = child._tree_repr(tab, open_fn, close_fn, data_fn, density).split('\n') subtree = child._tree_repr(tab, open_fn, close_fn, data_fn, density).split('\n')
content.append('\n'.join((tab + s) for s in subtree)) content.append((sep + inner_tab).join(s for s in subtree))
return head + '\n'.join(content) + tail return head + tab + (sep + inner_tab).join(content) + tail
res = cast(str, self.result) # safe, because if there are no children, result is a string 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] == "<": if density & 1 and res.find('\n') < 0: # and head[0] == "<":
...@@ -534,7 +537,8 @@ class Node(collections.abc.Sized): ...@@ -534,7 +537,8 @@ class Node(collections.abc.Sized):
txt = [left_bracket, node.tag_name] txt = [left_bracket, node.tag_name]
# s += " '(pos %i)" % node.add_pos # s += " '(pos %i)" % node.add_pos
if hasattr(node, '_xml_attr'): if hasattr(node, '_xml_attr'):
txt.extend(' `(%s "%s")' % (k, v) for k, v in node.attributes.items()) txt.extend(' `(%s "%s")' % (k, v)
for k, v in node.attributes.items() if k != '_inline')
if src: if src:
txt.append(" `(pos %i %i %i)" % (node.pos, *line_col(lbreaks, node.pos))) txt.append(" `(pos %i %i %i)" % (node.pos, *line_col(lbreaks, node.pos)))
# if node.error_flag: # just for debugging error collecting # if node.error_flag: # just for debugging error collecting
...@@ -566,30 +570,24 @@ class Node(collections.abc.Sized): ...@@ -566,30 +570,24 @@ class Node(collections.abc.Sized):
column. column.
""" """
inline = False
def opening(node) -> str: def opening(node) -> str:
"""Returns the opening string for the representation of `node`.""" """Returns the opening string for the representation of `node`."""
txt = ['<', node.tag_name] txt = ['<', node.tag_name]
has_reserved_attrs = hasattr(node, '_xml_attr') \ has_reserved_attrs = hasattr(node, '_xml_attr') \
and any (r in node.attributes for r in {'err', 'line', 'col'}) and any (r in node.attributes for r in {'err', 'line', 'col'})
nonlocal inline
inline = False
if hasattr(node, '_xml_attr'): if hasattr(node, '_xml_attr'):
if '_inline' in node.attributes: txt.extend(' %s="%s"' % (k, v)
inline = True for k, v in node.attributes.items() if k != '_inline')
del node.attributes['_inline']
txt.extend(' %s="%s"' % (k, v) for k, v in node.attributes.items())
if src and not has_reserved_attrs: if src and not has_reserved_attrs:
txt.append(' line="%i" col="%i"' % line_col(line_breaks, node.pos)) txt.append(' line="%i" col="%i"' % line_col(line_breaks, node.pos))
if showerrors and node.errors and not has_reserved_attrs: if showerrors and node.errors and not has_reserved_attrs:
txt.append(' err="%s"' % ''.join(str(err).replace('"', r'\"') txt.append(' err="%s"' % ''.join(str(err).replace('"', r'\"')
for err in node.errors)) for err in node.errors))
return "".join(txt + [">" if inline else ">\n"]) return "".join(txt + [">\n"])
def closing(node): def closing(node):
"""Returns the closing string for the representation of `node`.""" """Returns the closing string for the representation of `node`."""
return ('</' if inline else '\n</') + node.tag_name + '>' return ('\n</') + node.tag_name + '>'
line_breaks = linebreaks(src) if src else [] line_breaks = linebreaks(src) if src else []
return self._tree_repr(' ' * indentation, opening, closing, density=1) return self._tree_repr(' ' * indentation, opening, closing, density=1)
......
...@@ -219,7 +219,6 @@ class TestNodeFind(): ...@@ -219,7 +219,6 @@ class TestNodeFind():
def test_getitem(self): def test_getitem(self):
tree = parse_sxpr('(a (b X) (X (c d)) (e (X F)))') tree = parse_sxpr('(a (b X) (X (c d)) (e (X F)))')
# print(tree.as_sxpr())
assert tree[0] == parse_sxpr('(b X)') assert tree[0] == parse_sxpr('(b X)')
assert tree[2] == parse_sxpr('(e (X F))') assert tree[2] == parse_sxpr('(e (X F))')
try: try:
...@@ -256,6 +255,12 @@ class TestSerialization: ...@@ -256,6 +255,12 @@ class TestSerialization:
tree3 = parse_sxpr('(A `(attr "value") `(attr2 "value2") "B")') tree3 = parse_sxpr('(A `(attr "value") `(attr2 "value2") "B")')
assert tree.as_sxpr() == tree3.as_sxpr() assert tree.as_sxpr() == tree3.as_sxpr()
def test_xml_inlining(self):
tree = parse_sxpr('(A (B "C") (D "E"))')
tree.attributes['_inline'] = "1"
xml = tree.as_xml()
assert xml == "<A>\n <B>C</B><D>E</D>\n</A>"
if __name__ == "__main__": 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