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