chiark / gitweb /
6224de3cc01d15a7bce1e2a9fb2b29bf4124355d
[elogind.git] / tools / make-man-rules.py
1 #  -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
2 #
3 #  This file is part of systemd.
4 #
5 #  Copyright 2013 Zbigniew JÄ™drzejewski-Szmek
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/>.
19
20 from __future__ import print_function
21 import collections
22 import sys
23 import os.path
24 from xml_helper import *
25
26 SECTION = '''\
27 MANPAGES += \\
28         {manpages}
29 MANPAGES_ALIAS += \\
30         {aliases}
31 {rules}
32 {htmlrules}
33 '''
34
35 CONDITIONAL = '''\
36 if {conditional}
37 ''' \
38 + SECTION + \
39 '''\
40 endif
41 '''
42
43 HEADER = '''\
44 # Do not edit. Generated by make-man-rules.py.
45 # Regenerate with 'make all update-man-list'.
46
47 '''
48
49 HTML_ALIAS_RULE = '''\
50 {}.html: {}.html
51         $(html-alias)
52 '''
53
54 FOOTER = '''\
55
56 EXTRA_DIST += \\
57        {files}
58 '''
59
60 def man(page, number):
61     return 'man/{}.{}'.format(page, number)
62
63 def xml(file):
64     return 'man/{}'.format(os.path.basename(file))
65
66 def add_rules(rules, name):
67     xml = xml_parse(name)
68     # print('parsing {}'.format(name), file=sys.stderr)
69     if xml.getroot().tag != 'refentry':
70         return
71     conditional = xml.getroot().get('conditional') or ''
72     rulegroup = rules[conditional]
73     refmeta = xml.find('./refmeta')
74     title = refmeta.find('./refentrytitle').text
75     number = refmeta.find('./manvolnum').text
76     refnames = xml.findall('./refnamediv/refname')
77     target = man(refnames[0].text, number)
78     if title != refnames[0].text:
79         raise ValueError('refmeta and refnamediv disagree: ' + name)
80     for refname in refnames:
81         assert all(refname not in group
82                    for group in rules.values()), "duplicate page name"
83         alias = man(refname.text, number)
84         rulegroup[alias] = target
85         # print('{} => {} [{}]'.format(alias, target, conditional), file=sys.stderr)
86
87 def create_rules(xml_files):
88     " {conditional => {alias-name => source-name}} "
89     rules = collections.defaultdict(dict)
90     for name in xml_files:
91         try:
92             add_rules(rules, name)
93         except Exception:
94             print("Failed to process", name, file=sys.stderr)
95             raise
96     return rules
97
98 def mjoin(files):
99     return ' \\\n\t'.join(sorted(files) or '#')
100
101 def make_makefile(rules, files):
102     return HEADER + '\n'.join(
103         (CONDITIONAL if conditional else SECTION).format(
104             manpages=mjoin(set(rulegroup.values())),
105             aliases=mjoin(k for k,v in rulegroup.items() if k != v),
106             rules='\n'.join('{}: {}'.format(k,v)
107                             for k,v in sorted(rulegroup.items())
108                             if k != v),
109             htmlrules='\n'.join(HTML_ALIAS_RULE.format(k[:-2],v[:-2])
110                                 for k,v in sorted(rulegroup.items())
111                                 if k != v),
112             conditional=conditional)
113         for conditional,rulegroup in sorted(rules.items())
114         ) + FOOTER.format(files=mjoin(sorted(files)))
115
116 if __name__ == '__main__':
117     rules = create_rules(sys.argv[1:])
118     files = (xml(file) for file in sys.argv[1:])
119     print(make_makefile(rules, files), end='')