chiark / gitweb /
tabs to spaces
[elogind.git] / make-man-index.py
1 #  -*- Mode: python; indent-tabs-mode: nil -*- */
2 #
3 #  This file is part of systemd.
4 #
5 #  Copyright 2012 Lennart Poettering
6 #
7 #  systemd is free software; you can redistribute it and/or modify it
8 #  under the terms of the GNU Lesser General Public License as published by
9 #  the Free Software Foundation; either version 2.1 of the License, or
10 #  (at your option) any later version.
11 #
12 #  systemd is distributed in the hope that it will be useful, but
13 #  WITHOUT ANY WARRANTY; without even the implied warranty of
14 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 #  Lesser General Public License for more details.
16 #
17 #  You should have received a copy of the GNU Lesser General Public License
18 #  along with systemd; If not, see <http://www.gnu.org/licenses/>.
19
20 from xml.etree.ElementTree import parse, Element, SubElement, tostring
21 from sys import argv, stdout
22
23 index = {}
24
25 def prettify(elem, indent = 0):
26         s = "\n" + indent * "  "
27         if len(elem):
28                 if not elem.text or not elem.text.strip():
29                         elem.text = s + "  "
30                 for e in elem:
31                         prettify(e, indent + 1)
32                         if not e.tail or not e.tail.strip():
33                                 e.tail = s + "  "
34                 if not e.tail or not e.tail.strip():
35                         e.tail = s
36         else:
37                 if indent and (not elem.tail or not elem.tail.strip()):
38                         elem.tail = s
39
40 for p in argv[1:]:
41         t = parse(p)
42         section = t.find('./refmeta/manvolnum').text
43         purpose = ' '.join(t.find('./refnamediv/refpurpose').text.split())
44         for f in t.findall('./refnamediv/refname'):
45                 index[f.text] = (p, section, purpose)
46
47 html = Element('html')
48
49 head = SubElement(html, 'head')
50 title = SubElement(head, 'title')
51 title.text = 'Manual Page Index'
52
53 body = SubElement(html, 'body')
54 h1 = SubElement(body, 'h1')
55 h1.text = 'Manual Page Index'
56
57 letter = None
58 for n in sorted(index.keys(), key = str.lower):
59         path, section, purpose = index[n]
60
61         if path.endswith('.xml'):
62                 path = path[:-4] + ".html"
63
64         c = path.rfind('/')
65         if c >= 0:
66                 path = path[c+1:]
67
68         if letter is None or n[0].upper() != letter:
69                 letter = n[0].upper()
70
71                 h2 = SubElement(body, 'h2')
72                 h2.text = letter
73
74                 ul = SubElement(body, 'ul')
75                 ul.set('style', 'list-style-type:none')
76
77         li = SubElement(ul, 'li')
78
79         a = SubElement(li, 'a')
80         a.set('href', path)
81         a.text = n + '(' + section + ')'
82         a.tail = ' -- '
83
84         i = SubElement(li, 'i')
85         i.text = purpose
86
87 hr = SubElement(body, 'hr')
88
89 p = SubElement(body, 'p')
90 p.text = "This index contains %s entries, referring to %i individual manual pages." % (len(index), len(argv)-1)
91
92 if hasattr(stdout, "buffer"):
93         stdout = stdout.buffer
94 prettify(html)
95 stdout.write(tostring(html))