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