]>
Commit | Line | Data |
---|---|---|
3e67e5c9 | 1 | #!/usr/bin/env python3 |
56ba3c78 | 2 | # -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */ |
35df7443 | 3 | # SPDX-License-Identifier: LGPL-2.1+ |
56ba3c78 ZJS |
4 | |
5 | from __future__ import print_function | |
56ba3c78 ZJS |
6 | import collections |
7 | import sys | |
40be878a | 8 | import os.path |
e2bb4105 | 9 | import pprint |
1c6c3ef0 | 10 | from xml_helper import xml_parse |
56ba3c78 | 11 | |
56ba3c78 | 12 | def man(page, number): |
0689f766 | 13 | return '{}.{}'.format(page, number) |
56ba3c78 | 14 | |
40be878a | 15 | def xml(file): |
0689f766 | 16 | return os.path.basename(file) |
40be878a | 17 | |
56ba3c78 | 18 | def add_rules(rules, name): |
1a13e31d | 19 | xml = xml_parse(name) |
56ba3c78 | 20 | # print('parsing {}'.format(name), file=sys.stderr) |
c0652d45 ZJS |
21 | if xml.getroot().tag != 'refentry': |
22 | return | |
56ba3c78 ZJS |
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 | ||
40be878a | 39 | def create_rules(xml_files): |
56ba3c78 ZJS |
40 | " {conditional => {alias-name => source-name}} " |
41 | rules = collections.defaultdict(dict) | |
42 | for name in xml_files: | |
c0652d45 ZJS |
43 | try: |
44 | add_rules(rules, name) | |
45 | except Exception: | |
46 | print("Failed to process", name, file=sys.stderr) | |
47 | raise | |
56ba3c78 ZJS |
48 | return rules |
49 | ||
50 | def mjoin(files): | |
51 | return ' \\\n\t'.join(sorted(files) or '#') | |
52 | ||
e2bb4105 ZJS |
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 | ||
56ba3c78 | 78 | if __name__ == '__main__': |
0689f766 | 79 | pages = sys.argv[1:] |
e2bb4105 ZJS |
80 | |
81 | rules = create_rules(pages) | |
82 | dist_files = (xml(file) for file in pages | |
e4f42f9d ZJS |
83 | if not file.endswith(".directives.xml") and |
84 | not file.endswith(".index.xml")) | |
0689f766 | 85 | print(make_mesonfile(rules, dist_files)) |