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