chiark / gitweb /
util: add hexmem() and strextend() calls
[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 try:
23     from lxml import etree as tree
24     PRETTY = dict(pretty_print=True)
25 except ImportError:
26     import xml.etree.ElementTree as tree
27     PRETTY = {}
28 import sys
29 import re
30 MDASH = ' — ' if sys.version_info.major >= 3 else ' -- '
31
32 TEMPLATE = '''\
33 <refentry id="systemd.index" conditional="HAVE_PYTHON">
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
61 SUMMARY = '''\
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
72 COUNTS = '\
73 This index contains {count} entries, referring to {pages} individual manual pages.'
74
75 def 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
80 def make_index(pages):
81     index = collections.defaultdict(list)
82     for p in pages:
83         t = tree.parse(p)
84         check_id(p, t)
85         section = t.find('./refmeta/manvolnum').text
86         refname = t.find('./refnamediv/refname').text
87         purpose = ' '.join(t.find('./refnamediv/refpurpose').text.split())
88         for f in t.findall('./refnamediv/refname'):
89             infos = (f.text, section, purpose, refname)
90             index[f.text[0].upper()].append(infos)
91     return index
92
93 def 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
100
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
106
107         b.tail = MDASH + purpose # + ' (' + p + ')'
108
109         tree.SubElement(para, 'sbr')
110
111 def 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))
119
120     refsect1 = tree.fromstring(SUMMARY)
121     template.append(refsect1)
122
123     para = template.find(".//para[@id='counts']")
124     para.text = COUNTS.format(count=count, pages=len(pages))
125
126 def make_page(xml_files):
127     template = tree.fromstring(TEMPLATE)
128     index = make_index(xml_files)
129
130     for letter in sorted(index):
131         add_letter(template, letter, index[letter])
132
133     add_summary(template, index.values())
134
135     return template
136
137 if __name__ == '__main__':
138     tree.dump(make_page(sys.argv[1:]), **PRETTY)