1 #! /usr/bin/env nix-shell
2 #! nix-shell -I nixpkgs=channel:nixos-unstable -i python3 -p python3 -p python3.pkgs.lxml
5 Pandoc will try to resolve xrefs and replace them with regular links.
6 let’s replace them with links with empty labels which MyST
7 and our pandoc filters recognize as cross-references.
10 import lxml
.etree
as ET
13 XLINK_NS
= "http://www.w3.org/1999/xlink"
16 "db": "http://docbook.org/ns/docbook",
20 if __name__
== '__main__':
21 assert len(sys
.argv
) >= 3, "usage: replace-xrefs-by-empty-links.py <input> <output>"
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
)
32 tree
.write(sys
.argv
[2])