forked from openvswitch/ovs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xml2nroff
executable file
·143 lines (122 loc) · 4.11 KB
/
xml2nroff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#! /usr/bin/python
# Copyright (c) 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
#
# 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.
from datetime import date
import getopt
import os
import sys
import xml.dom.minidom
from build.nroff import *
argv0 = sys.argv[0]
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 [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, 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:
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
..
''' % (text_to_nroff(program), text_to_nroff(section),
text_to_nroff(title), text_to_nroff(version))
s += block_xml_to_nroff(doc.childNodes) + "\n"
return s
def usage():
print """\
%(argv0)s: converts XML in a somewhat HTML-like format to nroff
usage: %(argv0)s [OPTIONS] XML
where XML is documentation in a somewhat HTML-like XML format.
The manpage, in nroff "man" format, is output on stdout.
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)
if __name__ == "__main__":
try:
options, args = getopt.gnu_getopt(sys.argv[1:], 'hV',
['version=', 'help'])
except getopt.GetoptError, geo:
sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
sys.exit(1)
er_diagram = None
title = None
version = None
for key, value in options:
if key == '--version':
version = value
elif key in ['-h', '--help']:
usage()
else:
sys.exit(0)
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('&', '&')
value = value.replace('<', '<')
value = value.replace('>', '>')
value = value.replace('"', '"')
value = value.replace("'", ''')
subst['@%s@' % var] = value
try:
s = manpage_to_nroff(args[0], subst, version)
except error.Error, e:
sys.stderr.write("%s: %s\n" % (argv0, e.msg))
sys.exit(1)
for line in s.splitlines():
line = line.strip()
if line:
print line
# Local variables:
# mode: python
# End: