]>
Commit | Line | Data |
---|---|---|
1 | #!/usr/bin/env python3 | |
2 | # SPDX-License-Identifier: LGPL-2.1-or-later | |
3 | ||
4 | import collections | |
5 | import re | |
6 | import sys | |
7 | ||
8 | from xml_helper import tree, xml_parse, xml_print | |
9 | ||
10 | MDASH = ' — ' if sys.version_info.major >= 3 else ' -- ' | |
11 | ||
12 | TEMPLATE = '''\ | |
13 | <refentry id="systemd.index"> | |
14 | ||
15 | <refentryinfo> | |
16 | <title>systemd.index</title> | |
17 | <productname>systemd</productname> | |
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 | ||
46 | ||
47 | def check_id(page, t): | |
48 | page_id = t.getroot().get('id') | |
49 | if not re.search('/' + page_id + '[.]', page.translate(str.maketrans('@', '_'))): | |
50 | raise ValueError(f"id='{page_id}' is not the same as page name '{page}'") | |
51 | ||
52 | def make_index(pages): | |
53 | index = collections.defaultdict(list) | |
54 | for p in pages: | |
55 | t = xml_parse(p) | |
56 | check_id(p, t) | |
57 | section = t.find('./refmeta/manvolnum').text | |
58 | refname = t.find('./refnamediv/refname').text | |
59 | purpose_text = ' '.join(t.find('./refnamediv/refpurpose').itertext()) | |
60 | purpose = ' '.join(purpose_text.split()) | |
61 | for f in t.findall('./refnamediv/refname'): | |
62 | infos = (f.text, section, purpose, refname) | |
63 | index[f.text[0].upper()].append(infos) | |
64 | return index | |
65 | ||
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])): | |
72 | refname, section, purpose, _realname = info | |
73 | ||
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 | |
79 | ||
80 | b.tail = MDASH + purpose # + ' (' + p + ')' | |
81 | ||
82 | tree.SubElement(para, 'sbr') | |
83 | ||
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: | |
90 | _refname, section, _purpose, realname = info | |
91 | pages.add((realname, section)) | |
92 | ||
93 | refsect1 = tree.fromstring(SUMMARY) | |
94 | template.append(refsect1) | |
95 | ||
96 | para = template.find(".//para[@id='counts']") | |
97 | para.text = COUNTS.format(count=count, pages=len(pages)) | |
98 | ||
99 | def make_page(*xml_files): | |
100 | template = tree.fromstring(TEMPLATE) | |
101 | index = make_index(xml_files) | |
102 | ||
103 | for letter in sorted(index): | |
104 | add_letter(template, letter, index[letter]) | |
105 | ||
106 | add_summary(template, index.values()) | |
107 | ||
108 | return template | |
109 | ||
110 | if __name__ == '__main__': | |
111 | with open(sys.argv[1], 'wb') as file: | |
112 | file.write(xml_print(make_page(*sys.argv[2:]))) |