]> git.ipfire.org Git - thirdparty/systemd.git/blame - tools/make-man-index.py
Merge pull request #6918 from ssahani/issue-5625
[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+
6b91ae13
LP
4#
5# This file is part of systemd.
6#
7# Copyright 2012 Lennart Poettering
f6b6728d 8# Copyright 2013 Zbigniew Jędrzejewski-Szmek
6b91ae13
LP
9#
10# systemd is free software; you can redistribute it and/or modify it
11# under the terms of the GNU Lesser General Public License as published by
12# the Free Software Foundation; either version 2.1 of the License, or
13# (at your option) any later version.
14#
15# systemd is distributed in the hope that it will be useful, but
16# WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18# Lesser General Public License for more details.
19#
20# You should have received a copy of the GNU Lesser General Public License
21# along with systemd; If not, see <http://www.gnu.org/licenses/>.
9c4fa6ed 22
f6b6728d 23import collections
f6b6728d 24import sys
3c1872eb 25import re
1c6c3ef0 26from xml_helper import xml_parse, xml_print, tree
1a13e31d 27
dd6f5e4f 28MDASH = ' — ' if sys.version_info.major >= 3 else ' -- '
f6b6728d
ZJS
29
30TEMPLATE = '''\
56ba3c78 31<refentry id="systemd.index" conditional="HAVE_PYTHON">
f6b6728d
ZJS
32
33 <refentryinfo>
34 <title>systemd.index</title>
35 <productname>systemd</productname>
36
37 <authorgroup>
38 <author>
39 <contrib>Developer</contrib>
40 <firstname>Lennart</firstname>
41 <surname>Poettering</surname>
42 <email>lennart@poettering.net</email>
43 </author>
44 </authorgroup>
45 </refentryinfo>
46
47 <refmeta>
48 <refentrytitle>systemd.index</refentrytitle>
49 <manvolnum>7</manvolnum>
50 </refmeta>
51
52 <refnamediv>
53 <refname>systemd.index</refname>
54 <refpurpose>List all manpages from the systemd project</refpurpose>
55 </refnamediv>
56</refentry>
57'''
58
59SUMMARY = '''\
60 <refsect1>
61 <title>See Also</title>
62 <para>
63 <citerefentry><refentrytitle>systemd.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry>
64 </para>
65
66 <para id='counts' />
67 </refsect1>
68'''
69
70COUNTS = '\
71This index contains {count} entries, referring to {pages} individual manual pages.'
72
1a13e31d 73
3c1872eb
ZJS
74def check_id(page, t):
75 id = t.getroot().get('id')
76 if not re.search('/' + id + '[.]', page):
77 raise ValueError("id='{}' is not the same as page name '{}'".format(id, page))
78
f6b6728d
ZJS
79def make_index(pages):
80 index = collections.defaultdict(list)
81 for p in pages:
1a13e31d 82 t = xml_parse(p)
3c1872eb 83 check_id(p, t)
88641113 84 section = t.find('./refmeta/manvolnum').text
f6b6728d 85 refname = t.find('./refnamediv/refname').text
7653b3c2 86 purpose = ' '.join(t.find('./refnamediv/refpurpose').text.split())
9c4fa6ed 87 for f in t.findall('./refnamediv/refname'):
f6b6728d
ZJS
88 infos = (f.text, section, purpose, refname)
89 index[f.text[0].upper()].append(infos)
90 return index
9c4fa6ed 91
f6b6728d
ZJS
92def add_letter(template, letter, pages):
93 refsect1 = tree.SubElement(template, 'refsect1')
94 title = tree.SubElement(refsect1, 'title')
95 title.text = letter
96 para = tree.SubElement(refsect1, 'para')
97 for info in sorted(pages, key=lambda info: str.lower(info[0])):
98 refname, section, purpose, realname = info
9c4fa6ed 99
f6b6728d
ZJS
100 b = tree.SubElement(para, 'citerefentry')
101 c = tree.SubElement(b, 'refentrytitle')
102 c.text = refname
103 d = tree.SubElement(b, 'manvolnum')
104 d.text = section
9c4fa6ed 105
dd6f5e4f 106 b.tail = MDASH + purpose # + ' (' + p + ')'
9c4fa6ed 107
f6b6728d 108 tree.SubElement(para, 'sbr')
9c4fa6ed 109
f6b6728d
ZJS
110def add_summary(template, indexpages):
111 count = 0
112 pages = set()
113 for group in indexpages:
114 count += len(group)
115 for info in group:
116 refname, section, purpose, realname = info
117 pages.add((realname, section))
9c4fa6ed 118
f6b6728d
ZJS
119 refsect1 = tree.fromstring(SUMMARY)
120 template.append(refsect1)
9c4fa6ed 121
f6b6728d
ZJS
122 para = template.find(".//para[@id='counts']")
123 para.text = COUNTS.format(count=count, pages=len(pages))
9c4fa6ed 124
1a13e31d 125def make_page(*xml_files):
f6b6728d
ZJS
126 template = tree.fromstring(TEMPLATE)
127 index = make_index(xml_files)
9c4fa6ed 128
f6b6728d
ZJS
129 for letter in sorted(index):
130 add_letter(template, letter, index[letter])
9c4fa6ed 131
f6b6728d 132 add_summary(template, index.values())
9c4fa6ed 133
f6b6728d 134 return template
92e1ecc6 135
f6b6728d 136if __name__ == '__main__':
1a13e31d
ZJS
137 with open(sys.argv[1], 'wb') as f:
138 f.write(xml_print(make_page(*sys.argv[2:])))