2019-09-20 08:30:42 -07:00
|
|
|
#! /usr/bin/python3
|
2015-02-19 11:08:53 -08:00
|
|
|
|
2016-01-12 10:37:48 -08:00
|
|
|
# Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015, 2016 Nicira, Inc.
|
2015-02-19 11:08:53 -08:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at:
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import getopt
|
|
|
|
import sys
|
|
|
|
import xml.dom.minidom
|
|
|
|
|
2023-08-23 16:29:00 +02:00
|
|
|
from ovs_build_helpers import nroff
|
2015-02-19 11:08:53 -08:00
|
|
|
|
|
|
|
argv0 = sys.argv[0]
|
|
|
|
|
2015-12-10 14:08:44 -05:00
|
|
|
|
2015-02-19 11:08:53 -08:00
|
|
|
def usage():
|
2017-01-05 18:01:08 -08:00
|
|
|
print("""\
|
2015-02-19 11:08:53 -08:00
|
|
|
%(argv0)s: XML to nroff converter
|
|
|
|
Converts the XML format supplied as input into an nroff-formatted manpage.
|
2015-06-16 08:22:46 -07:00
|
|
|
usage: %(argv0)s [OPTIONS] INPUT.XML [VAR=VALUE]...
|
2015-02-19 11:08:53 -08:00
|
|
|
where INPUT.XML is a manpage in an OVS-specific XML format.
|
|
|
|
|
2015-06-16 08:22:46 -07:00
|
|
|
Each VAR, when enclosed by "@"s in the input, is replaced by its
|
|
|
|
corresponding VALUE, with characters &<>"' in VALUE escaped.
|
|
|
|
|
2015-02-19 11:08:53 -08:00
|
|
|
The following options are also available:
|
2016-01-12 10:37:48 -08:00
|
|
|
-I, --include=DIR search DIR for include files (default: .)
|
2015-02-19 11:08:53 -08:00
|
|
|
--version=VERSION use VERSION to display on document footer
|
|
|
|
-h, --help display this help message\
|
2017-01-05 18:01:08 -08:00
|
|
|
""" % {'argv0': argv0})
|
2015-02-19 11:08:53 -08:00
|
|
|
sys.exit(0)
|
|
|
|
|
2015-12-10 14:08:44 -05:00
|
|
|
|
2016-01-12 10:37:48 -08:00
|
|
|
def manpage_to_nroff(xml_file, subst, include_path, version=None):
|
2015-12-10 14:22:58 -05:00
|
|
|
with open(xml_file) as f:
|
|
|
|
content = f.read()
|
2017-01-05 18:01:08 -08:00
|
|
|
for k, v in subst.items():
|
2015-12-10 14:22:58 -05:00
|
|
|
content = content.replace(k, v)
|
|
|
|
doc = xml.dom.minidom.parseString(content).documentElement
|
2015-02-19 11:08:53 -08:00
|
|
|
|
2016-01-12 09:35:06 +08:00
|
|
|
xi_nodes = doc.getElementsByTagName("xi:include")
|
|
|
|
for node in xi_nodes:
|
2016-01-12 10:37:48 -08:00
|
|
|
fn = node.getAttribute("href")
|
|
|
|
content = None
|
|
|
|
for dir in include_path:
|
|
|
|
try:
|
|
|
|
with open("%s/%s" % (dir, fn)) as xi_f:
|
|
|
|
content = xi_f.read()
|
|
|
|
except IOError:
|
|
|
|
pass
|
|
|
|
if not content:
|
|
|
|
sys.stderr.write("%s: could not open include file %s\n"
|
|
|
|
% (argv0, fn))
|
|
|
|
sys.exit(1)
|
2017-01-05 18:01:08 -08:00
|
|
|
for k, v in subst.items():
|
2016-01-12 09:35:06 +08:00
|
|
|
content = content.replace(k, v)
|
|
|
|
xi_doc = xml.dom.minidom.parseString(content).documentElement
|
|
|
|
doc.replaceChild(xi_doc, node)
|
|
|
|
|
2015-12-10 14:08:44 -05:00
|
|
|
if version is None:
|
2015-02-19 11:08:53 -08:00
|
|
|
version = "UNKNOWN"
|
|
|
|
program = doc.attributes['program'].nodeValue
|
|
|
|
title = doc.attributes['title'].nodeValue
|
|
|
|
section = doc.attributes['section'].nodeValue
|
|
|
|
|
|
|
|
# Putting '\" p as the first line tells "man" that the manpage
|
|
|
|
# needs to be preprocessed by "pic".
|
|
|
|
s = r''''\" p
|
|
|
|
.\" -*- nroff -*-
|
|
|
|
.TH "%s" %s "%s" "Open vSwitch %s" "Open vSwitch Manual"
|
|
|
|
.fp 5 L CR \\" Make fixed-width font available as \\fL.
|
|
|
|
.de TQ
|
|
|
|
. br
|
|
|
|
. ns
|
|
|
|
. TP "\\$1"
|
|
|
|
..
|
|
|
|
.de ST
|
|
|
|
. PP
|
|
|
|
. RS -0.15in
|
|
|
|
. I "\\$1"
|
|
|
|
. RE
|
|
|
|
..
|
2023-08-23 16:29:00 +02:00
|
|
|
''' % (nroff.text_to_nroff(program), nroff.text_to_nroff(section),
|
|
|
|
nroff.text_to_nroff(title), nroff.text_to_nroff(version))
|
2015-02-19 11:08:53 -08:00
|
|
|
|
2023-08-23 16:29:00 +02:00
|
|
|
s += nroff.block_xml_to_nroff(doc.childNodes) + "\n"
|
2015-02-19 11:08:53 -08:00
|
|
|
|
|
|
|
return s
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
try:
|
2016-01-12 10:37:48 -08:00
|
|
|
options, args = getopt.gnu_getopt(sys.argv[1:], 'hVI:',
|
|
|
|
['version=', 'help', 'include='])
|
2017-01-05 18:01:08 -08:00
|
|
|
except getopt.GetoptError as geo:
|
2015-02-19 11:08:53 -08:00
|
|
|
sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
er_diagram = None
|
|
|
|
title = None
|
|
|
|
version = None
|
2016-01-12 10:37:48 -08:00
|
|
|
include_path = []
|
2015-02-19 11:08:53 -08:00
|
|
|
for key, value in options:
|
|
|
|
if key == '--version':
|
|
|
|
version = value
|
|
|
|
elif key in ['-h', '--help']:
|
|
|
|
usage()
|
2016-01-12 10:37:48 -08:00
|
|
|
elif key in ['-I', '--include']:
|
|
|
|
include_path.append(value)
|
2015-02-19 11:08:53 -08:00
|
|
|
else:
|
|
|
|
sys.exit(0)
|
2016-01-12 10:37:48 -08:00
|
|
|
if not include_path:
|
|
|
|
include_path = ['.']
|
2015-02-19 11:08:53 -08:00
|
|
|
|
2015-06-16 08:22:46 -07:00
|
|
|
if len(args) < 1:
|
2015-02-19 11:08:53 -08:00
|
|
|
sys.stderr.write("%s: exactly 1 non-option arguments required "
|
|
|
|
"(use --help for help)\n" % argv0)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2015-06-16 08:22:46 -07:00
|
|
|
subst = {}
|
|
|
|
for s in args[1:]:
|
|
|
|
var, value = s.split('=', 1)
|
|
|
|
value = value.replace('&', '&')
|
|
|
|
value = value.replace('<', '<')
|
|
|
|
value = value.replace('>', '>')
|
|
|
|
value = value.replace('"', '"')
|
|
|
|
value = value.replace("'", ''')
|
|
|
|
subst['@%s@' % var] = value
|
|
|
|
|
2015-02-19 11:08:53 -08:00
|
|
|
try:
|
2016-01-12 10:37:48 -08:00
|
|
|
s = manpage_to_nroff(args[0], subst, include_path, version)
|
2023-08-23 16:29:00 +02:00
|
|
|
except nroff.error.Error as e:
|
2015-02-19 11:08:53 -08:00
|
|
|
sys.stderr.write("%s: %s\n" % (argv0, e.msg))
|
|
|
|
sys.exit(1)
|
|
|
|
for line in s.splitlines():
|
|
|
|
line = line.strip()
|
|
|
|
if line:
|
2017-01-05 18:01:08 -08:00
|
|
|
print(line)
|
2015-02-19 11:08:53 -08:00
|
|
|
|
|
|
|
|
|
|
|
# Local variables:
|
|
|
|
# mode: python
|
|
|
|
# End:
|