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