chiark / gitweb /
Prep v239: Fix main() to call manager_new() again.
[elogind.git] / tools / make-man-rules.py
1 #!/usr/bin/env python3
2 #  -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
3 #  SPDX-License-Identifier: LGPL-2.1+
4
5 from __future__ import print_function
6 import collections
7 import sys
8 import os.path
9 import pprint
10 from xml_helper import xml_parse
11
12 def man(page, number):
13     return '{}.{}'.format(page, number)
14
15 def xml(file):
16     return os.path.basename(file)
17
18 def add_rules(rules, name):
19     xml = xml_parse(name)
20     # print('parsing {}'.format(name), file=sys.stderr)
21     if xml.getroot().tag != 'refentry':
22         return
23     conditional = xml.getroot().get('conditional') or ''
24     rulegroup = rules[conditional]
25     refmeta = xml.find('./refmeta')
26     title = refmeta.find('./refentrytitle').text
27     number = refmeta.find('./manvolnum').text
28     refnames = xml.findall('./refnamediv/refname')
29     target = man(refnames[0].text, number)
30     if title != refnames[0].text:
31         raise ValueError('refmeta and refnamediv disagree: ' + name)
32     for refname in refnames:
33         assert all(refname not in group
34                    for group in rules.values()), "duplicate page name"
35         alias = man(refname.text, number)
36         rulegroup[alias] = target
37         # print('{} => {} [{}]'.format(alias, target, conditional), file=sys.stderr)
38
39 def create_rules(xml_files):
40     " {conditional => {alias-name => source-name}} "
41     rules = collections.defaultdict(dict)
42     for name in xml_files:
43         try:
44             add_rules(rules, name)
45         except Exception:
46             print("Failed to process", name, file=sys.stderr)
47             raise
48     return rules
49
50 def mjoin(files):
51     return ' \\\n\t'.join(sorted(files) or '#')
52
53 MESON_HEADER = '''\
54 # Do not edit. Generated by make-man-rules.py.
55 manpages = ['''
56
57 MESON_FOOTER = '''\
58 ]
59 # Really, do not edit.'''
60
61 def make_mesonfile(rules, dist_files):
62     # reformat rules as
63     # grouped = [ [name, section, [alias...], condition], ...]
64     #
65     # but first create a dictionary like
66     # lists = { (name, condition) => [alias...]
67     grouped = collections.defaultdict(list)
68     for condition, items in rules.items():
69         for alias, name in items.items():
70             group = grouped[(name, condition)]
71             if name != alias:
72                 group.append(alias)
73
74     lines = [ [p[0][:-2], p[0][-1], sorted(a[:-2] for a in aliases), p[1]]
75               for p, aliases in sorted(grouped.items()) ]
76     return '\n'.join((MESON_HEADER, pprint.pformat(lines)[1:-1], MESON_FOOTER))
77
78 if __name__ == '__main__':
79     pages = sys.argv[1:]
80
81     rules = create_rules(pages)
82     dist_files = (xml(file) for file in pages
83                   if not file.endswith(".directives.xml") and
84                      not file.endswith(".index.xml"))
85     print(make_mesonfile(rules, dist_files))