]>
Commit | Line | Data |
---|---|---|
3e67e5c9 | 1 | #!/usr/bin/env python3 |
db9ecf05 | 2 | # SPDX-License-Identifier: LGPL-2.1-or-later |
9c4fa6ed | 3 | |
f6b6728d | 4 | import collections |
3c1872eb | 5 | import re |
c6041b8b FS |
6 | import sys |
7 | ||
8 | from xml_helper import tree, xml_parse, xml_print | |
1a13e31d | 9 | |
dd6f5e4f | 10 | MDASH = ' — ' if sys.version_info.major >= 3 else ' -- ' |
f6b6728d ZJS |
11 | |
12 | TEMPLATE = '''\ | |
4f0ef40d | 13 | <refentry id="systemd.index"> |
f6b6728d ZJS |
14 | |
15 | <refentryinfo> | |
16 | <title>systemd.index</title> | |
17 | <productname>systemd</productname> | |
f6b6728d ZJS |
18 | </refentryinfo> |
19 | ||
20 | <refmeta> | |
21 | <refentrytitle>systemd.index</refentrytitle> | |
22 | <manvolnum>7</manvolnum> | |
23 | </refmeta> | |
24 | ||
25 | <refnamediv> | |
26 | <refname>systemd.index</refname> | |
27 | <refpurpose>List all manpages from the systemd project</refpurpose> | |
28 | </refnamediv> | |
29 | </refentry> | |
30 | ''' | |
31 | ||
32 | SUMMARY = '''\ | |
33 | <refsect1> | |
34 | <title>See Also</title> | |
35 | <para> | |
36 | <citerefentry><refentrytitle>systemd.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry> | |
37 | </para> | |
38 | ||
39 | <para id='counts' /> | |
40 | </refsect1> | |
41 | ''' | |
42 | ||
43 | COUNTS = '\ | |
44 | This index contains {count} entries, referring to {pages} individual manual pages.' | |
45 | ||
1a13e31d | 46 | |
3c1872eb | 47 | def check_id(page, t): |
c6041b8b | 48 | page_id = t.getroot().get('id') |
c101b656 | 49 | if not re.search('/' + page_id + '[.]', page.translate(str.maketrans('@', '_'))): |
c6041b8b | 50 | raise ValueError(f"id='{page_id}' is not the same as page name '{page}'") |
3c1872eb | 51 | |
f6b6728d ZJS |
52 | def make_index(pages): |
53 | index = collections.defaultdict(list) | |
54 | for p in pages: | |
1a13e31d | 55 | t = xml_parse(p) |
3c1872eb | 56 | check_id(p, t) |
88641113 | 57 | section = t.find('./refmeta/manvolnum').text |
f6b6728d | 58 | refname = t.find('./refnamediv/refname').text |
f3317336 HT |
59 | purpose_text = ' '.join(t.find('./refnamediv/refpurpose').itertext()) |
60 | purpose = ' '.join(purpose_text.split()) | |
9c4fa6ed | 61 | for f in t.findall('./refnamediv/refname'): |
f6b6728d ZJS |
62 | infos = (f.text, section, purpose, refname) |
63 | index[f.text[0].upper()].append(infos) | |
64 | return index | |
9c4fa6ed | 65 | |
f6b6728d ZJS |
66 | def add_letter(template, letter, pages): |
67 | refsect1 = tree.SubElement(template, 'refsect1') | |
68 | title = tree.SubElement(refsect1, 'title') | |
69 | title.text = letter | |
70 | para = tree.SubElement(refsect1, 'para') | |
71 | for info in sorted(pages, key=lambda info: str.lower(info[0])): | |
c6041b8b | 72 | refname, section, purpose, _realname = info |
9c4fa6ed | 73 | |
f6b6728d ZJS |
74 | b = tree.SubElement(para, 'citerefentry') |
75 | c = tree.SubElement(b, 'refentrytitle') | |
76 | c.text = refname | |
77 | d = tree.SubElement(b, 'manvolnum') | |
78 | d.text = section | |
9c4fa6ed | 79 | |
dd6f5e4f | 80 | b.tail = MDASH + purpose # + ' (' + p + ')' |
9c4fa6ed | 81 | |
f6b6728d | 82 | tree.SubElement(para, 'sbr') |
9c4fa6ed | 83 | |
f6b6728d ZJS |
84 | def add_summary(template, indexpages): |
85 | count = 0 | |
86 | pages = set() | |
87 | for group in indexpages: | |
88 | count += len(group) | |
89 | for info in group: | |
c6041b8b | 90 | _refname, section, _purpose, realname = info |
f6b6728d | 91 | pages.add((realname, section)) |
9c4fa6ed | 92 | |
f6b6728d ZJS |
93 | refsect1 = tree.fromstring(SUMMARY) |
94 | template.append(refsect1) | |
9c4fa6ed | 95 | |
f6b6728d ZJS |
96 | para = template.find(".//para[@id='counts']") |
97 | para.text = COUNTS.format(count=count, pages=len(pages)) | |
9c4fa6ed | 98 | |
1a13e31d | 99 | def make_page(*xml_files): |
f6b6728d ZJS |
100 | template = tree.fromstring(TEMPLATE) |
101 | index = make_index(xml_files) | |
9c4fa6ed | 102 | |
f6b6728d ZJS |
103 | for letter in sorted(index): |
104 | add_letter(template, letter, index[letter]) | |
9c4fa6ed | 105 | |
f6b6728d | 106 | add_summary(template, index.values()) |
9c4fa6ed | 107 | |
f6b6728d | 108 | return template |
92e1ecc6 | 109 | |
f6b6728d | 110 | if __name__ == '__main__': |
c6041b8b FS |
111 | with open(sys.argv[1], 'wb') as file: |
112 | file.write(xml_print(make_page(*sys.argv[2:]))) |