2013-01-15 07:17:49 +04:00
# -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
2012-09-13 06:05:28 +04:00
#
# This file is part of systemd.
#
# Copyright 2012 Lennart Poettering
2013-01-15 07:17:49 +04:00
# Copyright 2013 Zbigniew Jędrzejewski-Szmek
2012-09-13 06:05:28 +04:00
#
# systemd is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# systemd is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with systemd; If not, see <http://www.gnu.org/licenses/>.
2012-07-16 19:19:39 +04:00
2013-01-15 07:17:49 +04:00
import collections
2013-03-08 05:13:40 +04:00
try :
from lxml import etree as tree
PRETTY = dict ( pretty_print = True )
except ImportError :
import xml . etree . ElementTree as tree
PRETTY = { }
2013-01-15 07:17:49 +04:00
import sys
2013-03-07 22:04:17 +04:00
import re
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 = ''' \
2013-02-03 07:47:47 +04:00
< refentry id = " systemd.index " conditional = " HAVE_PYTHON " >
2013-01-15 07:17:49 +04:00
< refentryinfo >
< title > systemd . index < / title >
< productname > systemd < / productname >
< authorgroup >
< author >
< contrib > Developer < / contrib >
< firstname > Lennart < / firstname >
< surname > Poettering < / surname >
< email > lennart @poettering.net < / email >
< / author >
< / authorgroup >
< / 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-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 :
t = tree . 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
2012-07-16 23:27:06 +04:00
purpose = ' ' . join ( t . find ( ' ./refnamediv/refpurpose ' ) . 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-01-15 07:17:49 +04:00
def make_page ( xml_files ) :
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-08 05:13:40 +04:00
tree . dump ( make_page ( sys . argv [ 1 : ] ) , * * PRETTY )