| author | Alan Dipert
<alan@dipert.org> 2025-10-09 01:36:43 UTC |
| committer | Alan Dipert
<alan@dipert.org> 2025-10-09 01:36:43 UTC |
| parent | 6917e12fc0229c2f5c44c5d6a81179ef9d1c8e94 |
| tools/xml2md.py | +11 | -8 |
diff --git a/tools/xml2md.py b/tools/xml2md.py index 28d449a..58ea4de 100755 --- a/tools/xml2md.py +++ b/tools/xml2md.py @@ -301,6 +301,16 @@ def render_document(root: ET.Element) -> str: return markdown.rstrip() + "\n" +def xml_to_markdown(xml_text: str) -> str: + root = ET.fromstring(xml_text) + if strip_tag(root.tag) != "document": + for child in list(root): + if strip_tag(child.tag) == "document": + root = child + break + return render_document(root) + + def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("input", nargs="?", help="XML file to read (defaults to stdin)") @@ -315,14 +325,7 @@ def main() -> None: else: xml_data = sys.stdin.read() - root = ET.fromstring(xml_data) - if strip_tag(root.tag) != "document": - for child in list(root): - if strip_tag(child.tag) == "document": - root = child - break - - markdown = render_document(root) + markdown = xml_to_markdown(xml_data) if args.output: Path(args.output).write_text(markdown)