XML Formatter QoL improvements #88

Merged
bema merged 7 commits from bema/ref/formatter_reliability_ref into master 2023-03-01 12:40:32 +01:00
Showing only changes of commit 8a863b7650 - Show all commits

View File

@@ -25,18 +25,18 @@ def minimize(source: str) -> str:
:param source: XML :param source: XML
:return: minimized XML :return: minimized XML
""" """
result = source
to_remove = [" ", " ", "\t", "\n"] prolog = ""
to_substitute = [(" <", "<"), ("> ", ">"), ("</ ", "</"), (" >", ">")] prolog_start = source.find("<?")
for chars in to_remove: if prolog_start != -1:
result = result.replace(chars, "") prolog_end = source.find("?>") + 2
prolog = source[prolog_start:prolog_end]
source = source[prolog_end: ]
for e in to_substitute: parser = etree.XMLParser(remove_blank_text=True)
result = result.replace(e[0], e[1]) xml = etree.fromstring(source, parser=parser)
return prolog + etree.tostring(xml, pretty_print=False).decode()
return result
def xpath(source: str, xpath: str) -> str: def xpath(source: str, xpath: str) -> str: