blob: 2aa7284beb03d5aaa1be77bc66d436114b9f01ee [file] [log] [blame]
Fred Drake03204731998-11-23 17:02:03 +00001#! /usr/bin/env python
2
3"""Promote the IDs from <label/> elements to the enclosing section / chapter /
4whatever, then remove the <label/> elements. This allows *ML style internal
5linking rather than the bogus LaTeX model.
6
7Note that <label/>s in <title> elements are promoted two steps, since the
8<title> elements are artificially created from the section parameter, and the
9label really refers to the sectioning construct.
10"""
11__version__ = '$Revision$'
12
13
14import errno
Fred Drake4db5b461998-12-01 19:03:01 +000015import esistools
16import re
Fred Drake03204731998-11-23 17:02:03 +000017import string
18import sys
19import xml.dom.core
20import xml.dom.esis_builder
21
22
Fred Drakef8ebb551999-01-14 19:45:38 +000023class ConversionError(Exception):
24 pass
25
26
Fred Drakefcc59101999-01-06 22:50:52 +000027DEBUG_PARA_FIXER = 0
28
29
Fred Drake03204731998-11-23 17:02:03 +000030# Workaround to deal with invalid documents (multiple root elements). This
31# does not indicate a bug in the DOM implementation.
32#
33def get_documentElement(self):
34 docelem = None
35 for n in self._node.children:
36 if n.type == xml.dom.core.ELEMENT:
37 docelem = xml.dom.core.Element(n, self, self)
38 return docelem
39
40xml.dom.core.Document.get_documentElement = get_documentElement
41
42
43# Replace get_childNodes for the Document class; without this, children
44# accessed from the Document object via .childNodes (no matter how many
45# levels of access are used) will be given an ownerDocument of None.
46#
47def get_childNodes(self):
48 return xml.dom.core.NodeList(self._node.children, self, self)
49
50xml.dom.core.Document.get_childNodes = get_childNodes
51
52
53def get_first_element(doc, gi):
54 for n in doc.childNodes:
55 if n.nodeType == xml.dom.core.ELEMENT and n.tagName == gi:
56 return n
57
58def extract_first_element(doc, gi):
59 node = get_first_element(doc, gi)
60 if node is not None:
61 doc.removeChild(node)
62 return node
63
64
65def simplify(doc):
66 # Try to rationalize the document a bit, since these things are simply
67 # not valid SGML/XML documents as they stand, and need a little work.
68 documentclass = "document"
69 inputs = []
70 node = extract_first_element(doc, "documentclass")
71 if node is not None:
72 documentclass = node.getAttribute("classname")
73 node = extract_first_element(doc, "title")
74 if node is not None:
75 inputs.append(node)
76 # update the name of the root element
77 node = get_first_element(doc, "document")
78 if node is not None:
79 node._node.name = documentclass
80 while 1:
81 node = extract_first_element(doc, "input")
82 if node is None:
83 break
84 inputs.append(node)
85 if inputs:
86 docelem = doc.documentElement
87 inputs.reverse()
88 for node in inputs:
89 text = doc.createTextNode("\n")
90 docelem.insertBefore(text, docelem.firstChild)
91 docelem.insertBefore(node, text)
92 docelem.insertBefore(doc.createTextNode("\n"), docelem.firstChild)
93 while doc.firstChild.nodeType == xml.dom.core.TEXT:
94 doc.removeChild(doc.firstChild)
95
96
97def cleanup_root_text(doc):
98 discards = []
99 skip = 0
100 for n in doc.childNodes:
101 prevskip = skip
102 skip = 0
103 if n.nodeType == xml.dom.core.TEXT and not prevskip:
104 discards.append(n)
Fred Drake4db5b461998-12-01 19:03:01 +0000105 elif n.nodeType == xml.dom.core.ELEMENT and n.tagName == "COMMENT":
Fred Drake03204731998-11-23 17:02:03 +0000106 skip = 1
107 for node in discards:
108 doc.removeChild(node)
109
110
111def rewrite_desc_entries(doc, argname_gi):
112 argnodes = doc.getElementsByTagName(argname_gi)
113 for node in argnodes:
114 parent = node.parentNode
115 nodes = []
116 for n in parent.childNodes:
117 if n.nodeType != xml.dom.core.ELEMENT or n.tagName != argname_gi:
118 nodes.append(n)
119 desc = doc.createElement("description")
120 for n in nodes:
121 parent.removeChild(n)
122 desc.appendChild(n)
123 if node.childNodes:
124 # keep the <args>...</args>, newline & indent
125 parent.insertBefore(doc.createText("\n "), node)
126 else:
127 # no arguments, remove the <args/> node
128 parent.removeChild(node)
129 parent.appendChild(doc.createText("\n "))
130 parent.appendChild(desc)
131 parent.appendChild(doc.createText("\n"))
132
133def handle_args(doc):
134 rewrite_desc_entries(doc, "args")
135 rewrite_desc_entries(doc, "constructor-args")
136
137
Fred Drake4db5b461998-12-01 19:03:01 +0000138def handle_appendix(doc):
139 # must be called after simplfy() if document is multi-rooted to begin with
140 docelem = doc.documentElement
141 toplevel = docelem.tagName == "manual" and "chapter" or "section"
142 appendices = 0
143 nodes = []
144 for node in docelem.childNodes:
145 if appendices:
146 nodes.append(node)
147 elif node.nodeType == xml.dom.core.ELEMENT:
148 appnodes = node.getElementsByTagName("appendix")
149 if appnodes:
150 appendices = 1
151 parent = appnodes[0].parentNode
152 parent.removeChild(appnodes[0])
153 parent.normalize()
154 if nodes:
155 map(docelem.removeChild, nodes)
156 docelem.appendChild(doc.createTextNode("\n\n\n"))
157 back = doc.createElement("back-matter")
158 docelem.appendChild(back)
159 back.appendChild(doc.createTextNode("\n"))
160 while nodes and nodes[0].nodeType == xml.dom.core.TEXT \
161 and not string.strip(nodes[0].data):
162 del nodes[0]
163 map(back.appendChild, nodes)
164 docelem.appendChild(doc.createTextNode("\n"))
Fred Drake03204731998-11-23 17:02:03 +0000165
166
167def handle_labels(doc):
168 labels = doc.getElementsByTagName("label")
169 for label in labels:
170 id = label.getAttribute("id")
171 if not id:
172 continue
173 parent = label.parentNode
174 if parent.tagName == "title":
175 parent.parentNode.setAttribute("id", id)
176 else:
177 parent.setAttribute("id", id)
178 # now, remove <label id="..."/> from parent:
179 parent.removeChild(label)
180
181
Fred Drake1ff6db41998-11-23 23:10:35 +0000182def fixup_trailing_whitespace(doc, wsmap):
183 queue = [doc]
184 while queue:
185 node = queue[0]
186 del queue[0]
187 if node.nodeType == xml.dom.core.ELEMENT \
188 and wsmap.has_key(node.tagName):
189 ws = wsmap[node.tagName]
190 children = node.childNodes
191 children.reverse()
192 if children[0].nodeType == xml.dom.core.TEXT:
193 data = string.rstrip(children[0].data) + ws
194 children[0].data = data
195 children.reverse()
196 # hack to get the title in place:
197 if node.tagName == "title" \
198 and node.parentNode.firstChild.nodeType == xml.dom.core.ELEMENT:
199 node.parentNode.insertBefore(doc.createText("\n "),
200 node.parentNode.firstChild)
201 for child in node.childNodes:
202 if child.nodeType == xml.dom.core.ELEMENT:
203 queue.append(child)
204
205
206def normalize(doc):
207 for node in doc.childNodes:
208 if node.nodeType == xml.dom.core.ELEMENT:
209 node.normalize()
210
211
212def cleanup_trailing_parens(doc, element_names):
213 d = {}
214 for gi in element_names:
215 d[gi] = gi
216 rewrite_element = d.has_key
217 queue = []
218 for node in doc.childNodes:
219 if node.nodeType == xml.dom.core.ELEMENT:
220 queue.append(node)
221 while queue:
222 node = queue[0]
223 del queue[0]
224 if rewrite_element(node.tagName):
225 children = node.childNodes
226 if len(children) == 1 \
227 and children[0].nodeType == xml.dom.core.TEXT:
228 data = children[0].data
229 if data[-2:] == "()":
230 children[0].data = data[:-2]
231 else:
232 for child in node.childNodes:
233 if child.nodeType == xml.dom.core.ELEMENT:
234 queue.append(child)
235
236
Fred Drakeaaed9711998-12-10 20:25:30 +0000237def contents_match(left, right):
238 left_children = left.childNodes
239 right_children = right.childNodes
240 if len(left_children) != len(right_children):
241 return 0
242 for l, r in map(None, left_children, right_children):
243 nodeType = l.nodeType
244 if nodeType != r.nodeType:
245 return 0
246 if nodeType == xml.dom.core.ELEMENT:
247 if l.tagName != r.tagName:
248 return 0
249 # should check attributes, but that's not a problem here
250 if not contents_match(l, r):
251 return 0
252 elif nodeType == xml.dom.core.TEXT:
253 if l.data != r.data:
254 return 0
255 else:
256 # not quite right, but good enough
257 return 0
258 return 1
259
260
261def create_module_info(doc, section):
262 # Heavy.
263 node = extract_first_element(section, "modulesynopsis")
264 if node is None:
265 return
266 node._node.name = "synopsis"
267 lastchild = node.childNodes[-1]
268 if lastchild.nodeType == xml.dom.core.TEXT \
269 and lastchild.data[-1:] == ".":
270 lastchild.data = lastchild.data[:-1]
271 if section.tagName == "section":
272 modinfo_pos = 2
273 modinfo = doc.createElement("moduleinfo")
274 moddecl = extract_first_element(section, "declaremodule")
275 name = None
276 if moddecl:
277 modinfo.appendChild(doc.createTextNode("\n "))
278 name = moddecl.attributes["name"].value
279 namenode = doc.createElement("name")
280 namenode.appendChild(doc.createTextNode(name))
281 modinfo.appendChild(namenode)
282 type = moddecl.attributes.get("type")
283 if type:
284 type = type.value
285 modinfo.appendChild(doc.createTextNode("\n "))
286 typenode = doc.createElement("type")
287 typenode.appendChild(doc.createTextNode(type))
288 modinfo.appendChild(typenode)
289 title = get_first_element(section, "title")
290 if title:
291 children = title.childNodes
292 if len(children) >= 2 \
293 and children[0].nodeType == xml.dom.core.ELEMENT \
294 and children[0].tagName == "module" \
295 and children[0].childNodes[0].data == name:
296 # this is it; morph the <title> into <short-synopsis>
297 first_data = children[1]
298 if first_data.data[:4] == " ---":
299 first_data.data = string.lstrip(first_data.data[4:])
300 title._node.name = "short-synopsis"
301 if children[-1].data[-1:] == ".":
302 children[-1].data = children[-1].data[:-1]
303 section.removeChild(title)
304 section.removeChild(section.childNodes[0])
305 title.removeChild(children[0])
306 modinfo_pos = 0
307 else:
308 sys.stderr.write(
309 "module name in title doesn't match"
310 " <declaremodule>; no <short-synopsis>\n")
311 else:
312 sys.stderr.write(
313 "Unexpected condition: <section> without <title>\n")
314 modinfo.appendChild(doc.createTextNode("\n "))
315 modinfo.appendChild(node)
316 if title and not contents_match(title, node):
317 # The short synopsis is actually different,
318 # and needs to be stored:
319 modinfo.appendChild(doc.createTextNode("\n "))
320 modinfo.appendChild(title)
321 modinfo.appendChild(doc.createTextNode("\n "))
322 section.insertBefore(modinfo, section.childNodes[modinfo_pos])
323 section.insertBefore(doc.createTextNode("\n "), modinfo)
324
325
Fred Drakefba0ba21998-12-10 05:07:09 +0000326def cleanup_synopses(doc):
Fred Drakeaaed9711998-12-10 20:25:30 +0000327 for node in doc.childNodes:
328 if node.nodeType == xml.dom.core.ELEMENT \
329 and node.tagName == "section":
330 create_module_info(doc, node)
331
332
Fred Drakef8ebb551999-01-14 19:45:38 +0000333def remap_element_names(root, name_map):
334 queue = []
335 for child in root.childNodes:
336 if child.nodeType == xml.dom.core.ELEMENT:
337 queue.append(child)
338 while queue:
339 node = queue.pop()
340 tagName = node.tagName
341 if name_map.has_key(tagName):
342 name, attrs = name_map[tagName]
343 node._node.name = name
344 for attr, value in attrs.items():
345 node.setAttribute(attr, value)
346 for child in node.childNodes:
347 if child.nodeType == xml.dom.core.ELEMENT:
348 queue.append(child)
349
350
351def fixup_table_structures(doc):
352 # must be done after remap_element_names(), or the tables won't be found
353 for child in doc.childNodes:
354 if child.nodeType == xml.dom.core.ELEMENT:
355 tables = child.getElementsByTagName("table")
356 for table in tables:
357 fixup_table(doc, table)
358
359def fixup_table(doc, table):
360 # create the table head
361 thead = doc.createElement("thead")
362 row = doc.createElement("row")
363 move_elements_by_name(doc, table, row, "entry")
364 thead.appendChild(doc.createTextNode("\n "))
365 thead.appendChild(row)
366 thead.appendChild(doc.createTextNode("\n "))
367 # create the table body
368 tbody = doc.createElement("tbody")
369 prev_row = None
370 last_was_hline = 0
371 children = table.childNodes
372 for child in children:
373 if child.nodeType == xml.dom.core.ELEMENT:
374 tagName = child.tagName
375 if tagName == "hline" and prev_row is not None:
376 prev_row.setAttribute("rowsep", "1")
377 elif tagName == "row":
378 prev_row = child
379 # save the rows:
380 tbody.appendChild(doc.createTextNode("\n "))
381 move_elements_by_name(doc, table, tbody, "row", sep="\n ")
382 # and toss the rest:
383 while children:
384 child = children[0]
385 nodeType = child.nodeType
386 if nodeType == xml.dom.core.TEXT:
387 if string.strip(child.data):
388 raise ConversionError("unexpected free data in table")
389 table.removeChild(child)
390 continue
391 if nodeType == xml.dom.core.ELEMENT:
392 if child.tagName != "hline":
393 raise ConversionError(
394 "unexpected <%s> in table" % child.tagName)
395 table.removeChild(child)
396 continue
397 raise ConversionError(
398 "unexpected %s node in table" % child.__class__.__name__)
399 # nothing left in the <table>; add the <thead> and <tbody>
400 tgroup = doc.createElement("tgroup")
401 tgroup.appendChild(doc.createTextNode("\n "))
402 tgroup.appendChild(thead)
403 tgroup.appendChild(doc.createTextNode("\n "))
404 tgroup.appendChild(tbody)
405 tgroup.appendChild(doc.createTextNode("\n "))
406 table.appendChild(tgroup)
407 # now make the <entry>s look nice:
408 for row in table.getElementsByTagName("row"):
409 fixup_row(doc, row)
410
411
412def fixup_row(doc, row):
413 entries = []
414 map(entries.append, row.childNodes[1:])
415 for entry in entries:
416 row.insertBefore(doc.createTextNode("\n "), entry)
417# row.appendChild(doc.createTextNode("\n "))
418
419
420def move_elements_by_name(doc, source, dest, name, sep=None):
421 nodes = []
422 for child in source.childNodes:
423 if child.nodeType == xml.dom.core.ELEMENT and child.tagName == name:
424 nodes.append(child)
425 for node in nodes:
426 source.removeChild(node)
427 dest.appendChild(node)
428 if sep:
429 dest.appendChild(doc.createTextNode(sep))
430
431
Fred Drakefcc59101999-01-06 22:50:52 +0000432FIXUP_PARA_ELEMENTS = (
433 "chapter",
434 "section", "subsection", "subsubsection",
435 "paragraph", "subparagraph")
436
437PARA_LEVEL_ELEMENTS = (
438 "moduleinfo", "title", "opcodedesc",
439 "verbatim", "funcdesc", "methoddesc", "excdesc", "datadesc",
440 "funcdescni", "methoddescni", "excdescni", "datadescni",
441 "tableii", "tableiii", "tableiv", "localmoduletable",
442 "sectionauthor",
443 # include <para>, so we can just do it again to get subsequent paras:
444 "para",
445 )
446
447PARA_LEVEL_PRECEEDERS = (
448 "index", "indexii", "indexiii", "indexiv",
449 "stindex", "obindex", "COMMENT", "label",
450 )
451
Fred Drakeaaed9711998-12-10 20:25:30 +0000452def fixup_paras(doc):
Fred Drakefcc59101999-01-06 22:50:52 +0000453 for child in doc.childNodes:
454 if child.nodeType == xml.dom.core.ELEMENT \
455 and child.tagName in FIXUP_PARA_ELEMENTS:
456 fixup_paras_helper(doc, child)
457 descriptions = child.getElementsByTagName("description")
458 for description in descriptions:
459 if DEBUG_PARA_FIXER:
460 sys.stderr.write("-- Fixing up <description> element...\n")
461 fixup_paras_helper(doc, description)
462
463
464def fixup_paras_helper(doc, container):
465 # document is already normalized
466 children = container.childNodes
467 start = 0
468 start_fixed = 0
469 i = 0
470 SKIP_ELEMENTS = PARA_LEVEL_ELEMENTS + PARA_LEVEL_PRECEEDERS
471 for child in children:
472 if child.nodeType == xml.dom.core.ELEMENT:
473 if child.tagName in FIXUP_PARA_ELEMENTS:
474 fixup_paras_helper(doc, child)
475 break
476 elif child.tagName in SKIP_ELEMENTS:
477 if not start_fixed:
478 start = i + 1
479 elif not start_fixed:
480 start_fixed = 1
481 i = i + 1
482 else:
483 if child.nodeType == xml.dom.core.TEXT \
484 and string.strip(child.data) and not start_fixed:
485 start_fixed = 1
486 i = i + 1
487 if DEBUG_PARA_FIXER:
488 sys.stderr.write("fixup_paras_helper() called on <%s>; %d, %d\n"
489 % (container.tagName, start, i))
490 if i > start:
491 # the first [start:i] children shoudl be rewritten as <para> elements
492 # start by breaking text nodes that contain \n\n+ into multiple nodes
493 nstart, i = skip_leading_nodes(container.childNodes, start, i)
494 if i > nstart:
495 build_para(doc, container, nstart, i)
496 fixup_paras_helper(doc, container)
497
498
499def build_para(doc, parent, start, i):
500 children = parent.childNodes
501 # collect all children until \n\n+ is found in a text node or a
502 # PARA_LEVEL_ELEMENT is found.
503 after = start + 1
504 have_last = 0
505 BREAK_ELEMENTS = PARA_LEVEL_ELEMENTS + FIXUP_PARA_ELEMENTS
506 for j in range(start, i):
507 after = j + 1
508 child = children[j]
509 nodeType = child.nodeType
510 if nodeType == xml.dom.core.ELEMENT:
511 if child.tagName in BREAK_ELEMENTS:
512 after = j
513 break
514 elif nodeType == xml.dom.core.TEXT:
515 pos = string.find(child.data, "\n\n")
516 if pos == 0:
517 after = j
518 break
519 if pos >= 1:
520 child.splitText(pos)
521 break
522 else:
523 have_last = 1
524 if children[after - 1].nodeType == xml.dom.core.TEXT:
525 # we may need to split off trailing white space:
526 child = children[after - 1]
527 data = child.data
528 if string.rstrip(data) != data:
529 have_last = 0
530 child.splitText(len(string.rstrip(data)))
531 children = parent.childNodes
532 para = doc.createElement("para")
533 prev = None
534 indexes = range(start, after)
535 indexes.reverse()
536 for j in indexes:
537 node = children[j]
538 parent.removeChild(node)
539 para.insertBefore(node, prev)
540 prev = node
541 if have_last:
542 parent.appendChild(para)
543 else:
544 parent.insertBefore(para, parent.childNodes[start])
545
546
547def skip_leading_nodes(children, start, i):
548 i = min(i, len(children))
549 while i > start:
550 # skip over leading comments and whitespace:
551 try:
552 child = children[start]
553 except IndexError:
554 sys.stderr.write(
555 "skip_leading_nodes() failed at index %d\n" % start)
556 raise
557 nodeType = child.nodeType
558 if nodeType == xml.dom.core.COMMENT:
559 start = start + 1
560 elif nodeType == xml.dom.core.TEXT:
561 data = child.data
562 shortened = string.lstrip(data)
563 if shortened:
564 if data != shortened:
565 # break into two nodes: whitespace and non-whitespace
566 child.splitText(len(data) - len(shortened))
567 return start + 1, i + 1
568 break
569 # all whitespace, just skip
570 start = start + 1
571 elif nodeType == xml.dom.core.ELEMENT:
572 if child.tagName in PARA_LEVEL_ELEMENTS + PARA_LEVEL_PRECEEDERS:
573 start = start + 1
574 else:
575 break
576 else:
577 break
578 return start, i
Fred Drakefba0ba21998-12-10 05:07:09 +0000579
580
Fred Draked24167b1999-01-14 21:18:03 +0000581def fixup_rfc_references(doc):
582 rfc_nodes = []
583 for child in doc.childNodes:
584 if child.nodeType == xml.dom.core.ELEMENT:
585 kids = child.getElementsByTagName("rfc")
586 for k in kids:
587 rfc_nodes.append(k)
588 for rfc_node in rfc_nodes:
589 rfc_node.appendChild(doc.createTextNode(
590 "RFC " + rfc_node.getAttribute("num")))
591
592
593def fixup_signatures(doc):
594 for child in doc.childNodes:
595 if child.nodeType == xml.dom.core.ELEMENT:
596 args = child.getElementsByTagName("args")
597 for arg in args:
598 fixup_args(doc, arg)
599 args = child.getElementsByTagName("constructor-args")
600 for arg in args:
601 fixup_args(doc, arg)
602 arg.normalize()
603
604
605def fixup_args(doc, arglist):
606 for child in arglist.childNodes:
607 if child.nodeType == xml.dom.core.ELEMENT \
608 and child.tagName == "optional":
609 # found it; fix and return
610 arglist.insertBefore(doc.createTextNode("["), child)
611 optkids = child.childNodes
612 while optkids:
613 k = optkids[0]
614 child.removeChild(k)
615 arglist.insertBefore(k, child)
616 arglist.insertBefore(doc.createTextNode("]"), child)
617 arglist.removeChild(child)
618 return fixup_args(doc, arglist)
619
620
Fred Drake4db5b461998-12-01 19:03:01 +0000621_token_rx = re.compile(r"[a-zA-Z][a-zA-Z0-9.-]*$")
Fred Drakefcc59101999-01-06 22:50:52 +0000622
Fred Drake4db5b461998-12-01 19:03:01 +0000623def write_esis(doc, ofp, knownempty):
624 for node in doc.childNodes:
625 nodeType = node.nodeType
626 if nodeType == xml.dom.core.ELEMENT:
627 gi = node.tagName
628 if knownempty(gi):
629 if node.hasChildNodes():
630 raise ValueError, "declared-empty node has children"
631 ofp.write("e\n")
632 for k, v in node.attributes.items():
633 value = v.value
634 if _token_rx.match(value):
635 dtype = "TOKEN"
636 else:
637 dtype = "CDATA"
638 ofp.write("A%s %s %s\n" % (k, dtype, esistools.encode(value)))
639 ofp.write("(%s\n" % gi)
640 write_esis(node, ofp, knownempty)
641 ofp.write(")%s\n" % gi)
642 elif nodeType == xml.dom.core.TEXT:
643 ofp.write("-%s\n" % esistools.encode(node.data))
644 else:
645 raise RuntimeError, "unsupported node type: %s" % nodeType
646
647
Fred Drake03204731998-11-23 17:02:03 +0000648def convert(ifp, ofp):
Fred Drake4db5b461998-12-01 19:03:01 +0000649 p = esistools.ExtendedEsisBuilder()
Fred Drake03204731998-11-23 17:02:03 +0000650 p.feed(ifp.read())
651 doc = p.document
Fred Drake1ff6db41998-11-23 23:10:35 +0000652 normalize(doc)
Fred Drake03204731998-11-23 17:02:03 +0000653 handle_args(doc)
Fred Drake03204731998-11-23 17:02:03 +0000654 simplify(doc)
655 handle_labels(doc)
Fred Drake4db5b461998-12-01 19:03:01 +0000656 handle_appendix(doc)
Fred Drake1ff6db41998-11-23 23:10:35 +0000657 fixup_trailing_whitespace(doc, {
658 "abstract": "\n",
659 "title": "",
660 "chapter": "\n\n",
661 "section": "\n\n",
662 "subsection": "\n\n",
663 "subsubsection": "\n\n",
664 "paragraph": "\n\n",
665 "subparagraph": "\n\n",
666 })
Fred Drake03204731998-11-23 17:02:03 +0000667 cleanup_root_text(doc)
Fred Drake1ff6db41998-11-23 23:10:35 +0000668 cleanup_trailing_parens(doc, ["function", "method", "cfunction"])
Fred Drakefba0ba21998-12-10 05:07:09 +0000669 cleanup_synopses(doc)
Fred Drakeaaed9711998-12-10 20:25:30 +0000670 normalize(doc)
671 fixup_paras(doc)
Fred Drakef8ebb551999-01-14 19:45:38 +0000672 remap_element_names(doc, {
673 "tableii": ("table", {"cols": "2"}),
674 "tableiii": ("table", {"cols": "3"}),
675 "tableiv": ("table", {"cols": "4"}),
676 "lineii": ("row", {}),
677 "lineiii": ("row", {}),
678 "lineiv": ("row", {}),
Fred Draked6ced7d1999-01-19 17:11:23 +0000679 "refmodule": ("module", {"link": "link"}),
Fred Drakef8ebb551999-01-14 19:45:38 +0000680 })
681 fixup_table_structures(doc)
Fred Draked24167b1999-01-14 21:18:03 +0000682 fixup_rfc_references(doc)
683 fixup_signatures(doc)
Fred Drake4db5b461998-12-01 19:03:01 +0000684 #
685 d = {}
686 for gi in p.get_empties():
687 d[gi] = gi
Fred Draked24167b1999-01-14 21:18:03 +0000688 if d.has_key("rfc"):
689 del d["rfc"]
Fred Drake4db5b461998-12-01 19:03:01 +0000690 knownempty = d.has_key
691 #
Fred Drake03204731998-11-23 17:02:03 +0000692 try:
Fred Drake4db5b461998-12-01 19:03:01 +0000693 write_esis(doc, ofp, knownempty)
Fred Drake03204731998-11-23 17:02:03 +0000694 except IOError, (err, msg):
695 # Ignore EPIPE; it just means that whoever we're writing to stopped
696 # reading. The rest of the output would be ignored. All other errors
697 # should still be reported,
698 if err != errno.EPIPE:
699 raise
700
701
702def main():
703 if len(sys.argv) == 1:
704 ifp = sys.stdin
705 ofp = sys.stdout
706 elif len(sys.argv) == 2:
707 ifp = open(sys.argv[1])
708 ofp = sys.stdout
709 elif len(sys.argv) == 3:
710 ifp = open(sys.argv[1])
711 ofp = open(sys.argv[2], "w")
712 else:
713 usage()
714 sys.exit(2)
715 convert(ifp, ofp)
716
717
718if __name__ == "__main__":
719 main()