1#! /usr/bin/env nix-shell
2#! nix-shell -I nixpkgs=channel:nixos-unstable -i python3 -p python3 -p python3.pkgs.lxml
3
4"""
5Pandoc will try to resolve xrefs and replace them with regular links.
6let’s replace them with links with empty labels which MyST
7and our pandoc filters recognize as cross-references.
8"""
9
10import lxml.etree as ET
11import sys
12
13XLINK_NS = "http://www.w3.org/1999/xlink"
14
15ns = {
16 "db": "http://docbook.org/ns/docbook",
17}
18
19
20if __name__ == '__main__':
21 assert len(sys.argv) >= 3, "usage: replace-xrefs-by-empty-links.py <input> <output>"
22
23 tree = ET.parse(sys.argv[1])
24 for xref in tree.findall(".//db:xref", ns):
25 text = ET.tostring(xref, encoding=str)
26 parent = xref.getparent()
27 link = parent.makeelement('link')
28 target_name = xref.get("linkend")
29 link.set(f"{{{XLINK_NS}}}href", f"#{target_name}")
30 parent.replace(xref, link)
31
32 tree.write(sys.argv[2])