]>
Commit | Line | Data |
---|---|---|
a4e3c495 BP |
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 | from datetime import date | |
18 | import getopt | |
19 | import os | |
20 | import sys | |
21 | import xml.dom.minidom | |
22 | ||
23 | from build.nroff import * | |
24 | ||
25 | argv0 = sys.argv[0] | |
26 | ||
27 | def usage(): | |
28 | print """\ | |
29 | %(argv0)s: XML to nroff converter | |
30 | Converts the XML format supplied as input into an nroff-formatted manpage. | |
b59de254 | 31 | usage: %(argv0)s [OPTIONS] INPUT.XML [VAR=VALUE]... |
a4e3c495 BP |
32 | where INPUT.XML is a manpage in an OVS-specific XML format. |
33 | ||
b59de254 BP |
34 | Each VAR, when enclosed by "@"s in the input, is replaced by its |
35 | corresponding VALUE, with characters &<>"' in VALUE escaped. | |
36 | ||
a4e3c495 BP |
37 | The following options are also available: |
38 | --version=VERSION use VERSION to display on document footer | |
39 | -h, --help display this help message\ | |
40 | """ % {'argv0': argv0} | |
41 | sys.exit(0) | |
42 | ||
b59de254 BP |
43 | def manpage_to_nroff(xml_file, subst, version=None): |
44 | f = open(xml_file) | |
45 | input = [] | |
46 | for line in f: | |
47 | for k, v in subst.iteritems(): | |
48 | line = line.replace(k, v) | |
49 | input += [line] | |
50 | doc = xml.dom.minidom.parseString(''.join(input)).documentElement | |
a4e3c495 BP |
51 | d = date.fromtimestamp(os.stat(xml_file).st_mtime) |
52 | ||
53 | if version == None: | |
54 | version = "UNKNOWN" | |
55 | program = doc.attributes['program'].nodeValue | |
56 | title = doc.attributes['title'].nodeValue | |
57 | section = doc.attributes['section'].nodeValue | |
58 | ||
59 | # Putting '\" p as the first line tells "man" that the manpage | |
60 | # needs to be preprocessed by "pic". | |
61 | s = r''''\" p | |
62 | .\" -*- nroff -*- | |
63 | .TH "%s" %s "%s" "Open vSwitch %s" "Open vSwitch Manual" | |
64 | .fp 5 L CR \\" Make fixed-width font available as \\fL. | |
65 | .de TQ | |
66 | . br | |
67 | . ns | |
68 | . TP "\\$1" | |
69 | .. | |
70 | .de ST | |
71 | . PP | |
72 | . RS -0.15in | |
73 | . I "\\$1" | |
74 | . RE | |
75 | .. | |
4d9d1d9e BP |
76 | ''' % (text_to_nroff(program), text_to_nroff(section), |
77 | text_to_nroff(title), text_to_nroff(version)) | |
a4e3c495 | 78 | |
4d9d1d9e | 79 | s += block_xml_to_nroff(doc.childNodes) + "\n" |
a4e3c495 BP |
80 | |
81 | return s | |
82 | ||
83 | def usage(): | |
84 | print """\ | |
85 | %(argv0)s: converts XML in a somewhat HTML-like format to nroff | |
86 | usage: %(argv0)s [OPTIONS] XML | |
87 | where XML is documentation in a somewhat HTML-like XML format. | |
88 | The manpage, in nroff "man" format, is output on stdout. | |
89 | ||
90 | The following options are also available: | |
91 | --version=VERSION use VERSION to display on document footer | |
92 | -h, --help display this help message\ | |
93 | """ % {'argv0': argv0} | |
94 | sys.exit(0) | |
95 | ||
96 | if __name__ == "__main__": | |
97 | try: | |
98 | options, args = getopt.gnu_getopt(sys.argv[1:], 'hV', | |
99 | ['version=', 'help']) | |
100 | except getopt.GetoptError, geo: | |
101 | sys.stderr.write("%s: %s\n" % (argv0, geo.msg)) | |
102 | sys.exit(1) | |
103 | ||
104 | er_diagram = None | |
105 | title = None | |
106 | version = None | |
107 | for key, value in options: | |
108 | if key == '--version': | |
109 | version = value | |
110 | elif key in ['-h', '--help']: | |
111 | usage() | |
112 | else: | |
113 | sys.exit(0) | |
114 | ||
b59de254 | 115 | if len(args) < 1: |
a4e3c495 BP |
116 | sys.stderr.write("%s: exactly 1 non-option arguments required " |
117 | "(use --help for help)\n" % argv0) | |
118 | sys.exit(1) | |
119 | ||
b59de254 BP |
120 | subst = {} |
121 | for s in args[1:]: | |
122 | var, value = s.split('=', 1) | |
123 | value = value.replace('&', '&') | |
124 | value = value.replace('<', '<') | |
125 | value = value.replace('>', '>') | |
126 | value = value.replace('"', '"') | |
127 | value = value.replace("'", ''') | |
128 | subst['@%s@' % var] = value | |
129 | ||
a4e3c495 | 130 | try: |
b59de254 | 131 | s = manpage_to_nroff(args[0], subst, version) |
a4e3c495 BP |
132 | except error.Error, e: |
133 | sys.stderr.write("%s: %s\n" % (argv0, e.msg)) | |
134 | sys.exit(1) | |
135 | for line in s.splitlines(): | |
136 | line = line.strip() | |
137 | if line: | |
138 | print line | |
139 | ||
140 | ||
141 | # Local variables: | |
142 | # mode: python | |
143 | # End: |