1 # Copyright (c) 2010, 2011, 2012, 2015 Nicira, Inc.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
17 from ovs.db import error
19 def textToNroff(s, font=r'\fR'):
23 # In Roman type, let -- in XML be \- in nroff. That gives us a way to
24 # write minus signs, which is important in some places in manpages.
26 # Bold in nroff usually represents literal text, where there's no
27 # distinction between hyphens and minus sign. The convention in nroff
28 # appears to be to use a minus sign in such cases, so we follow that
31 # Finally, we always output - as a minus sign when it is followed by a
34 if c == '--' and font == r'\fR':
36 if c != '-' or font in (r'\fB', r'\fL'):
37 return c.replace('-', r'\-')
48 # groff(7) says that . can be escaped by \. but in practice groff
49 # still gives an error with \. at the beginning of a line.
52 raise error.Error("bad escape")
54 # Escape - \ " ' . as needed by nroff.
55 s = re.sub('(-[0-9]|--|[-"\'\\\\.])', escape, s)
58 def escapeNroffLiteral(s, font=r'\fB'):
59 return font + r'%s\fR' % textToNroff(s, font)
61 def inlineXmlToNroff(node, font, to_upper=False):
62 if node.nodeType == node.TEXT_NODE:
64 return textToNroff(node.data.upper(), font)
66 return textToNroff(node.data, font)
67 elif node.nodeType == node.ELEMENT_NODE:
68 if node.tagName in ['code', 'em', 'option']:
70 for child in node.childNodes:
71 s += inlineXmlToNroff(child, r'\fB')
73 elif node.tagName == 'ref':
75 if node.hasAttribute('column'):
76 s += node.attributes['column'].nodeValue
77 if node.hasAttribute('key'):
78 s += ':' + node.attributes['key'].nodeValue
79 elif node.hasAttribute('table'):
80 s += node.attributes['table'].nodeValue
81 elif node.hasAttribute('group'):
82 s += node.attributes['group'].nodeValue
83 elif node.hasAttribute('db'):
84 s += node.attributes['db'].nodeValue
86 raise error.Error("'ref' lacks required attributes: %s" % node.attributes.keys())
88 elif node.tagName == 'var' or node.tagName == 'dfn':
90 for child in node.childNodes:
91 s += inlineXmlToNroff(child, r'\fI')
94 raise error.Error("element <%s> unknown or invalid here" % node.tagName)
96 raise error.Error("unknown node %s in inline xml" % node)
98 def pre_to_nroff(nodes, para, font):
101 if node.nodeType != node.TEXT_NODE:
102 fatal("<pre> element may only have text children")
103 for line in node.data.split('\n'):
104 s += escapeNroffLiteral(line, font) + '\n.br\n'
108 def blockXmlToNroff(nodes, para='.PP'):
111 if node.nodeType == node.TEXT_NODE:
112 s += textToNroff(node.data)
114 elif node.nodeType == node.ELEMENT_NODE:
115 if node.tagName in ['ul', 'ol']:
120 for liNode in node.childNodes:
121 if (liNode.nodeType == node.ELEMENT_NODE
122 and liNode.tagName == 'li'):
124 if node.tagName == 'ul':
127 s += ".IP %d. .25in\n" % i
128 s += blockXmlToNroff(liNode.childNodes, ".IP")
129 elif (liNode.nodeType != node.TEXT_NODE
130 or not liNode.data.isspace()):
131 raise error.Error("<%s> element may only have <li> children" % node.tagName)
133 elif node.tagName == 'dl':
138 for liNode in node.childNodes:
139 if (liNode.nodeType == node.ELEMENT_NODE
140 and liNode.tagName == 'dt'):
146 elif (liNode.nodeType == node.ELEMENT_NODE
147 and liNode.tagName == 'dd'):
151 elif (liNode.nodeType != node.TEXT_NODE
152 or not liNode.data.isspace()):
153 raise error.Error("<dl> element may only have <dt> and <dd> children")
154 s += blockXmlToNroff(liNode.childNodes, ".IP")
156 elif node.tagName == 'p':
158 if not s.endswith("\n"):
161 s += blockXmlToNroff(node.childNodes, para)
162 elif node.tagName in ('h1', 'h2', 'h3'):
164 if not s.endswith("\n"):
166 nroffTag = {'h1': 'SH', 'h2': 'SS', 'h3': 'ST'}[node.tagName]
167 s += '.%s "' % nroffTag
168 for child_node in node.childNodes:
169 s += inlineXmlToNroff(child_node, r'\fR',
170 to_upper=(nroffTag == 'SH'))
172 elif node.tagName == 'pre':
173 fixed = node.getAttribute('fixed')
178 s += pre_to_nroff(node.childNodes, para, font)
180 s += inlineXmlToNroff(node, r'\fR')
182 raise error.Error("unknown node %s in block xml" % node)
183 if s != "" and not s.endswith('\n'):