telegraf: 1.27.0 -> 1.27.1
[NixPkgs.git] / maintainers / scripts / doc / replace-xrefs-by-empty-links.py
blob2006ef897f7af15be41ca252d858b62b9bf4a3e7
1 #! /usr/bin/env nix-shell
2 #! nix-shell -I nixpkgs=channel:nixos-unstable -i python3 -p python3 -p python3.pkgs.lxml
4 """
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.
8 """
10 import lxml.etree as ET
11 import sys
13 XLINK_NS = "http://www.w3.org/1999/xlink"
15 ns = {
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])