metaflow: Convert hex parsing to use new utility functions.
[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 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.
31 usage: %(argv0)s [OPTIONS] INPUT.XML
32 where INPUT.XML is a manpage in an OVS-specific XML format.
33
34 The following options are also available:
35   --version=VERSION           use VERSION to display on document footer
36   -h, --help                  display this help message\
37 """ % {'argv0': argv0}
38     sys.exit(0)
39
40 def manpage_to_nroff(xml_file, version=None):
41     doc = xml.dom.minidom.parse(xml_file).documentElement
42     d = date.fromtimestamp(os.stat(xml_file).st_mtime)
43
44     if version == None:
45         version = "UNKNOWN"
46     program = doc.attributes['program'].nodeValue
47     title = doc.attributes['title'].nodeValue
48     section = doc.attributes['section'].nodeValue
49
50     # Putting '\" p as the first line tells "man" that the manpage
51     # needs to be preprocessed by "pic".
52     s = r''''\" p
53 .\" -*- nroff -*-
54 .TH "%s" %s "%s" "Open vSwitch %s" "Open vSwitch Manual"
55 .fp 5 L CR              \\" Make fixed-width font available as \\fL.
56 .de TQ
57 .  br
58 .  ns
59 .  TP "\\$1"
60 ..
61 .de ST
62 .  PP
63 .  RS -0.15in
64 .  I "\\$1"
65 .  RE
66 ..
67 ''' % (textToNroff(program), textToNroff(section), textToNroff(title), textToNroff(version))
68
69     s += blockXmlToNroff(doc.childNodes) + "\n"
70
71     return s
72
73 def usage():
74     print """\
75 %(argv0)s: converts XML in a somewhat HTML-like format to nroff
76 usage: %(argv0)s [OPTIONS] XML
77 where XML is documentation in a somewhat HTML-like XML format.
78 The manpage, in nroff "man" format, is output on stdout.
79
80 The following options are also available:
81   --version=VERSION           use VERSION to display on document footer
82   -h, --help                  display this help message\
83 """ % {'argv0': argv0}
84     sys.exit(0)
85
86 if __name__ == "__main__":
87     try:
88         options, args = getopt.gnu_getopt(sys.argv[1:], 'hV',
89                                           ['version=', 'help'])
90     except getopt.GetoptError, geo:
91         sys.stderr.write("%s: %s\n" % (argv0, geo.msg))
92         sys.exit(1)
93
94     er_diagram = None
95     title = None
96     version = None
97     for key, value in options:
98         if key == '--version':
99             version = value
100         elif key in ['-h', '--help']:
101             usage()
102         else:
103             sys.exit(0)
104
105     if len(args) != 1:
106         sys.stderr.write("%s: exactly 1 non-option arguments required "
107                          "(use --help for help)\n" % argv0)
108         sys.exit(1)
109
110     try:
111         s = manpage_to_nroff(args[0], version)
112     except error.Error, e:
113         sys.stderr.write("%s: %s\n" % (argv0, e.msg))
114         sys.exit(1)
115     for line in s.splitlines():
116         line = line.strip()
117         if line:
118             print line
119
120
121 # Local variables:
122 # mode: python
123 # End: