summaryrefslogtreecommitdiff
path: root/lib/gir.py
blob: ccfbbc140c2788d3cfa87d41ad1cc2b2fa80290f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
Vala's generated gir does not contain comments,
so we use valadoc to generate them. However, they are formatted
for valadoc and not gi-docgen so we need to fix it.
"""

import xml.etree.ElementTree as ET
import html
import sys
import subprocess


def fix_gir(name: str, gir: str):
    namespaces = {
        "": "http://www.gtk.org/introspection/core/1.0",
        "c": "http://www.gtk.org/introspection/c/1.0",
        "glib": "http://www.gtk.org/introspection/glib/1.0",
    }
    for prefix, uri in namespaces.items():
        ET.register_namespace(prefix, uri)

    tree = ET.parse(gir)
    root = tree.getroot()

    for doc in root.findall(".//doc", namespaces):
        if doc.text:
            doc.text = (
                html.unescape(doc.text).replace("<para>", "").replace("</para>", "")
            )

    if (inc := root.find("c:include", namespaces)) is not None:
        inc.set("name", f"{name}.h")
    else:
        print("no c:include tag found", file=sys.stderr)
        exit(1)

    tree.write(gir, encoding="utf-8", xml_declaration=True)


def valadoc(name: str, gir: str, args: list[str]):
    cmd = ["valadoc", "-o", "docs", "--package-name", name, "--gir", gir, *args]
    try:
        subprocess.run(cmd, check=True, text=True, capture_output=True)
    except subprocess.CalledProcessError as e:
        print(e.stderr, file=sys.stderr)
        exit(1)


if __name__ == "__main__":
    name = sys.argv[1]
    gir = sys.argv[2]
    args = sys.argv[3:]

    valadoc(name, gir, args)
    fix_gir(name, gir)