build-sys,man: use XML entities to substite strings

This makes it easier to add substitutions to man pages,
avoiding the separate transformation step.

mkdir -p's are removed from the rule, because xsltproc will
will create directories on it's own.

All in all, two or three forks per man page are avoided,
which should make things marginally faster.

Unfortunately python parsers must too be tweaked to handle
entities. This isn't particularly easy: with lxml a custom
Resolver can be used, but the stdlib etree doesn't support
external entities *at all*. So when running without lxml,
the entities are just removed. Right now it doesn't matter,
since the entities are not indexed anyway. But I intend to
add indexing of filenames in the near future, and then the
index generated without lxml might be missing a few lines.
Oh well.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2013-03-29 14:22:27 -04:00
parent 4e7b3c20e0
commit 1a13e31d27
8 changed files with 92 additions and 42 deletions

2
.gitignore vendored
View File

@ -139,6 +139,8 @@
*.lo
*.o
*.stamp
*.pyc
__pycache__/
*~
.deps/
.dirstamp

View File

@ -97,6 +97,7 @@ rootbindir=$(rootprefix)/bin
rootlibexecdir=$(rootprefix)/lib/systemd
CLEANFILES = $(BUILT_SOURCES)
DISTCLEANFILES =
EXTRA_DIST =
BUILT_SOURCES =
INSTALL_EXEC_HOOKS =
@ -556,9 +557,10 @@ noinst_DATA += \
CLEANFILES += \
man/index.html
NON_INDEX_XML_FILES = $(filter-out man/systemd.index.xml,$(XML_FILES))
XML_GLOB = $(wildcard $(top_srcdir)/man/*.xml)
NON_INDEX_XML_FILES = $(filter-out man/systemd.index.xml,$(XML_FILES))
SOURCE_XML_FILES = $(filter-out man/systemd.directives.xml,$(NON_INDEX_XML_FILES))
update-man-list: make-man-rules.py $(XML_GLOB)
$(AM_V_GEN)$(PYTHON) $^ > $(top_srcdir)/Makefile-man.tmp
$(AM_V_at)mv $(top_srcdir)/Makefile-man.tmp $(top_srcdir)/Makefile-man.am
@ -566,11 +568,11 @@ update-man-list: make-man-rules.py $(XML_GLOB)
man/systemd.index.xml: make-man-index.py $(NON_INDEX_XML_FILES)
$(AM_V_at)$(MKDIR_P) $(dir $@)
$(AM_V_GEN)$(PYTHON) $^ > $@
$(AM_V_GEN)$(PYTHON) $< $@ $(filter-out $<,$^)
man/systemd.directives.xml: make-directive-index.py $(filter-out man/systemd.directives.xml,$(NON_INDEX_XML_FILES))
man/systemd.directives.xml: make-directive-index.py $(SOURCE_XML_FILES)
$(AM_V_at)$(MKDIR_P) $(dir $@)
$(AM_V_GEN)$(PYTHON) $^ > $@
$(AM_V_GEN)$(PYTHON) $< $@ $(filter-out $<,$^)
EXTRA_DIST += \
man/systemd.index.xml \
@ -591,7 +593,8 @@ EXTRA_DIST += \
$(HTML_ALIAS) \
$(dist_MANS) \
make-man-index.py \
make-directive-index.py
make-directive-index.py \
xml_helper.py
# ------------------------------------------------------------------------------
noinst_LTLIBRARIES += \
@ -3819,38 +3822,46 @@ CLEANFILES += \
# ------------------------------------------------------------------------------
if ENABLE_MANPAGES
man/custom-entities.ent: Makefile
$(AM_V_GEN)$(MKDIR_P) $(dir $@)
$(AM_V_GEN)(echo '<?xml version="1.0" encoding="utf-8" ?>' && \
echo '$(subst '|,<!ENTITY ,$(subst =, ",$(subst |',">,$(substitutions))))') \
> $@ # '
DISTCLEANFILES += \
man/custom-entities.ent
XSLTPROC_FLAGS = \
--nonet \
--stringparam man.output.quietly 1 \
--stringparam funcsynopsis.style ansi \
--stringparam man.authors.section.enabled 0 \
--stringparam man.copyright.section.enabled 0 \
--stringparam systemd.version $(VERSION)
--stringparam systemd.version $(VERSION) \
--path '$(builddir)/man:$(srcdir)/man'
XSLTPROC_PROCESS_MAN = \
$(AM_V_XSLT)$(MKDIR_P) $(dir $@) && \
$(XSLTPROC) -o $@ $(XSLTPROC_FLAGS) $(srcdir)/man/custom-man.xsl $<
$(AM_V_XSLT)$(XSLTPROC) -o $@ $(XSLTPROC_FLAGS) $(srcdir)/man/custom-man.xsl $<
XSLTPROC_PROCESS_HTML = \
$(AM_V_XSLT)$(MKDIR_P) $(dir $@) && \
$(XSLTPROC) -o $@ $(XSLTPROC_FLAGS) $(srcdir)/man/custom-html.xsl $<
$(AM_V_XSLT)$(XSLTPROC) -o $@ $(XSLTPROC_FLAGS) $(srcdir)/man/custom-html.xsl $<
man/%.1: man/%.xml man/custom-man.xsl
man/%.1: man/%.xml man/custom-man.xsl man/custom-entities.ent
$(XSLTPROC_PROCESS_MAN)
man/%.3: man/%.xml man/custom-man.xsl
man/%.3: man/%.xml man/custom-man.xsl man/custom-entities.ent
$(XSLTPROC_PROCESS_MAN)
man/%.5: man/%.xml man/custom-man.xsl
man/%.5: man/%.xml man/custom-man.xsl man/custom-entities.ent
$(XSLTPROC_PROCESS_MAN)
man/%.7: man/%.xml man/custom-man.xsl
man/%.7: man/%.xml man/custom-man.xsl man/custom-entities.ent
$(XSLTPROC_PROCESS_MAN)
man/%.8: man/%.xml man/custom-man.xsl
man/%.8: man/%.xml man/custom-man.xsl man/custom-entities.ent
$(XSLTPROC_PROCESS_MAN)
man/%.html: man/%.xml man/custom-html.xsl
man/%.html: man/%.xml man/custom-html.xsl man/custom-entities.ent
$(XSLTPROC_PROCESS_HTML)
define html-alias

View File

@ -19,13 +19,8 @@
import sys
import collections
try:
from lxml import etree as tree
PRETTY = dict(pretty_print=True)
except ImportError:
import xml.etree.ElementTree as tree
PRETTY = {}
import re
from xml_helper import *
TEMPLATE = '''\
<refentry id="systemd.directives" conditional="HAVE_PYTHON">
@ -173,7 +168,7 @@ referring to {pages} individual manual pages.
'''
def _extract_directives(directive_groups, formatting, page):
t = tree.parse(page)
t = xml_parse(page)
section = t.find('./refmeta/manvolnum').text
pagename = t.find('./refmeta/refentrytitle').text
@ -282,4 +277,5 @@ def make_page(*xml_files):
return _make_page(template, directive_groups, formatting)
if __name__ == '__main__':
tree.dump(make_page(*sys.argv[1:]), **PRETTY)
with open(sys.argv[1], 'wb') as f:
f.write(xml_print(make_page(*sys.argv[2:])))

View File

@ -19,14 +19,10 @@
# along with systemd; If not, see <http://www.gnu.org/licenses/>.
import collections
try:
from lxml import etree as tree
PRETTY = dict(pretty_print=True)
except ImportError:
import xml.etree.ElementTree as tree
PRETTY = {}
import sys
import re
from xml_helper import *
MDASH = '' if sys.version_info.major >= 3 else ' -- '
TEMPLATE = '''\
@ -72,6 +68,7 @@ SUMMARY = '''\
COUNTS = '\
This index contains {count} entries, referring to {pages} individual manual pages.'
def check_id(page, t):
id = t.getroot().get('id')
if not re.search('/' + id + '[.]', page):
@ -80,7 +77,7 @@ def check_id(page, t):
def make_index(pages):
index = collections.defaultdict(list)
for p in pages:
t = tree.parse(p)
t = xml_parse(p)
check_id(p, t)
section = t.find('./refmeta/manvolnum').text
refname = t.find('./refnamediv/refname').text
@ -123,7 +120,7 @@ def add_summary(template, indexpages):
para = template.find(".//para[@id='counts']")
para.text = COUNTS.format(count=count, pages=len(pages))
def make_page(xml_files):
def make_page(*xml_files):
template = tree.fromstring(TEMPLATE)
index = make_index(xml_files)
@ -135,4 +132,5 @@ def make_page(xml_files):
return template
if __name__ == '__main__':
tree.dump(make_page(sys.argv[1:]), **PRETTY)
with open(sys.argv[1], 'wb') as f:
f.write(xml_print(make_page(*sys.argv[2:])))

View File

@ -18,9 +18,9 @@
# along with systemd; If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import xml.etree.ElementTree as tree
import collections
import sys
from xml_helper import *
SECTION = '''\
MANPAGES += \\
@ -54,7 +54,7 @@ def man(page, number):
return 'man/{}.{}'.format(page, number)
def add_rules(rules, name):
xml = tree.parse(name)
xml = xml_parse(name)
# print('parsing {}'.format(name), file=sys.stderr)
conditional = xml.getroot().get('conditional') or ''
rulegroup = rules[conditional]

2
man/.gitignore vendored
View File

@ -1,4 +1,4 @@
/systemd.directives.xml
/systemd.index.xml
/*.[13578]
/python-systemd/
/custom-entities.ent

View File

@ -1,6 +1,9 @@
<?xml version='1.0'?> <!--*-nxml-*-->
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [
<!ENTITY % entities SYSTEM "custom-entities.ent" >
%entities;
]>
<!--
This file is part of systemd.
@ -278,7 +281,7 @@
<entry>Generated units</entry>
</row>
<row>
<entry><filename>@SYSTEM_CONFIG_UNIT_PATH@</filename></entry>
<entry><filename>&SYSTEM_CONFIG_UNIT_PATH;</filename></entry>
<entry morerows='1'>Local configuration</entry>
</row>
<row>
@ -297,7 +300,7 @@
<entry>Units for local packages</entry>
</row>
<row>
<entry><filename>@systemunitdir@</filename></entry>
<entry><filename>&systemunitdir;</filename></entry>
<entry>Systemd package configuration</entry>
</row>
<row>
@ -335,7 +338,7 @@
<entry>Generated units</entry>
</row>
<row>
<entry><filename>@USER_CONFIG_UNIT_PATH@</filename></entry>
<entry><filename>&USER_CONFIG_UNIT_PATH;</filename></entry>
<entry morerows='1'>Local configuration</entry>
</row>
<row>
@ -357,7 +360,7 @@
<entry><filename>/usr/local/share/systemd/user</filename></entry>
</row>
<row>
<entry><filename>@userunitdir@</filename></entry>
<entry><filename>&userunitdir;</filename></entry>
<entry>Systemd package configuration</entry>
</row>
<row>

40
xml_helper.py Normal file
View File

@ -0,0 +1,40 @@
# -*- Mode: python; coding: utf-8; indent-tabs-mode: nil -*- */
#
# This file is part of systemd.
#
# Copyright 2012-2013 Zbigniew Jędrzejewski-Szmek
#
# 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/>.
try:
from lxml import etree as tree
class CustomResolver(tree.Resolver):
def resolve(self, url, id, context):
if 'custom-entities.ent' in url:
return self.resolve_filename('man/custom-entities.ent', context)
_parser = tree.XMLParser()
_parser.resolvers.add(CustomResolver())
xml_parse = lambda page: tree.parse(page, _parser)
xml_print = lambda xml: tree.tostring(xml, pretty_print=True)
except ImportError:
import xml.etree.ElementTree as tree
import re as _re
import io as _io
def xml_parse(page):
s = _re.sub(b'&[a-zA-Z0-9_]+;', b'', open(page, 'rb').read())
return tree.parse(_io.BytesIO(s))
xml_print = tree.tostring