From: Zbigniew Jędrzejewski-Szmek Date: Fri, 29 Mar 2013 18:22:27 +0000 (-0400) Subject: build-sys,man: use XML entities to substite strings X-Git-Tag: v201~148 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=1a13e31d275430ffba713c8a68ee7f22093c29e0 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. --- diff --git a/.gitignore b/.gitignore index c3bb81b1f..58a500021 100644 --- a/.gitignore +++ b/.gitignore @@ -139,6 +139,8 @@ *.lo *.o *.stamp +*.pyc +__pycache__/ *~ .deps/ .dirstamp diff --git a/Makefile.am b/Makefile.am index d5b319e2c..92bd54276 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 +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)) -XML_GLOB = $(wildcard $(top_srcdir)/man/*.xml) 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 '' && \ + echo '$(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 diff --git a/make-directive-index.py b/make-directive-index.py index 039efaa43..99e7bfaf2 100755 --- a/make-directive-index.py +++ b/make-directive-index.py @@ -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 = '''\ @@ -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:]))) diff --git a/make-man-index.py b/make-man-index.py index d9ab5cc75..74a47b821 100755 --- a/make-man-index.py +++ b/make-man-index.py @@ -19,14 +19,10 @@ # along with systemd; If not, see . 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:]))) diff --git a/make-man-rules.py b/make-man-rules.py index 46a586374..ad601f874 100644 --- a/make-man-rules.py +++ b/make-man-rules.py @@ -18,9 +18,9 @@ # along with systemd; If not, see . 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] diff --git a/man/.gitignore b/man/.gitignore index 3798c75f1..bf5eeab93 100644 --- a/man/.gitignore +++ b/man/.gitignore @@ -1,4 +1,4 @@ /systemd.directives.xml /systemd.index.xml /*.[13578] -/python-systemd/ +/custom-entities.ent diff --git a/man/systemd.unit.xml b/man/systemd.unit.xml index 2196e73bb..47c50315a 100644 --- a/man/systemd.unit.xml +++ b/man/systemd.unit.xml @@ -1,6 +1,9 @@ + "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" [ + +%entities; +]>