Debian Codegraph @

doc/fix-rst-dbus.py

raw
1 #!/usr/bin/env python3
2
3 # This script massages the reStructuredText files generated by
4 # gdbus-codegen into something that's slightly more suitable for
5 # the online documentation of XDG Desktop Portal. It's not a
6 # general purpose script.
7
8 import os
9 import sys
10
11 output_dir = sys.argv[1]
12 filename_prefix = sys.argv[2]
13 inputs = sys.argv[3:]
14
15
16 def adjust_title(lines):
17 title = lines[3].strip()
18
19 if title.startswith("org.freedesktop.portal."):
20 adjusted_title = title.replace("org.freedesktop.portal.", "")
21 elif title.startswith("org.freedesktop.impl.portal"):
22 adjusted_title = title.replace("org.freedesktop.impl.portal.", "")
23 elif title.startswith("org.freedesktop.host.portal"):
24 adjusted_title = title.replace("org.freedesktop.host.portal.", "")
25 elif title.startswith("org.freedesktop.background.Monitor"):
26 adjusted_title = title.replace(
27 "org.freedesktop.background.Monitor", "Background Apps Monitor"
28 )
29 else:
30 adjusted_title = title
31
32 # CamelCase → Camel Case
33 if adjusted_title not in ["OpenURI", "ScreenCast"]:
34 adjusted_title = "".join(
35 map(lambda x: x if x.islower() else f" {x}", adjusted_title)
36 )
37
38 lines[3] = f"{adjusted_title}\n"
39
40
41 # Temporary fix for '.. {title}:' strings in the generated files. Should be
42 # removed after GLib 2.78.4 hits the CI images.
43 #
44 # See: https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3751
45 def fix_title_template_string(lines):
46 for index, line in enumerate(lines):
47 if line.strip() == ".. _{title}:":
48 next_title = lines[index + 2].strip()
49 lines[index] = f".. _{next_title}:\n"
50
51
52 for file in inputs:
53 basename = os.path.basename(file)
54 fullpath = os.path.join(output_dir, f"{filename_prefix}-{basename}")
55
56 with open(fullpath) as f:
57 lines = f.readlines()
58
59 adjust_title(lines)
60 fix_title_template_string(lines)
61
62 with open(fullpath, "w") as f:
63 f.writelines(lines)