]> git.ipfire.org Git - thirdparty/systemd.git/blame - tools/make-man-index.py
shell-completion: replace "gdb" verb with "debug" for coredumpctl
[thirdparty/systemd.git] / tools / make-man-index.py
CommitLineData
3e67e5c9 1#!/usr/bin/env python3
f6b6728d 2# -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
35df7443 3# SPDX-License-Identifier: LGPL-2.1+
9c4fa6ed 4
f6b6728d 5import collections
f6b6728d 6import sys
3c1872eb 7import re
1c6c3ef0 8from xml_helper import xml_parse, xml_print, tree
1a13e31d 9
dd6f5e4f 10MDASH = ' — ' if sys.version_info.major >= 3 else ' -- '
f6b6728d
ZJS
11
12TEMPLATE = '''\
56ba3c78 13<refentry id="systemd.index" conditional="HAVE_PYTHON">
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
32SUMMARY = '''\
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
43COUNTS = '\
44This index contains {count} entries, referring to {pages} individual manual pages.'
45
1a13e31d 46
3c1872eb
ZJS
47def check_id(page, t):
48 id = t.getroot().get('id')
49 if not re.search('/' + id + '[.]', page):
50 raise ValueError("id='{}' is not the same as page name '{}'".format(id, page))
51
f6b6728d
ZJS
52def 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
7653b3c2 59 purpose = ' '.join(t.find('./refnamediv/refpurpose').text.split())
9c4fa6ed 60 for f in t.findall('./refnamediv/refname'):
f6b6728d
ZJS
61 infos = (f.text, section, purpose, refname)
62 index[f.text[0].upper()].append(infos)
63 return index
9c4fa6ed 64
f6b6728d
ZJS
65def add_letter(template, letter, pages):
66 refsect1 = tree.SubElement(template, 'refsect1')
67 title = tree.SubElement(refsect1, 'title')
68 title.text = letter
69 para = tree.SubElement(refsect1, 'para')
70 for info in sorted(pages, key=lambda info: str.lower(info[0])):
71 refname, section, purpose, realname = info
9c4fa6ed 72
f6b6728d
ZJS
73 b = tree.SubElement(para, 'citerefentry')
74 c = tree.SubElement(b, 'refentrytitle')
75 c.text = refname
76 d = tree.SubElement(b, 'manvolnum')
77 d.text = section
9c4fa6ed 78
dd6f5e4f 79 b.tail = MDASH + purpose # + ' (' + p + ')'
9c4fa6ed 80
f6b6728d 81 tree.SubElement(para, 'sbr')
9c4fa6ed 82
f6b6728d
ZJS
83def add_summary(template, indexpages):
84 count = 0
85 pages = set()
86 for group in indexpages:
87 count += len(group)
88 for info in group:
89 refname, section, purpose, realname = info
90 pages.add((realname, section))
9c4fa6ed 91
f6b6728d
ZJS
92 refsect1 = tree.fromstring(SUMMARY)
93 template.append(refsect1)
9c4fa6ed 94
f6b6728d
ZJS
95 para = template.find(".//para[@id='counts']")
96 para.text = COUNTS.format(count=count, pages=len(pages))
9c4fa6ed 97
1a13e31d 98def make_page(*xml_files):
f6b6728d
ZJS
99 template = tree.fromstring(TEMPLATE)
100 index = make_index(xml_files)
9c4fa6ed 101
f6b6728d
ZJS
102 for letter in sorted(index):
103 add_letter(template, letter, index[letter])
9c4fa6ed 104
f6b6728d 105 add_summary(template, index.values())
9c4fa6ed 106
f6b6728d 107 return template
92e1ecc6 108
f6b6728d 109if __name__ == '__main__':
1a13e31d
ZJS
110 with open(sys.argv[1], 'wb') as f:
111 f.write(xml_print(make_page(*sys.argv[2:])))