2017-05-01 03:26:56 +03:00
#!/usr/bin/env python3
2020-11-09 07:23:58 +03:00
# SPDX-License-Identifier: LGPL-2.1-or-later
2012-07-16 19:19:39 +04:00
2013-01-15 07:17:49 +04:00
import collections
import sys
2013-03-07 22:04:17 +04:00
import re
2017-07-03 03:26:32 +03:00
from xml_helper import xml_parse , xml_print , tree
2013-03-29 22:22:27 +04:00
2013-01-15 19:34:59 +04:00
MDASH = ' — ' if sys . version_info . major > = 3 else ' -- '
2013-01-15 07:17:49 +04:00
TEMPLATE = ''' \
2020-06-13 17:44:58 +03:00
< refentry id = " systemd.index " >
2013-01-15 07:17:49 +04: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-29 22:22:27 +04:00
2013-03-07 22:04:17 +04:00
def check_id ( page , t ) :
id = t . getroot ( ) . get ( ' id ' )
if not re . search ( ' / ' + id + ' [.] ' , page ) :
raise ValueError ( " id= ' {} ' is not the same as page name ' {} ' " . format ( id , page ) )
2013-01-15 07:17:49 +04:00
def make_index ( pages ) :
index = collections . defaultdict ( list )
for p in pages :
2013-03-29 22:22:27 +04:00
t = xml_parse ( p )
2013-03-07 22:04:17 +04:00
check_id ( p , t )
2012-07-16 19:39:26 +04:00
section = t . find ( ' ./refmeta/manvolnum ' ) . text
2013-01-15 07:17:49 +04:00
refname = t . find ( ' ./refnamediv/refname ' ) . text
2020-08-16 04:28:46 +03:00
purpose_text = ' ' . join ( t . find ( ' ./refnamediv/refpurpose ' ) . itertext ( ) )
purpose = ' ' . join ( purpose_text . split ( ) )
2012-07-16 19:19:39 +04:00
for f in t . findall ( ' ./refnamediv/refname ' ) :
2013-01-15 07:17:49 +04:00
infos = ( f . text , section , purpose , refname )
index [ f . text [ 0 ] . upper ( ) ] . append ( infos )
return index
2012-07-16 19:19:39 +04:00
2013-01-15 07:17:49 +04: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 ] ) ) :
refname , section , purpose , realname = info
2012-07-16 19:19:39 +04:00
2013-01-15 07:17:49 +04: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 19:19:39 +04:00
2013-01-15 19:34:59 +04:00
b . tail = MDASH + purpose # + ' (' + p + ')'
2012-07-16 19:19:39 +04:00
2013-01-15 07:17:49 +04:00
tree . SubElement ( para , ' sbr ' )
2012-07-16 19:19:39 +04:00
2013-01-15 07:17:49 +04:00
def add_summary ( template , indexpages ) :
count = 0
pages = set ( )
for group in indexpages :
count + = len ( group )
for info in group :
refname , section , purpose , realname = info
pages . add ( ( realname , section ) )
2012-07-16 19:19:39 +04:00
2013-01-15 07:17:49 +04:00
refsect1 = tree . fromstring ( SUMMARY )
template . append ( refsect1 )
2012-07-16 19:19:39 +04:00
2013-01-15 07:17:49 +04:00
para = template . find ( " .//para[@id= ' counts ' ] " )
para . text = COUNTS . format ( count = count , pages = len ( pages ) )
2012-07-16 19:19:39 +04:00
2013-03-29 22:22:27 +04:00
def make_page ( * xml_files ) :
2013-01-15 07:17:49 +04:00
template = tree . fromstring ( TEMPLATE )
index = make_index ( xml_files )
2012-07-16 19:19:39 +04:00
2013-01-15 07:17:49 +04:00
for letter in sorted ( index ) :
add_letter ( template , letter , index [ letter ] )
2012-07-16 19:19:39 +04:00
2013-01-15 07:17:49 +04:00
add_summary ( template , index . values ( ) )
2012-07-16 19:19:39 +04:00
2013-01-15 07:17:49 +04:00
return template
2012-07-16 20:10:18 +04:00
2013-01-15 07:17:49 +04:00
if __name__ == ' __main__ ' :
2013-03-29 22:22:27 +04:00
with open ( sys . argv [ 1 ] , ' wb ' ) as f :
f . write ( xml_print ( make_page ( * sys . argv [ 2 : ] ) ) )