chiark / gitweb /
core: fix %U when no User= used
[elogind.git] / make-man-index.py
1 #  -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
2 #
3 #  This file is part of systemd.
4 #
5 #  Copyright 2012 Lennart Poettering
6 #  Copyright 2013 Zbigniew Jędrzejewski-Szmek
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/>.
20
21 import collections
22 import xml.etree.ElementTree as tree
23 import sys
24 MDASH = ' — ' if sys.version_info.major >= 3 else ' -- '
25
26 TEMPLATE = '''\
27 <refentry id="systemd.index">
28
29   <refentryinfo>
30     <title>systemd.index</title>
31     <productname>systemd</productname>
32
33     <authorgroup>
34       <author>
35         <contrib>Developer</contrib>
36         <firstname>Lennart</firstname>
37         <surname>Poettering</surname>
38         <email>lennart@poettering.net</email>
39       </author>
40     </authorgroup>
41   </refentryinfo>
42
43   <refmeta>
44     <refentrytitle>systemd.index</refentrytitle>
45     <manvolnum>7</manvolnum>
46   </refmeta>
47
48   <refnamediv>
49     <refname>systemd.index</refname>
50     <refpurpose>List all manpages from the systemd project</refpurpose>
51   </refnamediv>
52 </refentry>
53 '''
54
55 SUMMARY = '''\
56   <refsect1>
57     <title>See Also</title>
58     <para>
59       <citerefentry><refentrytitle>systemd.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry>
60     </para>
61
62     <para id='counts' />
63   </refsect1>
64 '''
65
66 COUNTS = '\
67 This index contains {count} entries, referring to {pages} individual manual pages.'
68
69 def make_index(pages):
70     index = collections.defaultdict(list)
71     for p in pages:
72         t = tree.parse(p)
73         section = t.find('./refmeta/manvolnum').text
74         refname = t.find('./refnamediv/refname').text
75         purpose = ' '.join(t.find('./refnamediv/refpurpose').text.split())
76         for f in t.findall('./refnamediv/refname'):
77             infos = (f.text, section, purpose, refname)
78             index[f.text[0].upper()].append(infos)
79     return index
80
81 def add_letter(template, letter, pages):
82     refsect1 = tree.SubElement(template, 'refsect1')
83     title = tree.SubElement(refsect1, 'title')
84     title.text = letter
85     para = tree.SubElement(refsect1, 'para')
86     for info in sorted(pages, key=lambda info: str.lower(info[0])):
87         refname, section, purpose, realname = info
88
89         b = tree.SubElement(para, 'citerefentry')
90         c = tree.SubElement(b, 'refentrytitle')
91         c.text = refname
92         d = tree.SubElement(b, 'manvolnum')
93         d.text = section
94
95         b.tail = MDASH + purpose # + ' (' + p + ')'
96
97         tree.SubElement(para, 'sbr')
98
99 def add_summary(template, indexpages):
100     count = 0
101     pages = set()
102     for group in indexpages:
103         count += len(group)
104         for info in group:
105             refname, section, purpose, realname = info
106             pages.add((realname, section))
107
108     refsect1 = tree.fromstring(SUMMARY)
109     template.append(refsect1)
110
111     para = template.find(".//para[@id='counts']")
112     para.text = COUNTS.format(count=count, pages=len(pages))
113
114 def make_page(xml_files):
115     template = tree.fromstring(TEMPLATE)
116     index = make_index(xml_files)
117
118     for letter in sorted(index):
119         add_letter(template, letter, index[letter])
120
121     add_summary(template, index.values())
122
123     return template
124
125 if __name__ == '__main__':
126     tree.dump(make_page(sys.argv[1:]))