2017-05-01 08:26:56 +08:00
|
|
|
#!/usr/bin/env python3
|
2020-11-09 12:23:58 +08:00
|
|
|
# SPDX-License-Identifier: LGPL-2.1-or-later
|
2012-07-16 23:19:39 +08:00
|
|
|
|
2013-01-15 11:17:49 +08:00
|
|
|
import collections
|
2013-03-08 02:04:17 +08:00
|
|
|
import re
|
2023-07-18 02:02:54 +08:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from xml_helper import tree, xml_parse, xml_print
|
2013-03-30 02:22:27 +08:00
|
|
|
|
2013-01-15 23:34:59 +08:00
|
|
|
MDASH = ' — ' if sys.version_info.major >= 3 else ' -- '
|
2013-01-15 11:17:49 +08:00
|
|
|
|
|
|
|
TEMPLATE = '''\
|
2020-06-13 22:44:58 +08:00
|
|
|
<refentry id="systemd.index">
|
2013-01-15 11:17:49 +08:00
|
|
|
|
|
|
|
<refentryinfo>
|
|
|
|
<title>systemd.index</title>
|
|
|
|
<productname>systemd</productname>
|
|
|
|
</refentryinfo>
|
|
|
|
|
|
|
|
<refmeta>
|
|
|
|
<refentrytitle>systemd.index</refentrytitle>
|
|
|
|
<manvolnum>7</manvolnum>
|
|
|
|
</refmeta>
|
|
|
|
|
|
|
|
<refnamediv>
|
|
|
|
<refname>systemd.index</refname>
|
|
|
|
<refpurpose>List all manpages from the systemd project</refpurpose>
|
|
|
|
</refnamediv>
|
|
|
|
</refentry>
|
|
|
|
'''
|
|
|
|
|
|
|
|
SUMMARY = '''\
|
|
|
|
<refsect1>
|
|
|
|
<title>See Also</title>
|
|
|
|
<para>
|
|
|
|
<citerefentry><refentrytitle>systemd.directives</refentrytitle><manvolnum>7</manvolnum></citerefentry>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para id='counts' />
|
|
|
|
</refsect1>
|
|
|
|
'''
|
|
|
|
|
|
|
|
COUNTS = '\
|
|
|
|
This index contains {count} entries, referring to {pages} individual manual pages.'
|
|
|
|
|
2013-03-30 02:22:27 +08:00
|
|
|
|
2013-03-08 02:04:17 +08:00
|
|
|
def check_id(page, t):
|
2023-07-18 02:02:54 +08:00
|
|
|
page_id = t.getroot().get('id')
|
2023-12-24 00:22:04 +08:00
|
|
|
if not re.search('/' + page_id + '[.]', page.translate(str.maketrans('@', '_'))):
|
2023-07-18 02:02:54 +08:00
|
|
|
raise ValueError(f"id='{page_id}' is not the same as page name '{page}'")
|
2013-03-08 02:04:17 +08:00
|
|
|
|
2013-01-15 11:17:49 +08:00
|
|
|
def make_index(pages):
|
|
|
|
index = collections.defaultdict(list)
|
|
|
|
for p in pages:
|
2013-03-30 02:22:27 +08:00
|
|
|
t = xml_parse(p)
|
2013-03-08 02:04:17 +08:00
|
|
|
check_id(p, t)
|
2012-07-16 23:39:26 +08:00
|
|
|
section = t.find('./refmeta/manvolnum').text
|
2013-01-15 11:17:49 +08:00
|
|
|
refname = t.find('./refnamediv/refname').text
|
2020-08-16 09:28:46 +08:00
|
|
|
purpose_text = ' '.join(t.find('./refnamediv/refpurpose').itertext())
|
|
|
|
purpose = ' '.join(purpose_text.split())
|
2012-07-16 23:19:39 +08:00
|
|
|
for f in t.findall('./refnamediv/refname'):
|
2013-01-15 11:17:49 +08:00
|
|
|
infos = (f.text, section, purpose, refname)
|
|
|
|
index[f.text[0].upper()].append(infos)
|
|
|
|
return index
|
2012-07-16 23:19:39 +08:00
|
|
|
|
2013-01-15 11:17:49 +08:00
|
|
|
def add_letter(template, letter, pages):
|
|
|
|
refsect1 = tree.SubElement(template, 'refsect1')
|
|
|
|
title = tree.SubElement(refsect1, 'title')
|
|
|
|
title.text = letter
|
|
|
|
para = tree.SubElement(refsect1, 'para')
|
|
|
|
for info in sorted(pages, key=lambda info: str.lower(info[0])):
|
2023-07-18 02:02:54 +08:00
|
|
|
refname, section, purpose, _realname = info
|
2012-07-16 23:19:39 +08:00
|
|
|
|
2013-01-15 11:17:49 +08:00
|
|
|
b = tree.SubElement(para, 'citerefentry')
|
|
|
|
c = tree.SubElement(b, 'refentrytitle')
|
|
|
|
c.text = refname
|
|
|
|
d = tree.SubElement(b, 'manvolnum')
|
|
|
|
d.text = section
|
2012-07-16 23:19:39 +08:00
|
|
|
|
2013-01-15 23:34:59 +08:00
|
|
|
b.tail = MDASH + purpose # + ' (' + p + ')'
|
2012-07-16 23:19:39 +08:00
|
|
|
|
2013-01-15 11:17:49 +08:00
|
|
|
tree.SubElement(para, 'sbr')
|
2012-07-16 23:19:39 +08:00
|
|
|
|
2013-01-15 11:17:49 +08:00
|
|
|
def add_summary(template, indexpages):
|
|
|
|
count = 0
|
|
|
|
pages = set()
|
|
|
|
for group in indexpages:
|
|
|
|
count += len(group)
|
|
|
|
for info in group:
|
2023-07-18 02:02:54 +08:00
|
|
|
_refname, section, _purpose, realname = info
|
2013-01-15 11:17:49 +08:00
|
|
|
pages.add((realname, section))
|
2012-07-16 23:19:39 +08:00
|
|
|
|
2013-01-15 11:17:49 +08:00
|
|
|
refsect1 = tree.fromstring(SUMMARY)
|
|
|
|
template.append(refsect1)
|
2012-07-16 23:19:39 +08:00
|
|
|
|
2013-01-15 11:17:49 +08:00
|
|
|
para = template.find(".//para[@id='counts']")
|
|
|
|
para.text = COUNTS.format(count=count, pages=len(pages))
|
2012-07-16 23:19:39 +08:00
|
|
|
|
2013-03-30 02:22:27 +08:00
|
|
|
def make_page(*xml_files):
|
2013-01-15 11:17:49 +08:00
|
|
|
template = tree.fromstring(TEMPLATE)
|
|
|
|
index = make_index(xml_files)
|
2012-07-16 23:19:39 +08:00
|
|
|
|
2013-01-15 11:17:49 +08:00
|
|
|
for letter in sorted(index):
|
|
|
|
add_letter(template, letter, index[letter])
|
2012-07-16 23:19:39 +08:00
|
|
|
|
2013-01-15 11:17:49 +08:00
|
|
|
add_summary(template, index.values())
|
2012-07-16 23:19:39 +08:00
|
|
|
|
2013-01-15 11:17:49 +08:00
|
|
|
return template
|
2012-07-17 00:10:18 +08:00
|
|
|
|
2013-01-15 11:17:49 +08:00
|
|
|
if __name__ == '__main__':
|
2023-07-18 02:02:54 +08:00
|
|
|
with open(sys.argv[1], 'wb') as file:
|
|
|
|
file.write(xml_print(make_page(*sys.argv[2:])))
|