]>
Commit | Line | Data |
---|---|---|
3e67e5c9 | 1 | #!/usr/bin/env python3 |
35df7443 | 2 | # SPDX-License-Identifier: LGPL-2.1+ |
e7098b69 | 3 | |
d9cfd694 ZJS |
4 | import sys |
5 | import collections | |
ccc9a4f9 | 6 | import re |
1c6c3ef0 | 7 | from xml_helper import xml_parse, xml_print, tree |
827f70eb | 8 | from copy import deepcopy |
d9cfd694 | 9 | |
0acfdd61 ZJS |
10 | COLOPHON = '''\ |
11 | This index contains {count} entries in {sections} sections, | |
12 | referring to {pages} individual manual pages. | |
13 | ''' | |
14 | ||
d970bd6d | 15 | def _extract_directives(directive_groups, formatting, page): |
1a13e31d | 16 | t = xml_parse(page) |
d9cfd694 ZJS |
17 | section = t.find('./refmeta/manvolnum').text |
18 | pagename = t.find('./refmeta/refentrytitle').text | |
a4e0b94d ZJS |
19 | |
20 | storopt = directive_groups['options'] | |
d9cfd694 | 21 | for variablelist in t.iterfind('.//variablelist'): |
ccc9a4f9 | 22 | klass = variablelist.attrib.get('class') |
acbfdec3 | 23 | searchpath = variablelist.attrib.get('xpath','./varlistentry/term/varname') |
ccc9a4f9 | 24 | storvar = directive_groups[klass or 'miscellaneous'] |
ccc9a4f9 | 25 | # <option>s go in OPTIONS, unless class is specified |
acbfdec3 | 26 | for xpath, stor in ((searchpath, storvar), |
ccc9a4f9 ZJS |
27 | ('./varlistentry/term/option', |
28 | storvar if klass else storopt)): | |
29 | for name in variablelist.iterfind(xpath): | |
30 | text = re.sub(r'([= ]).*', r'\1', name.text).rstrip() | |
8c6c56c3 ZJS |
31 | if text.startswith('-'): |
32 | # for options, merge options with and without mandatory arg | |
33 | text = text.partition('=')[0] | |
ccc9a4f9 | 34 | stor[text].append((pagename, section)) |
d970bd6d ZJS |
35 | if text not in formatting: |
36 | # use element as formatted display | |
f8b68539 | 37 | if name.text[-1] in "= '": |
699ad6c0 ZJS |
38 | name.clear() |
39 | else: | |
40 | name.tail = '' | |
d970bd6d ZJS |
41 | name.text = text |
42 | formatting[text] = name | |
8906e262 JR |
43 | extra = variablelist.attrib.get('extra-ref') |
44 | if extra: | |
45 | stor[extra].append((pagename, section)) | |
46 | if extra not in formatting: | |
47 | elt = tree.Element("varname") | |
48 | elt.text= extra | |
49 | formatting[extra] = elt | |
d9cfd694 | 50 | |
a4e0b94d | 51 | storfile = directive_groups['filenames'] |
845c5324 ZJS |
52 | for xpath, absolute_only in (('.//refsynopsisdiv//filename', False), |
53 | ('.//refsynopsisdiv//command', False), | |
54 | ('.//filename', True)): | |
a4e0b94d | 55 | for name in t.iterfind(xpath): |
845c5324 ZJS |
56 | if absolute_only and not (name.text and name.text.startswith('/')): |
57 | continue | |
b0343f8c | 58 | if name.attrib.get('index') == 'false': |
845c5324 | 59 | continue |
a4e0b94d ZJS |
60 | name.tail = '' |
61 | if name.text: | |
845c5324 ZJS |
62 | if name.text.endswith('*'): |
63 | name.text = name.text[:-1] | |
a4e0b94d ZJS |
64 | if not name.text.startswith('.'): |
65 | text = name.text.partition(' ')[0] | |
66 | if text != name.text: | |
67 | name.clear() | |
68 | name.text = text | |
845c5324 ZJS |
69 | if text.endswith('/'): |
70 | text = text[:-1] | |
a4e0b94d ZJS |
71 | storfile[text].append((pagename, section)) |
72 | if text not in formatting: | |
73 | # use element as formatted display | |
74 | formatting[text] = name | |
75 | else: | |
76 | text = ' '.join(name.itertext()) | |
77 | storfile[text].append((pagename, section)) | |
78 | formatting[text] = name | |
79 | ||
785a51eb ZJS |
80 | storfile = directive_groups['constants'] |
81 | for name in t.iterfind('.//constant'): | |
b0343f8c | 82 | if name.attrib.get('index') == 'false': |
785a51eb ZJS |
83 | continue |
84 | name.tail = '' | |
85 | if name.text.startswith('('): # a cast, strip it | |
86 | name.text = name.text.partition(' ')[2] | |
87 | storfile[name.text].append((pagename, section)) | |
88 | formatting[name.text] = name | |
89 | ||
0d525a3e ZJS |
90 | storfile = directive_groups['specifiers'] |
91 | for name in t.iterfind(".//table[@class='specifiers']//entry/literal"): | |
92 | if name.text[0] != '%' or name.getparent().text is not None: | |
93 | continue | |
94 | if name.attrib.get('index') == 'false': | |
95 | continue | |
96 | storfile[name.text].append((pagename, section)) | |
97 | formatting[name.text] = name | |
6dbf4025 ZJS |
98 | for name in t.iterfind(".//literal[@class='specifiers']"): |
99 | storfile[name.text].append((pagename, section)) | |
100 | formatting[name.text] = name | |
0d525a3e | 101 | |
d970bd6d | 102 | def _make_section(template, name, directives, formatting): |
eeb019b5 | 103 | varlist = template.find(".//*[@id='{}']".format(name)) |
d9cfd694 ZJS |
104 | for varname, manpages in sorted(directives.items()): |
105 | entry = tree.SubElement(varlist, 'varlistentry') | |
d970bd6d | 106 | term = tree.SubElement(entry, 'term') |
827f70eb ZJS |
107 | display = deepcopy(formatting[varname]) |
108 | term.append(display) | |
d970bd6d | 109 | |
d9cfd694 ZJS |
110 | para = tree.SubElement(tree.SubElement(entry, 'listitem'), 'para') |
111 | ||
112 | b = None | |
ccc9a4f9 | 113 | for manpage, manvolume in sorted(set(manpages)): |
827f70eb ZJS |
114 | if b is not None: |
115 | b.tail = ', ' | |
116 | b = tree.SubElement(para, 'citerefentry') | |
117 | c = tree.SubElement(b, 'refentrytitle') | |
118 | c.text = manpage | |
958caa58 | 119 | c.attrib['target'] = varname |
827f70eb ZJS |
120 | d = tree.SubElement(b, 'manvolnum') |
121 | d.text = manvolume | |
d9cfd694 ZJS |
122 | entry.tail = '\n\n' |
123 | ||
0acfdd61 ZJS |
124 | def _make_colophon(template, groups): |
125 | count = 0 | |
126 | pages = set() | |
127 | for group in groups: | |
128 | count += len(group) | |
129 | for pagelist in group.values(): | |
130 | pages |= set(pagelist) | |
131 | ||
132 | para = template.find(".//para[@id='colophon']") | |
133 | para.text = COLOPHON.format(count=count, | |
134 | sections=len(groups), | |
135 | pages=len(pages)) | |
136 | ||
d970bd6d | 137 | def _make_page(template, directive_groups, formatting): |
d9cfd694 ZJS |
138 | """Create an XML tree from directive_groups. |
139 | ||
140 | directive_groups = { | |
141 | 'class': {'variable': [('manpage', 'manvolume'), ...], | |
142 | 'variable2': ...}, | |
143 | ... | |
144 | } | |
145 | """ | |
d9cfd694 | 146 | for name, directives in directive_groups.items(): |
827f70eb | 147 | _make_section(template, name, directives, formatting) |
d9cfd694 | 148 | |
0acfdd61 ZJS |
149 | _make_colophon(template, directive_groups.values()) |
150 | ||
eeb019b5 | 151 | return template |
d9cfd694 | 152 | |
28223088 | 153 | def make_page(template_path, xml_files): |
d9cfd694 | 154 | "Extract directives from xml_files and return XML index tree." |
28223088 | 155 | template = xml_parse(template_path) |
eeb019b5 | 156 | names = [vl.get('id') for vl in template.iterfind('.//variablelist')] |
d9cfd694 | 157 | directive_groups = {name:collections.defaultdict(list) |
eeb019b5 | 158 | for name in names} |
d970bd6d | 159 | formatting = {} |
d9cfd694 | 160 | for page in xml_files: |
ccc9a4f9 | 161 | try: |
d970bd6d | 162 | _extract_directives(directive_groups, formatting, page) |
ccc9a4f9 ZJS |
163 | except Exception: |
164 | raise ValueError("failed to process " + page) | |
d9cfd694 | 165 | |
d970bd6d | 166 | return _make_page(template, directive_groups, formatting) |
d9cfd694 ZJS |
167 | |
168 | if __name__ == '__main__': | |
1a13e31d | 169 | with open(sys.argv[1], 'wb') as f: |
28223088 ZJS |
170 | template_path = sys.argv[2] |
171 | xml_files = sys.argv[3:] | |
172 | xml = make_page(template_path, xml_files) | |
173 | f.write(xml_print(xml)) |