Lennart Poettering | 6b91ae1 | 2012-09-13 04:05:28 +0200 | [diff] [blame] | 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/>. |
Lennart Poettering | 9c4fa6e | 2012-07-16 17:19:39 +0200 | [diff] [blame] | 19 | |
| 20 | from xml.etree.ElementTree import parse, Element, SubElement, tostring |
Lennart Poettering | 8864111 | 2012-07-16 17:39:26 +0200 | [diff] [blame] | 21 | from sys import argv, stdout |
Lennart Poettering | 9c4fa6e | 2012-07-16 17:19:39 +0200 | [diff] [blame] | 22 | |
| 23 | index = {} |
| 24 | |
Kay Sievers | 7653b3c | 2012-07-16 21:27:06 +0200 | [diff] [blame] | 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 | |
Lennart Poettering | 8864111 | 2012-07-16 17:39:26 +0200 | [diff] [blame] | 40 | for p in argv[1:]: |
Lennart Poettering | 9c4fa6e | 2012-07-16 17:19:39 +0200 | [diff] [blame] | 41 | t = parse(p) |
Lennart Poettering | 8864111 | 2012-07-16 17:39:26 +0200 | [diff] [blame] | 42 | section = t.find('./refmeta/manvolnum').text |
Kay Sievers | 7653b3c | 2012-07-16 21:27:06 +0200 | [diff] [blame] | 43 | purpose = ' '.join(t.find('./refnamediv/refpurpose').text.split()) |
Lennart Poettering | 9c4fa6e | 2012-07-16 17:19:39 +0200 | [diff] [blame] | 44 | for f in t.findall('./refnamediv/refname'): |
Lennart Poettering | 8864111 | 2012-07-16 17:39:26 +0200 | [diff] [blame] | 45 | index[f.text] = (p, section, purpose) |
Lennart Poettering | 9c4fa6e | 2012-07-16 17:19:39 +0200 | [diff] [blame] | 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 |
Lennart Poettering | 8864111 | 2012-07-16 17:39:26 +0200 | [diff] [blame] | 58 | for n in sorted(index.keys(), key = str.lower): |
| 59 | path, section, purpose = index[n] |
Lennart Poettering | 9c4fa6e | 2012-07-16 17:19:39 +0200 | [diff] [blame] | 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 | |
Lennart Poettering | a6c9b1c | 2012-07-16 19:26:08 +0200 | [diff] [blame] | 71 | h2 = SubElement(body, 'h2') |
Lennart Poettering | 9c4fa6e | 2012-07-16 17:19:39 +0200 | [diff] [blame] | 72 | h2.text = letter |
| 73 | |
| 74 | ul = SubElement(body, 'ul') |
| 75 | ul.set('style', 'list-style-type:none') |
| 76 | |
Lennart Poettering | 92e1ecc | 2012-07-16 18:10:18 +0200 | [diff] [blame] | 77 | li = SubElement(ul, 'li') |
Lennart Poettering | 9c4fa6e | 2012-07-16 17:19:39 +0200 | [diff] [blame] | 78 | |
Lennart Poettering | 92e1ecc | 2012-07-16 18:10:18 +0200 | [diff] [blame] | 79 | a = SubElement(li, 'a') |
Lennart Poettering | 9c4fa6e | 2012-07-16 17:19:39 +0200 | [diff] [blame] | 80 | a.set('href', path) |
| 81 | a.text = n + '(' + section + ')' |
Lennart Poettering | 92e1ecc | 2012-07-16 18:10:18 +0200 | [diff] [blame] | 82 | a.tail = ' -- ' |
| 83 | |
| 84 | i = SubElement(li, 'i') |
| 85 | i.text = purpose |
Lennart Poettering | 9c4fa6e | 2012-07-16 17:19:39 +0200 | [diff] [blame] | 86 | |
Lennart Poettering | 051eaeb | 2012-07-16 19:11:10 +0200 | [diff] [blame] | 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 | |
Mantas Mikulėnas | b56d18e | 2012-07-25 02:59:28 +0300 | [diff] [blame] | 92 | if hasattr(stdout, "buffer"): |
| 93 | stdout = stdout.buffer |
Kay Sievers | 7653b3c | 2012-07-16 21:27:06 +0200 | [diff] [blame] | 94 | prettify(html) |
Lennart Poettering | 8864111 | 2012-07-16 17:39:26 +0200 | [diff] [blame] | 95 | stdout.write(tostring(html)) |