]> git.ipfire.org Git - thirdparty/systemd.git/blame - make-man-index.py
udev: no need to output OOM, if we call log_oom() anyway
[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
80cb917e
ZJS
22try:
23 from lxml import etree as tree
24 PRETTY = dict(pretty_print=True)
25except ImportError:
26 import xml.etree.ElementTree as tree
27 PRETTY = {}
f6b6728d 28import sys
3c1872eb 29import re
dd6f5e4f 30MDASH = ' — ' if sys.version_info.major >= 3 else ' -- '
f6b6728d
ZJS
31
32TEMPLATE = '''\
56ba3c78 33<refentry id="systemd.index" conditional="HAVE_PYTHON">
f6b6728d
ZJS
34
35 <refentryinfo>
36 <title>systemd.index</title>
37 <productname>systemd</productname>
38
39 <authorgroup>
40 <author>
41 <contrib>Developer</contrib>
42 <firstname>Lennart</firstname>
43 <surname>Poettering</surname>
44 <email>lennart@poettering.net</email>
45 </author>
46 </authorgroup>
47 </refentryinfo>
48
49 <refmeta>
50 <refentrytitle>systemd.index</refentrytitle>
51 <manvolnum>7</manvolnum>
52 </refmeta>
53
54 <refnamediv>
55 <refname>systemd.index</refname>
56 <refpurpose>List all manpages from the systemd project</refpurpose>
57 </refnamediv>
58</refentry>
59'''
60
61SUMMARY = '''\
62 <refsect1>
63 <title>See Also</title>
64 <para>
65 <citerefentry><refentrytitle>systemd.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry>
66 </para>
67
68 <para id='counts' />
69 </refsect1>
70'''
71
72COUNTS = '\
73This index contains {count} entries, referring to {pages} individual manual pages.'
74
3c1872eb
ZJS
75def check_id(page, t):
76 id = t.getroot().get('id')
77 if not re.search('/' + id + '[.]', page):
78 raise ValueError("id='{}' is not the same as page name '{}'".format(id, page))
79
f6b6728d
ZJS
80def make_index(pages):
81 index = collections.defaultdict(list)
82 for p in pages:
83 t = tree.parse(p)
3c1872eb 84 check_id(p, t)
88641113 85 section = t.find('./refmeta/manvolnum').text
f6b6728d 86 refname = t.find('./refnamediv/refname').text
7653b3c2 87 purpose = ' '.join(t.find('./refnamediv/refpurpose').text.split())
9c4fa6ed 88 for f in t.findall('./refnamediv/refname'):
f6b6728d
ZJS
89 infos = (f.text, section, purpose, refname)
90 index[f.text[0].upper()].append(infos)
91 return index
9c4fa6ed 92
f6b6728d
ZJS
93def add_letter(template, letter, pages):
94 refsect1 = tree.SubElement(template, 'refsect1')
95 title = tree.SubElement(refsect1, 'title')
96 title.text = letter
97 para = tree.SubElement(refsect1, 'para')
98 for info in sorted(pages, key=lambda info: str.lower(info[0])):
99 refname, section, purpose, realname = info
9c4fa6ed 100
f6b6728d
ZJS
101 b = tree.SubElement(para, 'citerefentry')
102 c = tree.SubElement(b, 'refentrytitle')
103 c.text = refname
104 d = tree.SubElement(b, 'manvolnum')
105 d.text = section
9c4fa6ed 106
dd6f5e4f 107 b.tail = MDASH + purpose # + ' (' + p + ')'
9c4fa6ed 108
f6b6728d 109 tree.SubElement(para, 'sbr')
9c4fa6ed 110
f6b6728d
ZJS
111def add_summary(template, indexpages):
112 count = 0
113 pages = set()
114 for group in indexpages:
115 count += len(group)
116 for info in group:
117 refname, section, purpose, realname = info
118 pages.add((realname, section))
9c4fa6ed 119
f6b6728d
ZJS
120 refsect1 = tree.fromstring(SUMMARY)
121 template.append(refsect1)
9c4fa6ed 122
f6b6728d
ZJS
123 para = template.find(".//para[@id='counts']")
124 para.text = COUNTS.format(count=count, pages=len(pages))
9c4fa6ed 125
f6b6728d
ZJS
126def make_page(xml_files):
127 template = tree.fromstring(TEMPLATE)
128 index = make_index(xml_files)
9c4fa6ed 129
f6b6728d
ZJS
130 for letter in sorted(index):
131 add_letter(template, letter, index[letter])
9c4fa6ed 132
f6b6728d 133 add_summary(template, index.values())
9c4fa6ed 134
f6b6728d 135 return template
92e1ecc6 136
f6b6728d 137if __name__ == '__main__':
80cb917e 138 tree.dump(make_page(sys.argv[1:]), **PRETTY)