ovn-nbctl: add db commands help and manpage
[cascardo/ovs.git] / build-aux / xml2nroff
1 #! /usr/bin/python
2
3 # Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import getopt
18 import sys
19 import xml.dom.minidom
20
21 import build.nroff
22
23 argv0 = sys.argv[0]
24
25
26 def usage():
27     print """\
28 %(argv0)s: XML to nroff converter
29 Converts the XML format supplied as input into an nroff-formatted manpage.
30 usage: %(argv0)s [OPTIONS] INPUT.XML [VAR=VALUE]...
31 where INPUT.XML is a manpage in an OVS-specific XML format.
32
33 Each VAR, when enclosed by "@"s in the input, is replaced by its
34 corresponding VALUE, with characters &<>"' in VALUE escaped.
35
36 The following options are also available:
37   --version=VERSION           use VERSION to display on document footer
38   -h, --help                  display this help message\
39 """ % {'argv0': argv0}
40     sys.exit(0)
41
42
43 def manpage_to_nroff(xml_file, subst, version=None):
44     with open(xml_file) as f:
45         content = f.read()
46     for k, v in subst.iteritems():
47         content = content.replace(k, v)
48     doc = xml.dom.minidom.parseString(content).documentElement
49
50     xi_nodes = doc.getElementsByTagName("xi:include")
51     for node in xi_nodes:
52         with open(node.getAttribute("href")) as xi_f:
53             content = xi_f.read()
54         for k, v in subst.iteritems():
55             content = content.replace(k, v)
56         xi_doc = xml.dom.minidom.parseString(content).documentElement
57         doc.replaceChild(xi_doc, node)
58
59     if version is None:
60         version = "UNKNOWN"
61     program = doc.attributes['program'].nodeValue
62     title = doc.attributes['title'].nodeValue
63     section = doc.attributes['section'].nodeValue
64
65     # Putting '\" p as the first line tells "man" that the manpage
66     # needs to be preprocessed by "pic".
67     s = r''''\" p
68 .\" -*- nroff -*-
69 .TH "%s" %s "%s" "Open vSwitch %s" "Open vSwitch Manual"
70 .fp 5 L CR              \\" Make fixed-width font available as \\fL.
71 .de TQ
72 .  br
73 .  ns
74 .  TP "\\$1"
75 ..
76 .de ST
77 .  PP
78 .  RS -0.15in
79 .  I "\\$1"
80 .  RE
81 ..
82 ''' % (build.nroff.text_to_nroff(program), build.nroff.text_to_nroff(section),
83        build.nroff.text_to_nroff(title), build.nroff.text_to_nroff(version))
84
85     s += build.nroff.block_xml_to_nroff(doc.childNodes) + "\n"
86
87     return s
88
89
90 if __name__ == "__main__":
91     try:
92         options, args = getopt.gnu_getopt(sys.argv[1:], 'hV',
93                                           ['version=', 'help'])
94     except getopt.GetoptError, geo:
95         sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
96         sys.exit(1)
97
98     er_diagram = None
99     title = None
100     version = None
101     for key, value in options:
102         if key == '--version':
103             version = value
104         elif key in ['-h', '--help']:
105             usage()
106         else:
107             sys.exit(0)
108
109     if len(args) < 1:
110         sys.stderr.write("%s: exactly 1 non-option arguments required "
111                          "(use --help for help)\n" % argv0)
112         sys.exit(1)
113
114     subst = {}
115     for s in args[1:]:
116         var, value = s.split('=', 1)
117         value = value.replace('&', '&amp;')
118         value = value.replace('<', '&lt;')
119         value = value.replace('>', '&gt;')
120         value = value.replace('"', '&quot;')
121         value = value.replace("'", '&apos;')
122         subst['@%s@' % var] = value
123
124     try:
125         s = manpage_to_nroff(args[0], subst, version)
126     except build.nroff.error.Error, e:
127         sys.stderr.write("%s: %s\n" % (argv0, e.msg))
128         sys.exit(1)
129     for line in s.splitlines():
130         line = line.strip()
131         if line:
132             print line
133
134
135 # Local variables:
136 # mode: python
137 # End: