Skip to content

Commit

Permalink
2025-01-09T22:24:33Z
Browse files Browse the repository at this point in the history
  • Loading branch information
wrmsr committed Jan 9, 2025
1 parent 69e7f93 commit 8499b6c
Showing 1 changed file with 27 additions and 16 deletions.
43 changes: 27 additions & 16 deletions omlish/formats/xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,38 @@ def as_dict(self) -> dict[str, ta.Any]:
return dct


def build_simple_element(element: 'ET.Element') -> SimpleElement:
atts = {}
for name, value in element.attrib.items():
atts[name] = value # noqa
def build_simple_element(
element: 'ET.Element',
*,
strip_tag_ns: bool = False,
) -> SimpleElement:
def rec(cur: 'ET.Element') -> SimpleElement:
atts = {}
for name, value in cur.attrib.items():
atts[name] = value # noqa

body: list[SimpleElement | str] = []
body: list[SimpleElement | str] = []

if element.text and (s := element.text.strip()):
body.append(s)
if cur.text and (s := cur.text.strip()):
body.append(s)

for child in element:
body.append(build_simple_element(child))
for child in cur:
body.append(rec(child))

if child.tail and (s := child.tail.strip()):
body.append(s)
if child.tail and (s := child.tail.strip()):
body.append(s)

tag = cur.tag
if strip_tag_ns:
tag = strip_ns(tag)

return SimpleElement(
tag,
atts,
body,
)

return SimpleElement(
element.tag,
atts,
body,
)
return rec(element)


def parse_tree(s: str) -> 'ET.ElementTree':
Expand Down

0 comments on commit 8499b6c

Please sign in to comment.