xml2nroff: Add support for variable substitutions.
authorBen Pfaff <blp@nicira.com>
Tue, 16 Jun 2015 15:22:46 +0000 (08:22 -0700)
committerBen Pfaff <blp@nicira.com>
Tue, 16 Jun 2015 15:22:46 +0000 (08:22 -0700)
This allows XML-generated manpages in the source tree to include correct
directory names for the local configuration, instead of just the plain
nroff ones.

Signed-off-by: Ben Pfaff <blp@nicira.com>
Acked-by: Alex Wang <alexw@nicira.com>
build-aux/xml2nroff
ovn/automake.mk

index 8dc9d4f..1df195e 100755 (executable)
@@ -28,17 +28,26 @@ def usage():
     print """\
 %(argv0)s: XML to nroff converter
 Converts the XML format supplied as input into an nroff-formatted manpage.
-usage: %(argv0)s [OPTIONS] INPUT.XML
+usage: %(argv0)s [OPTIONS] INPUT.XML [VAR=VALUE]...
 where INPUT.XML is a manpage in an OVS-specific XML format.
 
+Each VAR, when enclosed by "@"s in the input, is replaced by its
+corresponding VALUE, with characters &<>"' in VALUE escaped.
+
 The following options are also available:
   --version=VERSION           use VERSION to display on document footer
   -h, --help                  display this help message\
 """ % {'argv0': argv0}
     sys.exit(0)
 
-def manpage_to_nroff(xml_file, version=None):
-    doc = xml.dom.minidom.parse(xml_file).documentElement
+def manpage_to_nroff(xml_file, subst, version=None):
+    f = open(xml_file)
+    input = []
+    for line in f:
+        for k, v in subst.iteritems():
+            line = line.replace(k, v)
+        input += [line]
+    doc = xml.dom.minidom.parseString(''.join(input)).documentElement
     d = date.fromtimestamp(os.stat(xml_file).st_mtime)
 
     if version == None:
@@ -102,13 +111,23 @@ if __name__ == "__main__":
         else:
             sys.exit(0)
 
-    if len(args) != 1:
+    if len(args) < 1:
         sys.stderr.write("%s: exactly 1 non-option arguments required "
                          "(use --help for help)\n" % argv0)
         sys.exit(1)
 
+    subst = {}
+    for s in args[1:]:
+        var, value = s.split('=', 1)
+        value = value.replace('&', '&amp;')
+        value = value.replace('<', '&lt;')
+        value = value.replace('>', '&gt;')
+        value = value.replace('"', '&quot;')
+        value = value.replace("'", '&apos;')
+        subst['@%s@' % var] = value
+
     try:
-        s = manpage_to_nroff(args[0], version)
+        s = manpage_to_nroff(args[0], subst, version)
     except error.Error, e:
         sys.stderr.write("%s: %s\n" % (argv0, e.msg))
         sys.exit(1)
index 1ebaa55..7eb9c1d 100644 (file)
@@ -71,8 +71,21 @@ EXTRA_DIST += ovn/ovn-architecture.7.xml ovn/ovn-nbctl.8.xml
 
 SUFFIXES += .xml
 %: %.xml
-       $(AM_V_GEN)$(run_python) $(srcdir)/build-aux/xml2nroff \
-               --version=$(VERSION) $< > $@.tmp && mv $@.tmp $@
+       $(AM_V_GEN)$(run_python) $(srcdir)/build-aux/xml2nroff $< > $@.tmp \
+               --version=$(VERSION) \
+               PKIDIR='$(PKIDIR)' \
+               LOGDIR='$(LOGDIR)' \
+               DBDIR='$(DBDIR)' \
+               PERL='$(PERL)' \
+               PYTHON='$(PYTHON)' \
+               RUNDIR='$(RUNDIR)' \
+               VERSION='$(VERSION)' \
+               localstatedir='$(localstatedir)' \
+               pkgdatadir='$(pkgdatadir)' \
+               sysconfdir='$(sysconfdir)' \
+               bindir='$(bindir)' \
+               sbindir='$(sbindir)'
+       $(AM_v_at)mv $@.tmp $@
 
 EXTRA_DIST += \
        ovn/TODO \