blob: 56f38ce413e5e7fc6057e2cd1ffa3ac324adc1fd [file] [log] [blame]
Lennart Poettering9c4fa6e2012-07-16 17:19:39 +02001#!/usr/bin/env python
Lennart Poettering6b91ae12012-09-13 04:05:28 +02002# -*- Mode: python; indent-tabs-mode: nil -*- */
3#
4# This file is part of systemd.
5#
6# Copyright 2012 Lennart Poettering
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/>.
Lennart Poettering9c4fa6e2012-07-16 17:19:39 +020020
21from xml.etree.ElementTree import parse, Element, SubElement, tostring
Lennart Poettering88641112012-07-16 17:39:26 +020022from sys import argv, stdout
Lennart Poettering9c4fa6e2012-07-16 17:19:39 +020023
24index = {}
25
Kay Sievers7653b3c2012-07-16 21:27:06 +020026def prettify(elem, indent = 0):
27 s = "\n" + indent * " "
28 if len(elem):
29 if not elem.text or not elem.text.strip():
30 elem.text = s + " "
31 for e in elem:
32 prettify(e, indent + 1)
33 if not e.tail or not e.tail.strip():
34 e.tail = s + " "
35 if not e.tail or not e.tail.strip():
36 e.tail = s
37 else:
38 if indent and (not elem.tail or not elem.tail.strip()):
39 elem.tail = s
40
Lennart Poettering88641112012-07-16 17:39:26 +020041for p in argv[1:]:
Lennart Poettering9c4fa6e2012-07-16 17:19:39 +020042 t = parse(p)
Lennart Poettering88641112012-07-16 17:39:26 +020043 section = t.find('./refmeta/manvolnum').text
Kay Sievers7653b3c2012-07-16 21:27:06 +020044 purpose = ' '.join(t.find('./refnamediv/refpurpose').text.split())
Lennart Poettering9c4fa6e2012-07-16 17:19:39 +020045 for f in t.findall('./refnamediv/refname'):
Lennart Poettering88641112012-07-16 17:39:26 +020046 index[f.text] = (p, section, purpose)
Lennart Poettering9c4fa6e2012-07-16 17:19:39 +020047
48html = Element('html')
49
50head = SubElement(html, 'head')
51title = SubElement(head, 'title')
52title.text = 'Manual Page Index'
53
54body = SubElement(html, 'body')
55h1 = SubElement(body, 'h1')
56h1.text = 'Manual Page Index'
57
58letter = None
Lennart Poettering88641112012-07-16 17:39:26 +020059for n in sorted(index.keys(), key = str.lower):
60 path, section, purpose = index[n]
Lennart Poettering9c4fa6e2012-07-16 17:19:39 +020061
62 if path.endswith('.xml'):
63 path = path[:-4] + ".html"
64
65 c = path.rfind('/')
66 if c >= 0:
67 path = path[c+1:]
68
69 if letter is None or n[0].upper() != letter:
70 letter = n[0].upper()
71
Lennart Poetteringa6c9b1c2012-07-16 19:26:08 +020072 h2 = SubElement(body, 'h2')
Lennart Poettering9c4fa6e2012-07-16 17:19:39 +020073 h2.text = letter
74
75 ul = SubElement(body, 'ul')
76 ul.set('style', 'list-style-type:none')
77
Lennart Poettering92e1ecc2012-07-16 18:10:18 +020078 li = SubElement(ul, 'li')
Lennart Poettering9c4fa6e2012-07-16 17:19:39 +020079
Lennart Poettering92e1ecc2012-07-16 18:10:18 +020080 a = SubElement(li, 'a')
Lennart Poettering9c4fa6e2012-07-16 17:19:39 +020081 a.set('href', path)
82 a.text = n + '(' + section + ')'
Lennart Poettering92e1ecc2012-07-16 18:10:18 +020083 a.tail = ' -- '
84
85 i = SubElement(li, 'i')
86 i.text = purpose
Lennart Poettering9c4fa6e2012-07-16 17:19:39 +020087
Lennart Poettering051eaeb2012-07-16 19:11:10 +020088hr = SubElement(body, 'hr')
89
90p = SubElement(body, 'p')
91p.text = "This index contains %s entries, referring to %i individual manual pages." % (len(index), len(argv)-1)
92
Mantas Mikulėnasb56d18e2012-07-25 02:59:28 +030093if hasattr(stdout, "buffer"):
94 stdout = stdout.buffer
Kay Sievers7653b3c2012-07-16 21:27:06 +020095prettify(html)
Lennart Poettering88641112012-07-16 17:39:26 +020096stdout.write(tostring(html))