| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  | #! /usr/bin/env python | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  | """Perform massive transformations on a document tree created from the LaTeX
 | 
					
						
							|  |  |  | of the Python documentation, and dump the ESIS data for the transformed tree. | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import errno | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  | import esistools | 
					
						
							|  |  |  | import re | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  | import sys | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  | import xml.dom | 
					
						
							|  |  |  | import xml.dom.minidom | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  | ELEMENT = xml.dom.Node.ELEMENT_NODE | 
					
						
							|  |  |  | ENTITY_REFERENCE = xml.dom.Node.ENTITY_REFERENCE_NODE | 
					
						
							|  |  |  | TEXT = xml.dom.Node.TEXT_NODE | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-14 19:45:38 +00:00
										 |  |  | class ConversionError(Exception): | 
					
						
							|  |  |  |     pass | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  | ewrite = sys.stderr.write | 
					
						
							|  |  |  | try: | 
					
						
							|  |  |  |     # We can only do this trick on Unix (if tput is on $PATH)! | 
					
						
							|  |  |  |     if sys.platform != "posix" or not sys.stderr.isatty(): | 
					
						
							|  |  |  |         raise ImportError | 
					
						
							|  |  |  |     import commands | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  |     bwrite = ewrite | 
					
						
							|  |  |  | else: | 
					
						
							|  |  |  |     def bwrite(s, BOLDON=commands.getoutput("tput bold"), | 
					
						
							|  |  |  |                BOLDOFF=commands.getoutput("tput sgr0")): | 
					
						
							|  |  |  |         ewrite("%s%s%s" % (BOLDON, s, BOLDOFF)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  | PARA_ELEMENT = "para" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  | DEBUG_PARA_FIXER = 0 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  | if DEBUG_PARA_FIXER: | 
					
						
							|  |  |  |     def para_msg(s): | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |         ewrite("*** %s\n" % s) | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  | else: | 
					
						
							|  |  |  |     def para_msg(s): | 
					
						
							|  |  |  |         pass | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  | def get_first_element(doc, gi): | 
					
						
							|  |  |  |     for n in doc.childNodes: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         if n.nodeName == gi: | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  |             return n | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def extract_first_element(doc, gi): | 
					
						
							|  |  |  |     node = get_first_element(doc, gi) | 
					
						
							|  |  |  |     if node is not None: | 
					
						
							|  |  |  |         doc.removeChild(node) | 
					
						
							|  |  |  |     return node | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  | def get_documentElement(node): | 
					
						
							|  |  |  |     result = None | 
					
						
							|  |  |  |     for child in node.childNodes: | 
					
						
							|  |  |  |         if child.nodeType == ELEMENT: | 
					
						
							|  |  |  |             result = child | 
					
						
							|  |  |  |     return result | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def set_tagName(elem, gi): | 
					
						
							|  |  |  |     elem.nodeName = elem.tagName = gi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  | def find_all_elements(doc, gi): | 
					
						
							|  |  |  |     nodes = [] | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |     if doc.nodeName == gi: | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |         nodes.append(doc) | 
					
						
							|  |  |  |     for child in doc.childNodes: | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         if child.nodeType == ELEMENT: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |             if child.tagName == gi: | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |                 nodes.append(child) | 
					
						
							|  |  |  |             for node in child.getElementsByTagName(gi): | 
					
						
							|  |  |  |                 nodes.append(node) | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     return nodes | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  | def find_all_child_elements(doc, gi): | 
					
						
							|  |  |  |     nodes = [] | 
					
						
							|  |  |  |     for child in doc.childNodes: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         if child.nodeName == gi: | 
					
						
							| 
									
										
										
										
											1999-08-20 14:28:29 +00:00
										 |  |  |             nodes.append(child) | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |     return nodes | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  | def find_all_elements_from_set(doc, gi_set): | 
					
						
							|  |  |  |     return __find_all_elements_from_set(doc, gi_set, []) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def __find_all_elements_from_set(doc, gi_set, nodes): | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |     if doc.nodeName in gi_set: | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |         nodes.append(doc) | 
					
						
							|  |  |  |     for child in doc.childNodes: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         if child.nodeType == ELEMENT: | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |             __find_all_elements_from_set(child, gi_set, nodes) | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     return nodes | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  | def simplify(doc, fragment): | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  |     # Try to rationalize the document a bit, since these things are simply | 
					
						
							|  |  |  |     # not valid SGML/XML documents as they stand, and need a little work. | 
					
						
							|  |  |  |     documentclass = "document" | 
					
						
							|  |  |  |     inputs = [] | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |     node = extract_first_element(fragment, "documentclass") | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  |     if node is not None: | 
					
						
							|  |  |  |         documentclass = node.getAttribute("classname") | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |     node = extract_first_element(fragment, "title") | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  |     if node is not None: | 
					
						
							|  |  |  |         inputs.append(node) | 
					
						
							|  |  |  |     # update the name of the root element | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |     node = get_first_element(fragment, "document") | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  |     if node is not None: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         set_tagName(node, documentclass) | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |         # Move everything that comes before this node into this node; | 
					
						
							|  |  |  |         # this will be the document element. | 
					
						
							|  |  |  |         nodelist = fragment.childNodes | 
					
						
							|  |  |  |         point = node.firstChild | 
					
						
							|  |  |  |         while not nodelist[0].isSameNode(node): | 
					
						
							|  |  |  |             node.insertBefore(nodelist[0], point) | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  |     while 1: | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         node = extract_first_element(fragment, "input") | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  |         if node is None: | 
					
						
							|  |  |  |             break | 
					
						
							|  |  |  |         inputs.append(node) | 
					
						
							|  |  |  |     if inputs: | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         docelem = get_documentElement(fragment) | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  |         inputs.reverse() | 
					
						
							|  |  |  |         for node in inputs: | 
					
						
							|  |  |  |             text = doc.createTextNode("\n") | 
					
						
							|  |  |  |             docelem.insertBefore(text, docelem.firstChild) | 
					
						
							|  |  |  |             docelem.insertBefore(node, text) | 
					
						
							|  |  |  |         docelem.insertBefore(doc.createTextNode("\n"), docelem.firstChild) | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |     while fragment.firstChild and fragment.firstChild.nodeType == TEXT: | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         fragment.removeChild(fragment.firstChild) | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def cleanup_root_text(doc): | 
					
						
							|  |  |  |     discards = [] | 
					
						
							|  |  |  |     skip = 0 | 
					
						
							|  |  |  |     for n in doc.childNodes: | 
					
						
							|  |  |  |         prevskip = skip | 
					
						
							|  |  |  |         skip = 0 | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         if n.nodeType == TEXT and not prevskip: | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  |             discards.append(n) | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         elif n.nodeName == "COMMENT": | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  |             skip = 1 | 
					
						
							|  |  |  |     for node in discards: | 
					
						
							|  |  |  |         doc.removeChild(node) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  | DESCRIPTOR_ELEMENTS = ( | 
					
						
							|  |  |  |     "cfuncdesc", "cvardesc", "ctypedesc", | 
					
						
							|  |  |  |     "classdesc", "memberdesc", "memberdescni", "methoddesc", "methoddescni", | 
					
						
							|  |  |  |     "excdesc", "funcdesc", "funcdescni", "opcodedesc", | 
					
						
							|  |  |  |     "datadesc", "datadescni", | 
					
						
							|  |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  | def fixup_descriptors(doc, fragment): | 
					
						
							|  |  |  |     sections = find_all_elements(fragment, "section") | 
					
						
							| 
									
										
										
										
											1999-01-29 21:31:12 +00:00
										 |  |  |     for section in sections: | 
					
						
							|  |  |  |         find_and_fix_descriptors(doc, section) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def find_and_fix_descriptors(doc, container): | 
					
						
							|  |  |  |     children = container.childNodes | 
					
						
							|  |  |  |     for child in children: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         if child.nodeType == ELEMENT: | 
					
						
							|  |  |  |             tagName = child.tagName | 
					
						
							| 
									
										
										
										
											1999-01-29 21:31:12 +00:00
										 |  |  |             if tagName in DESCRIPTOR_ELEMENTS: | 
					
						
							|  |  |  |                 rewrite_descriptor(doc, child) | 
					
						
							|  |  |  |             elif tagName == "subsection": | 
					
						
							|  |  |  |                 find_and_fix_descriptors(doc, child) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def rewrite_descriptor(doc, descriptor): | 
					
						
							|  |  |  |     # | 
					
						
							|  |  |  |     # Do these things: | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |     #   1. Add an "index='no'" attribute to the element if the tagName | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     #      ends in 'ni', removing the 'ni' from the name. | 
					
						
							| 
									
										
										
										
											1999-08-20 14:28:29 +00:00
										 |  |  |     #   2. Create a <signature> from the name attribute | 
					
						
							|  |  |  |     #   2a.Create an <args> if it appears to be available. | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     #   3. Create additional <signature>s from <*line{,ni}> elements, | 
					
						
							|  |  |  |     #      if found. | 
					
						
							| 
									
										
										
										
											1999-01-29 22:12:29 +00:00
										 |  |  |     #   4. If a <versionadded> is found, move it to an attribute on the | 
					
						
							|  |  |  |     #      descriptor. | 
					
						
							|  |  |  |     #   5. Move remaining child nodes to a <description> element. | 
					
						
							|  |  |  |     #   6. Put it back together. | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     # | 
					
						
							| 
									
										
										
										
											1999-08-20 14:28:29 +00:00
										 |  |  |     # 1. | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |     descname = descriptor.tagName | 
					
						
							| 
									
										
										
										
											2001-07-06 21:03:30 +00:00
										 |  |  |     index = descriptor.getAttribute("name") != "no" | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     desctype = descname[:-4] # remove 'desc' | 
					
						
							|  |  |  |     linename = desctype + "line" | 
					
						
							|  |  |  |     if not index: | 
					
						
							|  |  |  |         linename = linename + "ni" | 
					
						
							|  |  |  |     # 2. | 
					
						
							|  |  |  |     signature = doc.createElement("signature") | 
					
						
							|  |  |  |     name = doc.createElement("name") | 
					
						
							|  |  |  |     signature.appendChild(doc.createTextNode("\n    ")) | 
					
						
							|  |  |  |     signature.appendChild(name) | 
					
						
							|  |  |  |     name.appendChild(doc.createTextNode(descriptor.getAttribute("name"))) | 
					
						
							|  |  |  |     descriptor.removeAttribute("name") | 
					
						
							| 
									
										
										
										
											1999-08-20 14:28:29 +00:00
										 |  |  |     # 2a. | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |     if descriptor.hasAttribute("var"): | 
					
						
							| 
									
										
										
										
											1999-08-20 14:28:29 +00:00
										 |  |  |         if descname != "opcodedesc": | 
					
						
							|  |  |  |             raise RuntimeError, \ | 
					
						
							|  |  |  |                   "got 'var' attribute on descriptor other than opcodedesc" | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |         variable = descriptor.getAttribute("var") | 
					
						
							|  |  |  |         if variable: | 
					
						
							|  |  |  |             args = doc.createElement("args") | 
					
						
							|  |  |  |             args.appendChild(doc.createTextNode(variable)) | 
					
						
							|  |  |  |             signature.appendChild(doc.createTextNode("\n    ")) | 
					
						
							|  |  |  |             signature.appendChild(args) | 
					
						
							|  |  |  |         descriptor.removeAttribute("var") | 
					
						
							|  |  |  |     newchildren = [signature] | 
					
						
							|  |  |  |     children = descriptor.childNodes | 
					
						
							| 
									
										
										
										
											1999-08-20 14:28:29 +00:00
										 |  |  |     pos = skip_leading_nodes(children) | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     if pos < len(children): | 
					
						
							|  |  |  |         child = children[pos] | 
					
						
							| 
									
										
										
										
											1999-11-23 21:52:03 +00:00
										 |  |  |         if child.nodeName == "args": | 
					
						
							|  |  |  |             # move <args> to <signature>, or remove if empty: | 
					
						
							|  |  |  |             child.parentNode.removeChild(child) | 
					
						
							|  |  |  |             if len(child.childNodes): | 
					
						
							|  |  |  |                 signature.appendChild(doc.createTextNode("\n    ")) | 
					
						
							|  |  |  |                 signature.appendChild(child) | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     signature.appendChild(doc.createTextNode("\n  ")) | 
					
						
							| 
									
										
										
										
											1999-01-29 22:12:29 +00:00
										 |  |  |     # 3, 4. | 
					
						
							| 
									
										
										
										
											1999-08-20 14:28:29 +00:00
										 |  |  |     pos = skip_leading_nodes(children, pos) | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     while pos < len(children) \ | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |           and children[pos].nodeName in (linename, "versionadded"): | 
					
						
							|  |  |  |         if children[pos].tagName == linename: | 
					
						
							| 
									
										
										
										
											1999-01-29 22:12:29 +00:00
										 |  |  |             # this is really a supplemental signature, create <signature> | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |             oldchild = children[pos].cloneNode(1) | 
					
						
							|  |  |  |             try: | 
					
						
							|  |  |  |                 sig = methodline_to_signature(doc, children[pos]) | 
					
						
							|  |  |  |             except KeyError: | 
					
						
							|  |  |  |                 print oldchild.toxml() | 
					
						
							|  |  |  |                 raise | 
					
						
							| 
									
										
										
										
											1999-01-29 22:12:29 +00:00
										 |  |  |             newchildren.append(sig) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             # <versionadded added=...> | 
					
						
							|  |  |  |             descriptor.setAttribute( | 
					
						
							|  |  |  |                 "added", children[pos].getAttribute("version")) | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |         pos = skip_leading_nodes(children, pos + 1) | 
					
						
							| 
									
										
										
										
											1999-01-29 22:12:29 +00:00
										 |  |  |     # 5. | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     description = doc.createElement("description") | 
					
						
							|  |  |  |     description.appendChild(doc.createTextNode("\n")) | 
					
						
							|  |  |  |     newchildren.append(description) | 
					
						
							|  |  |  |     move_children(descriptor, description, pos) | 
					
						
							|  |  |  |     last = description.childNodes[-1] | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |     if last.nodeType == TEXT: | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |         last.data = last.data.rstrip() + "\n  " | 
					
						
							| 
									
										
										
										
											1999-01-29 22:12:29 +00:00
										 |  |  |     # 6. | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     # should have nothing but whitespace and signature lines in <descriptor>; | 
					
						
							|  |  |  |     # discard them | 
					
						
							|  |  |  |     while descriptor.childNodes: | 
					
						
							|  |  |  |         descriptor.removeChild(descriptor.childNodes[0]) | 
					
						
							|  |  |  |     for node in newchildren: | 
					
						
							|  |  |  |         descriptor.appendChild(doc.createTextNode("\n  ")) | 
					
						
							|  |  |  |         descriptor.appendChild(node) | 
					
						
							|  |  |  |     descriptor.appendChild(doc.createTextNode("\n")) | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def methodline_to_signature(doc, methodline): | 
					
						
							|  |  |  |     signature = doc.createElement("signature") | 
					
						
							|  |  |  |     signature.appendChild(doc.createTextNode("\n    ")) | 
					
						
							|  |  |  |     name = doc.createElement("name") | 
					
						
							|  |  |  |     name.appendChild(doc.createTextNode(methodline.getAttribute("name"))) | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     methodline.removeAttribute("name") | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |     signature.appendChild(name) | 
					
						
							|  |  |  |     if len(methodline.childNodes): | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |         args = doc.createElement("args") | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |         signature.appendChild(doc.createTextNode("\n    ")) | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |         signature.appendChild(args) | 
					
						
							|  |  |  |         move_children(methodline, args) | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |     signature.appendChild(doc.createTextNode("\n  ")) | 
					
						
							|  |  |  |     return signature | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  | def move_children(origin, dest, start=0): | 
					
						
							|  |  |  |     children = origin.childNodes | 
					
						
							|  |  |  |     while start < len(children): | 
					
						
							|  |  |  |         node = children[start] | 
					
						
							|  |  |  |         origin.removeChild(node) | 
					
						
							|  |  |  |         dest.appendChild(node) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  | def handle_appendix(doc, fragment): | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  |     # must be called after simplfy() if document is multi-rooted to begin with | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |     docelem = get_documentElement(fragment) | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |     toplevel = docelem.tagName == "manual" and "chapter" or "section" | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  |     appendices = 0 | 
					
						
							|  |  |  |     nodes = [] | 
					
						
							|  |  |  |     for node in docelem.childNodes: | 
					
						
							|  |  |  |         if appendices: | 
					
						
							|  |  |  |             nodes.append(node) | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         elif node.nodeType == ELEMENT: | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  |             appnodes = node.getElementsByTagName("appendix") | 
					
						
							|  |  |  |             if appnodes: | 
					
						
							|  |  |  |                 appendices = 1 | 
					
						
							|  |  |  |                 parent = appnodes[0].parentNode | 
					
						
							|  |  |  |                 parent.removeChild(appnodes[0]) | 
					
						
							|  |  |  |                 parent.normalize() | 
					
						
							|  |  |  |     if nodes: | 
					
						
							|  |  |  |         map(docelem.removeChild, nodes) | 
					
						
							|  |  |  |         docelem.appendChild(doc.createTextNode("\n\n\n")) | 
					
						
							|  |  |  |         back = doc.createElement("back-matter") | 
					
						
							|  |  |  |         docelem.appendChild(back) | 
					
						
							|  |  |  |         back.appendChild(doc.createTextNode("\n")) | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         while nodes and nodes[0].nodeType == TEXT \ | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |               and not nodes[0].data.strip(): | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  |             del nodes[0] | 
					
						
							|  |  |  |         map(back.appendChild, nodes) | 
					
						
							|  |  |  |         docelem.appendChild(doc.createTextNode("\n")) | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  | def handle_labels(doc, fragment): | 
					
						
							|  |  |  |     for label in find_all_elements(fragment, "label"): | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |         id = label.getAttribute("id") | 
					
						
							|  |  |  |         if not id: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         parent = label.parentNode | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         parentTagName = parent.tagName | 
					
						
							| 
									
										
										
										
											1999-08-26 17:51:56 +00:00
										 |  |  |         if parentTagName == "title": | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |             parent.parentNode.setAttribute("id", id) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             parent.setAttribute("id", id) | 
					
						
							|  |  |  |         # now, remove <label id="..."/> from parent: | 
					
						
							|  |  |  |         parent.removeChild(label) | 
					
						
							| 
									
										
										
										
											1999-08-26 17:51:56 +00:00
										 |  |  |         if parentTagName == "title": | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |             parent.normalize() | 
					
						
							|  |  |  |             children = parent.childNodes | 
					
						
							|  |  |  |             if children[-1].nodeType == TEXT: | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |                 children[-1].data = children[-1].data.rstrip() | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  | def fixup_trailing_whitespace(doc, fragment, wsmap): | 
					
						
							|  |  |  |     queue = [fragment] | 
					
						
							|  |  |  |     fixups = [] | 
					
						
							| 
									
										
										
										
											1998-11-23 23:10:35 +00:00
										 |  |  |     while queue: | 
					
						
							|  |  |  |         node = queue[0] | 
					
						
							|  |  |  |         del queue[0] | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         if wsmap.has_key(node.nodeName): | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |             fixups.append(node) | 
					
						
							| 
									
										
										
										
											1998-11-23 23:10:35 +00:00
										 |  |  |         for child in node.childNodes: | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |             if child.nodeType == ELEMENT: | 
					
						
							| 
									
										
										
										
											1998-11-23 23:10:35 +00:00
										 |  |  |                 queue.append(child) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |     # reverse the list to process from the inside out | 
					
						
							|  |  |  |     fixups.reverse() | 
					
						
							|  |  |  |     for node in fixups: | 
					
						
							|  |  |  |         node.parentNode.normalize() | 
					
						
							|  |  |  |         lastchild = node.lastChild | 
					
						
							|  |  |  |         before, after = wsmap[node.tagName] | 
					
						
							|  |  |  |         if lastchild.nodeType == TEXT: | 
					
						
							|  |  |  |             data = lastchild.data.rstrip() + before | 
					
						
							|  |  |  |             lastchild.data = data | 
					
						
							|  |  |  |         norm = 0 | 
					
						
							|  |  |  |         if wsmap[node.tagName]: | 
					
						
							|  |  |  |             nextnode = node.nextSibling | 
					
						
							|  |  |  |             if nextnode and nextnode.nodeType == TEXT: | 
					
						
							|  |  |  |                 nextnode.data = after + nextnode.data.lstrip() | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 wsnode = doc.createTextNode(after) | 
					
						
							|  |  |  |                 node.parentNode.insertBefore(wsnode, nextnode) | 
					
						
							|  |  |  |         # hack to get the title in place: | 
					
						
							|  |  |  |         if node.tagName == "title" \ | 
					
						
							|  |  |  |            and node.parentNode.firstChild.nodeType == ELEMENT: | 
					
						
							|  |  |  |             node.parentNode.insertBefore(doc.createTextNode("\n  "), | 
					
						
							|  |  |  |                                          node.parentNode.firstChild) | 
					
						
							|  |  |  |             node.parentNode.normalize() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-11-23 23:10:35 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def normalize(doc): | 
					
						
							|  |  |  |     for node in doc.childNodes: | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         if node.nodeType == ELEMENT: | 
					
						
							| 
									
										
										
										
											1998-11-23 23:10:35 +00:00
										 |  |  |             node.normalize() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def cleanup_trailing_parens(doc, element_names): | 
					
						
							|  |  |  |     d = {} | 
					
						
							|  |  |  |     for gi in element_names: | 
					
						
							|  |  |  |         d[gi] = gi | 
					
						
							|  |  |  |     rewrite_element = d.has_key | 
					
						
							| 
									
										
										
										
											2001-09-29 05:05:25 +00:00
										 |  |  |     queue = [node for node in doc.childNodes if node.nodeType == ELEMENT] | 
					
						
							| 
									
										
										
										
											1998-11-23 23:10:35 +00:00
										 |  |  |     while queue: | 
					
						
							|  |  |  |         node = queue[0] | 
					
						
							|  |  |  |         del queue[0] | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         if rewrite_element(node.tagName): | 
					
						
							| 
									
										
										
										
											2001-09-29 05:05:25 +00:00
										 |  |  |             lastchild = node.lastChild | 
					
						
							|  |  |  |             if lastchild and lastchild.nodeType == TEXT: | 
					
						
							|  |  |  |                 data = lastchild.data | 
					
						
							|  |  |  |                 if data.endswith("()"): | 
					
						
							|  |  |  |                     lastchild.data = data[:-2] | 
					
						
							| 
									
										
										
										
											1998-11-23 23:10:35 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             for child in node.childNodes: | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |                 if child.nodeType == ELEMENT: | 
					
						
							| 
									
										
										
										
											1998-11-23 23:10:35 +00:00
										 |  |  |                     queue.append(child) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-10 20:25:30 +00:00
										 |  |  | def contents_match(left, right): | 
					
						
							|  |  |  |     left_children = left.childNodes | 
					
						
							|  |  |  |     right_children = right.childNodes | 
					
						
							|  |  |  |     if len(left_children) != len(right_children): | 
					
						
							|  |  |  |         return 0 | 
					
						
							|  |  |  |     for l, r in map(None, left_children, right_children): | 
					
						
							|  |  |  |         nodeType = l.nodeType | 
					
						
							|  |  |  |         if nodeType != r.nodeType: | 
					
						
							|  |  |  |             return 0 | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         if nodeType == ELEMENT: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |             if l.tagName != r.tagName: | 
					
						
							| 
									
										
										
										
											1998-12-10 20:25:30 +00:00
										 |  |  |                 return 0 | 
					
						
							|  |  |  |             # should check attributes, but that's not a problem here | 
					
						
							|  |  |  |             if not contents_match(l, r): | 
					
						
							|  |  |  |                 return 0 | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         elif nodeType == TEXT: | 
					
						
							| 
									
										
										
										
											1998-12-10 20:25:30 +00:00
										 |  |  |             if l.data != r.data: | 
					
						
							|  |  |  |                 return 0 | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             # not quite right, but good enough | 
					
						
							|  |  |  |             return 0 | 
					
						
							|  |  |  |     return 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def create_module_info(doc, section): | 
					
						
							|  |  |  |     # Heavy. | 
					
						
							|  |  |  |     node = extract_first_element(section, "modulesynopsis") | 
					
						
							|  |  |  |     if node is None: | 
					
						
							|  |  |  |         return | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |     set_tagName(node, "synopsis") | 
					
						
							| 
									
										
										
										
											1998-12-10 20:25:30 +00:00
										 |  |  |     lastchild = node.childNodes[-1] | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |     if lastchild.nodeType == TEXT \ | 
					
						
							| 
									
										
										
										
											1998-12-10 20:25:30 +00:00
										 |  |  |        and lastchild.data[-1:] == ".": | 
					
						
							|  |  |  |         lastchild.data = lastchild.data[:-1] | 
					
						
							| 
									
										
										
										
											1999-01-19 23:09:31 +00:00
										 |  |  |     modauthor = extract_first_element(section, "moduleauthor") | 
					
						
							|  |  |  |     if modauthor: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         set_tagName(modauthor, "author") | 
					
						
							| 
									
										
										
										
											1999-01-19 23:09:31 +00:00
										 |  |  |         modauthor.appendChild(doc.createTextNode( | 
					
						
							|  |  |  |             modauthor.getAttribute("name"))) | 
					
						
							|  |  |  |         modauthor.removeAttribute("name") | 
					
						
							| 
									
										
										
										
											1999-03-11 17:35:12 +00:00
										 |  |  |     platform = extract_first_element(section, "platform") | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |     if section.tagName == "section": | 
					
						
							| 
									
										
										
										
											1998-12-10 20:25:30 +00:00
										 |  |  |         modinfo_pos = 2 | 
					
						
							|  |  |  |         modinfo = doc.createElement("moduleinfo") | 
					
						
							|  |  |  |         moddecl = extract_first_element(section, "declaremodule") | 
					
						
							|  |  |  |         name = None | 
					
						
							|  |  |  |         if moddecl: | 
					
						
							|  |  |  |             modinfo.appendChild(doc.createTextNode("\n    ")) | 
					
						
							|  |  |  |             name = moddecl.attributes["name"].value | 
					
						
							|  |  |  |             namenode = doc.createElement("name") | 
					
						
							|  |  |  |             namenode.appendChild(doc.createTextNode(name)) | 
					
						
							|  |  |  |             modinfo.appendChild(namenode) | 
					
						
							|  |  |  |             type = moddecl.attributes.get("type") | 
					
						
							|  |  |  |             if type: | 
					
						
							|  |  |  |                 type = type.value | 
					
						
							|  |  |  |                 modinfo.appendChild(doc.createTextNode("\n    ")) | 
					
						
							|  |  |  |                 typenode = doc.createElement("type") | 
					
						
							|  |  |  |                 typenode.appendChild(doc.createTextNode(type)) | 
					
						
							|  |  |  |                 modinfo.appendChild(typenode) | 
					
						
							| 
									
										
										
										
											1999-01-29 22:12:29 +00:00
										 |  |  |         versionadded = extract_first_element(section, "versionadded") | 
					
						
							|  |  |  |         if versionadded: | 
					
						
							|  |  |  |             modinfo.setAttribute("added", versionadded.getAttribute("version")) | 
					
						
							| 
									
										
										
										
											1998-12-10 20:25:30 +00:00
										 |  |  |         title = get_first_element(section, "title") | 
					
						
							|  |  |  |         if title: | 
					
						
							|  |  |  |             children = title.childNodes | 
					
						
							|  |  |  |             if len(children) >= 2 \ | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |                and children[0].nodeName == "module" \ | 
					
						
							| 
									
										
										
										
											1998-12-10 20:25:30 +00:00
										 |  |  |                and children[0].childNodes[0].data == name: | 
					
						
							|  |  |  |                 # this is it; morph the <title> into <short-synopsis> | 
					
						
							|  |  |  |                 first_data = children[1] | 
					
						
							|  |  |  |                 if first_data.data[:4] == " ---": | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |                     first_data.data = first_data.data[4:].lstrip() | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |                 set_tagName(title, "short-synopsis") | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |                 if children[-1].nodeType == TEXT \ | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |                    and children[-1].data[-1:] == ".": | 
					
						
							| 
									
										
										
										
											1998-12-10 20:25:30 +00:00
										 |  |  |                     children[-1].data = children[-1].data[:-1] | 
					
						
							|  |  |  |                 section.removeChild(title) | 
					
						
							|  |  |  |                 section.removeChild(section.childNodes[0]) | 
					
						
							|  |  |  |                 title.removeChild(children[0]) | 
					
						
							|  |  |  |                 modinfo_pos = 0 | 
					
						
							|  |  |  |             else: | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |                 ewrite("module name in title doesn't match" | 
					
						
							|  |  |  |                        " <declaremodule/>; no <short-synopsis/>\n") | 
					
						
							| 
									
										
										
										
											1998-12-10 20:25:30 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |             ewrite("Unexpected condition: <section/> without <title/>\n") | 
					
						
							| 
									
										
										
										
											1998-12-10 20:25:30 +00:00
										 |  |  |         modinfo.appendChild(doc.createTextNode("\n    ")) | 
					
						
							|  |  |  |         modinfo.appendChild(node) | 
					
						
							|  |  |  |         if title and not contents_match(title, node): | 
					
						
							|  |  |  |             # The short synopsis is actually different, | 
					
						
							|  |  |  |             # and needs to be stored: | 
					
						
							|  |  |  |             modinfo.appendChild(doc.createTextNode("\n    ")) | 
					
						
							|  |  |  |             modinfo.appendChild(title) | 
					
						
							| 
									
										
										
										
											1999-01-19 23:09:31 +00:00
										 |  |  |         if modauthor: | 
					
						
							|  |  |  |             modinfo.appendChild(doc.createTextNode("\n    ")) | 
					
						
							|  |  |  |             modinfo.appendChild(modauthor) | 
					
						
							| 
									
										
										
										
											1999-03-11 17:35:12 +00:00
										 |  |  |         if platform: | 
					
						
							|  |  |  |             modinfo.appendChild(doc.createTextNode("\n    ")) | 
					
						
							|  |  |  |             modinfo.appendChild(platform) | 
					
						
							| 
									
										
										
										
											1998-12-10 20:25:30 +00:00
										 |  |  |         modinfo.appendChild(doc.createTextNode("\n  ")) | 
					
						
							|  |  |  |         section.insertBefore(modinfo, section.childNodes[modinfo_pos]) | 
					
						
							|  |  |  |         section.insertBefore(doc.createTextNode("\n  "), modinfo) | 
					
						
							| 
									
										
										
										
											1999-03-11 17:35:12 +00:00
										 |  |  |         # | 
					
						
							|  |  |  |         # The rest of this removes extra newlines from where we cut out | 
					
						
							|  |  |  |         # a lot of elements.  A lot of code for minimal value, but keeps | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |         # keeps the generated *ML from being too funny looking. | 
					
						
							| 
									
										
										
										
											1999-03-11 17:35:12 +00:00
										 |  |  |         # | 
					
						
							|  |  |  |         section.normalize() | 
					
						
							|  |  |  |         children = section.childNodes | 
					
						
							|  |  |  |         for i in range(len(children)): | 
					
						
							|  |  |  |             node = children[i] | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |             if node.nodeName == "moduleinfo": | 
					
						
							| 
									
										
										
										
											1999-03-11 17:35:12 +00:00
										 |  |  |                 nextnode = children[i+1] | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |                 if nextnode.nodeType == TEXT: | 
					
						
							| 
									
										
										
										
											1999-03-11 17:35:12 +00:00
										 |  |  |                     data = nextnode.data | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |                     s = data.lstrip() | 
					
						
							|  |  |  |                     if len(s) < (len(data) - 4): | 
					
						
							|  |  |  |                         nextnode.data = "\n\n\n" + s | 
					
						
							| 
									
										
										
										
											1998-12-10 20:25:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  | def cleanup_synopses(doc, fragment): | 
					
						
							|  |  |  |     for node in find_all_elements(fragment, "section"): | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |         create_module_info(doc, node) | 
					
						
							| 
									
										
										
										
											1998-12-10 20:25:30 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  | def fixup_table_structures(doc, fragment): | 
					
						
							|  |  |  |     for table in find_all_elements(fragment, "table"): | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |         fixup_table(doc, table) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-14 19:45:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def fixup_table(doc, table): | 
					
						
							|  |  |  |     # create the table head | 
					
						
							|  |  |  |     thead = doc.createElement("thead") | 
					
						
							|  |  |  |     row = doc.createElement("row") | 
					
						
							|  |  |  |     move_elements_by_name(doc, table, row, "entry") | 
					
						
							|  |  |  |     thead.appendChild(doc.createTextNode("\n    ")) | 
					
						
							|  |  |  |     thead.appendChild(row) | 
					
						
							|  |  |  |     thead.appendChild(doc.createTextNode("\n    ")) | 
					
						
							|  |  |  |     # create the table body | 
					
						
							|  |  |  |     tbody = doc.createElement("tbody") | 
					
						
							|  |  |  |     prev_row = None | 
					
						
							|  |  |  |     last_was_hline = 0 | 
					
						
							|  |  |  |     children = table.childNodes | 
					
						
							|  |  |  |     for child in children: | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         if child.nodeType == ELEMENT: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |             tagName = child.tagName | 
					
						
							| 
									
										
										
										
											1999-01-14 19:45:38 +00:00
										 |  |  |             if tagName == "hline" and prev_row is not None: | 
					
						
							|  |  |  |                 prev_row.setAttribute("rowsep", "1") | 
					
						
							|  |  |  |             elif tagName == "row": | 
					
						
							|  |  |  |                 prev_row = child | 
					
						
							|  |  |  |     # save the rows: | 
					
						
							|  |  |  |     tbody.appendChild(doc.createTextNode("\n    ")) | 
					
						
							|  |  |  |     move_elements_by_name(doc, table, tbody, "row", sep="\n    ") | 
					
						
							|  |  |  |     # and toss the rest: | 
					
						
							|  |  |  |     while children: | 
					
						
							|  |  |  |         child = children[0] | 
					
						
							|  |  |  |         nodeType = child.nodeType | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         if nodeType == TEXT: | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |             if child.data.strip(): | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |                 raise ConversionError("unexpected free data in <%s>: %r" | 
					
						
							|  |  |  |                                       % (table.tagName, child.data)) | 
					
						
							| 
									
										
										
										
											1999-01-14 19:45:38 +00:00
										 |  |  |             table.removeChild(child) | 
					
						
							|  |  |  |             continue | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         if nodeType == ELEMENT: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |             if child.tagName != "hline": | 
					
						
							| 
									
										
										
										
											1999-01-14 19:45:38 +00:00
										 |  |  |                 raise ConversionError( | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |                     "unexpected <%s> in table" % child.tagName) | 
					
						
							| 
									
										
										
										
											1999-01-14 19:45:38 +00:00
										 |  |  |             table.removeChild(child) | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  |         raise ConversionError( | 
					
						
							|  |  |  |             "unexpected %s node in table" % child.__class__.__name__) | 
					
						
							|  |  |  |     # nothing left in the <table>; add the <thead> and <tbody> | 
					
						
							|  |  |  |     tgroup = doc.createElement("tgroup") | 
					
						
							|  |  |  |     tgroup.appendChild(doc.createTextNode("\n  ")) | 
					
						
							|  |  |  |     tgroup.appendChild(thead) | 
					
						
							|  |  |  |     tgroup.appendChild(doc.createTextNode("\n  ")) | 
					
						
							|  |  |  |     tgroup.appendChild(tbody) | 
					
						
							|  |  |  |     tgroup.appendChild(doc.createTextNode("\n  ")) | 
					
						
							|  |  |  |     table.appendChild(tgroup) | 
					
						
							|  |  |  |     # now make the <entry>s look nice: | 
					
						
							|  |  |  |     for row in table.getElementsByTagName("row"): | 
					
						
							|  |  |  |         fixup_row(doc, row) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def fixup_row(doc, row): | 
					
						
							|  |  |  |     entries = [] | 
					
						
							|  |  |  |     map(entries.append, row.childNodes[1:]) | 
					
						
							|  |  |  |     for entry in entries: | 
					
						
							|  |  |  |         row.insertBefore(doc.createTextNode("\n         "), entry) | 
					
						
							|  |  |  | #    row.appendChild(doc.createTextNode("\n      ")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def move_elements_by_name(doc, source, dest, name, sep=None): | 
					
						
							|  |  |  |     nodes = [] | 
					
						
							|  |  |  |     for child in source.childNodes: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         if child.nodeName == name: | 
					
						
							| 
									
										
										
										
											1999-01-14 19:45:38 +00:00
										 |  |  |             nodes.append(child) | 
					
						
							|  |  |  |     for node in nodes: | 
					
						
							|  |  |  |         source.removeChild(node) | 
					
						
							|  |  |  |         dest.appendChild(node) | 
					
						
							|  |  |  |         if sep: | 
					
						
							|  |  |  |             dest.appendChild(doc.createTextNode(sep)) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  | RECURSE_INTO_PARA_CONTAINERS = ( | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     "chapter", "abstract", "enumerate", | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |     "section", "subsection", "subsubsection", | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     "paragraph", "subparagraph", "back-matter", | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     "howto", "manual", | 
					
						
							| 
									
										
										
										
											1999-08-03 15:32:48 +00:00
										 |  |  |     "item", "itemize", "fulllineitems", "enumeration", "descriptionlist", | 
					
						
							|  |  |  |     "definitionlist", "definition", | 
					
						
							| 
									
										
										
										
											1999-01-19 23:09:31 +00:00
										 |  |  |     ) | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | PARA_LEVEL_ELEMENTS = ( | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     "moduleinfo", "title", "verbatim", "enumerate", "item", | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     "interpreter-session", "back-matter", "interactive-session", | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     "opcodedesc", "classdesc", "datadesc", | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |     "cfuncdesc", "ctypedesc", "cvardesc", | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     "funcdesc", "methoddesc", "excdesc", "memberdesc", "membderdescni", | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |     "funcdescni", "methoddescni", "excdescni", | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |     "tableii", "tableiii", "tableiv", "localmoduletable", | 
					
						
							| 
									
										
										
										
											1999-08-03 15:32:48 +00:00
										 |  |  |     "sectionauthor", "seealso", "itemize", | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |     # include <para>, so we can just do it again to get subsequent paras: | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     PARA_ELEMENT, | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PARA_LEVEL_PRECEEDERS = ( | 
					
						
							| 
									
										
										
										
											2001-03-29 23:31:22 +00:00
										 |  |  |     "setindexsubitem", "author", | 
					
						
							| 
									
										
										
										
											2001-09-27 15:49:23 +00:00
										 |  |  |     "stindex", "obindex", "COMMENT", "label", "xi:include", "title", | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     "versionadded", "versionchanged", "declaremodule", "modulesynopsis", | 
					
						
							| 
									
										
										
										
											1999-08-03 15:32:48 +00:00
										 |  |  |     "moduleauthor", "indexterm", "leader", | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |     ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  | def fixup_paras(doc, fragment): | 
					
						
							|  |  |  |     for child in fragment.childNodes: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         if child.nodeName in RECURSE_INTO_PARA_CONTAINERS: | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |             fixup_paras_helper(doc, child) | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |     descriptions = find_all_elements(fragment, "description") | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     for description in descriptions: | 
					
						
							|  |  |  |         fixup_paras_helper(doc, description) | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  | def fixup_paras_helper(doc, container, depth=0): | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |     # document is already normalized | 
					
						
							|  |  |  |     children = container.childNodes | 
					
						
							| 
									
										
										
										
											1999-08-20 14:28:29 +00:00
										 |  |  |     start = skip_leading_nodes(children) | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |     while len(children) > start: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         if children[start].nodeName in RECURSE_INTO_PARA_CONTAINERS: | 
					
						
							| 
									
										
										
										
											1999-08-20 14:28:29 +00:00
										 |  |  |             # Something to recurse into: | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |             fixup_paras_helper(doc, children[start]) | 
					
						
							| 
									
										
										
										
											1999-08-20 14:28:29 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             # Paragraph material: | 
					
						
							|  |  |  |             build_para(doc, container, start, len(children)) | 
					
						
							|  |  |  |             if DEBUG_PARA_FIXER and depth == 10: | 
					
						
							|  |  |  |                 sys.exit(1) | 
					
						
							|  |  |  |         start = skip_leading_nodes(children, start + 1) | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def build_para(doc, parent, start, i): | 
					
						
							|  |  |  |     children = parent.childNodes | 
					
						
							|  |  |  |     after = start + 1 | 
					
						
							|  |  |  |     have_last = 0 | 
					
						
							| 
									
										
										
										
											1999-01-29 20:55:07 +00:00
										 |  |  |     BREAK_ELEMENTS = PARA_LEVEL_ELEMENTS + RECURSE_INTO_PARA_CONTAINERS | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |     # Collect all children until \n\n+ is found in a text node or a | 
					
						
							|  |  |  |     # member of BREAK_ELEMENTS is found. | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |     for j in range(start, i): | 
					
						
							|  |  |  |         after = j + 1 | 
					
						
							|  |  |  |         child = children[j] | 
					
						
							|  |  |  |         nodeType = child.nodeType | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         if nodeType == ELEMENT: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |             if child.tagName in BREAK_ELEMENTS: | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |                 after = j | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         elif nodeType == TEXT: | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |             pos = child.data.find("\n\n") | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |             if pos == 0: | 
					
						
							|  |  |  |                 after = j | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             if pos >= 1: | 
					
						
							|  |  |  |                 child.splitText(pos) | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         have_last = 1 | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |     if (start + 1) > after: | 
					
						
							|  |  |  |         raise ConversionError( | 
					
						
							|  |  |  |             "build_para() could not identify content to turn into a paragraph") | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |     if children[after - 1].nodeType == TEXT: | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |         # we may need to split off trailing white space: | 
					
						
							|  |  |  |         child = children[after - 1] | 
					
						
							|  |  |  |         data = child.data | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |         if data.rstrip() != data: | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |             have_last = 0 | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |             child.splitText(len(data.rstrip())) | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     para = doc.createElement(PARA_ELEMENT) | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |     prev = None | 
					
						
							|  |  |  |     indexes = range(start, after) | 
					
						
							|  |  |  |     indexes.reverse() | 
					
						
							|  |  |  |     for j in indexes: | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |         node = parent.childNodes[j] | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |         parent.removeChild(node) | 
					
						
							|  |  |  |         para.insertBefore(node, prev) | 
					
						
							|  |  |  |         prev = node | 
					
						
							|  |  |  |     if have_last: | 
					
						
							|  |  |  |         parent.appendChild(para) | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |         parent.appendChild(doc.createTextNode("\n\n")) | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |         return len(parent.childNodes) | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |     else: | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |         nextnode = parent.childNodes[start] | 
					
						
							|  |  |  |         if nextnode.nodeType == TEXT: | 
					
						
							|  |  |  |             if nextnode.data and nextnode.data[0] != "\n": | 
					
						
							|  |  |  |                 nextnode.data = "\n" + nextnode.data | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             newnode = doc.createTextNode("\n") | 
					
						
							|  |  |  |             parent.insertBefore(newnode, nextnode) | 
					
						
							|  |  |  |             nextnode = newnode | 
					
						
							|  |  |  |             start = start + 1 | 
					
						
							|  |  |  |         parent.insertBefore(para, nextnode) | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |         return start + 1 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-08-20 14:28:29 +00:00
										 |  |  | def skip_leading_nodes(children, start=0): | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |     """Return index into children of a node at which paragraph building should
 | 
					
						
							|  |  |  |     begin or a recursive call to fixup_paras_helper() should be made (for | 
					
						
							|  |  |  |     subsections, etc.). | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |     When the return value >= len(children), we've built all the paras we can | 
					
						
							|  |  |  |     from this list of children. | 
					
						
							|  |  |  |     """
 | 
					
						
							|  |  |  |     i = len(children) | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |     while i > start: | 
					
						
							|  |  |  |         # skip over leading comments and whitespace: | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |         child = children[start] | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |         nodeType = child.nodeType | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         if nodeType == TEXT: | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |             data = child.data | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |             shortened = data.lstrip() | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |             if shortened: | 
					
						
							|  |  |  |                 if data != shortened: | 
					
						
							|  |  |  |                     # break into two nodes: whitespace and non-whitespace | 
					
						
							|  |  |  |                     child.splitText(len(data) - len(shortened)) | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |                     return start + 1 | 
					
						
							|  |  |  |                 return start | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  |             # all whitespace, just skip | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         elif nodeType == ELEMENT: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |             tagName = child.tagName | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |             if tagName in RECURSE_INTO_PARA_CONTAINERS: | 
					
						
							|  |  |  |                 return start | 
					
						
							|  |  |  |             if tagName not in PARA_LEVEL_ELEMENTS + PARA_LEVEL_PRECEEDERS: | 
					
						
							|  |  |  |                 return start | 
					
						
							|  |  |  |         start = start + 1 | 
					
						
							|  |  |  |     return start | 
					
						
							| 
									
										
										
										
											1998-12-10 05:07:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  | def fixup_rfc_references(doc, fragment): | 
					
						
							| 
									
										
										
										
											2001-09-29 19:07:22 +00:00
										 |  |  |     for rfcnode in find_all_elements_from_set(fragment, ("pep", "rfc")): | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |         rfcnode.appendChild(doc.createTextNode( | 
					
						
							| 
									
										
										
										
											2001-09-29 19:07:22 +00:00
										 |  |  |             rfcnode.tagName.upper() + " " + rfcnode.getAttribute("num"))) | 
					
						
							| 
									
										
										
										
											1999-01-14 21:18:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  | def fixup_signatures(doc, fragment): | 
					
						
							|  |  |  |     for child in fragment.childNodes: | 
					
						
							|  |  |  |         if child.nodeType == ELEMENT: | 
					
						
							| 
									
										
										
										
											1999-01-14 21:18:03 +00:00
										 |  |  |             args = child.getElementsByTagName("args") | 
					
						
							|  |  |  |             for arg in args: | 
					
						
							| 
									
										
										
										
											2001-09-29 05:05:25 +00:00
										 |  |  |                 rewrite_args(doc, arg) | 
					
						
							| 
									
										
										
										
											1999-01-14 21:18:03 +00:00
										 |  |  |             args = child.getElementsByTagName("constructor-args") | 
					
						
							|  |  |  |             for arg in args: | 
					
						
							| 
									
										
										
										
											2001-09-29 05:05:25 +00:00
										 |  |  |                 rewrite_args(doc, arg) | 
					
						
							| 
									
										
										
										
											1999-01-14 21:18:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-29 05:05:25 +00:00
										 |  |  | def rewrite_args(doc, arglist): | 
					
						
							|  |  |  |     fixup_args(doc, arglist) | 
					
						
							|  |  |  |     arglist.normalize() | 
					
						
							|  |  |  |     if arglist.childNodes.length == 1 and arglist.firstChild.nodeType == TEXT: | 
					
						
							|  |  |  |         node = arglist.firstChild | 
					
						
							|  |  |  |         node.data = ' '.join(node.data.split()) | 
					
						
							| 
									
										
										
										
											1999-01-14 21:18:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def fixup_args(doc, arglist): | 
					
						
							|  |  |  |     for child in arglist.childNodes: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         if child.nodeName == "optional": | 
					
						
							| 
									
										
										
										
											1999-01-14 21:18:03 +00:00
										 |  |  |             # found it; fix and return | 
					
						
							|  |  |  |             arglist.insertBefore(doc.createTextNode("["), child) | 
					
						
							|  |  |  |             optkids = child.childNodes | 
					
						
							|  |  |  |             while optkids: | 
					
						
							| 
									
										
										
										
											2001-09-29 05:05:25 +00:00
										 |  |  |                 arglist.insertBefore(child.firstChild, child) | 
					
						
							| 
									
										
										
										
											1999-01-14 21:18:03 +00:00
										 |  |  |             arglist.insertBefore(doc.createTextNode("]"), child) | 
					
						
							|  |  |  |             arglist.removeChild(child) | 
					
						
							|  |  |  |             return fixup_args(doc, arglist) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  | def fixup_sectionauthors(doc, fragment): | 
					
						
							|  |  |  |     for sectauth in find_all_elements(fragment, "sectionauthor"): | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |         section = sectauth.parentNode | 
					
						
							|  |  |  |         section.removeChild(sectauth) | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         set_tagName(sectauth, "author") | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |         sectauth.appendChild(doc.createTextNode( | 
					
						
							|  |  |  |             sectauth.getAttribute("name"))) | 
					
						
							|  |  |  |         sectauth.removeAttribute("name") | 
					
						
							|  |  |  |         after = section.childNodes[2] | 
					
						
							|  |  |  |         title = section.childNodes[1] | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         if title.nodeName != "title": | 
					
						
							| 
									
										
										
										
											1999-01-28 23:59:58 +00:00
										 |  |  |             after = section.childNodes[0] | 
					
						
							|  |  |  |         section.insertBefore(doc.createTextNode("\n  "), after) | 
					
						
							|  |  |  |         section.insertBefore(sectauth, after) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-02-18 16:32:21 +00:00
										 |  |  | def fixup_verbatims(doc): | 
					
						
							|  |  |  |     for verbatim in find_all_elements(doc, "verbatim"): | 
					
						
							|  |  |  |         child = verbatim.childNodes[0] | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         if child.nodeType == TEXT \ | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |            and child.data.lstrip().startswith(">>>"): | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |             set_tagName(verbatim, "interactive-session") | 
					
						
							| 
									
										
										
										
											1999-02-18 16:32:21 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  | def add_node_ids(fragment, counter=0): | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |     fragment.node_id = counter | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     for node in fragment.childNodes: | 
					
						
							|  |  |  |         counter = counter + 1 | 
					
						
							|  |  |  |         if node.nodeType == ELEMENT: | 
					
						
							|  |  |  |             counter = add_node_ids(node, counter) | 
					
						
							|  |  |  |         else: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |             node.node_id = counter | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     return counter + 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-27 16:52:22 +00:00
										 |  |  | def fixup_ulink(doc, fragment): | 
					
						
							|  |  |  |     for ulink in find_all_elements(fragment, "ulink"): | 
					
						
							|  |  |  |         children = ulink.childNodes | 
					
						
							|  |  |  |         assert len(children) == 2 | 
					
						
							|  |  |  |         text = children[0] | 
					
						
							|  |  |  |         href = children[1] | 
					
						
							|  |  |  |         href.normalize() | 
					
						
							|  |  |  |         assert len(href.childNodes) == 1 | 
					
						
							|  |  |  |         assert href.childNodes[0].nodeType == TEXT | 
					
						
							|  |  |  |         url = href.childNodes[0].data | 
					
						
							|  |  |  |         ulink.setAttribute("href", url) | 
					
						
							|  |  |  |         ulink.removeChild(href) | 
					
						
							|  |  |  |         content = text.childNodes | 
					
						
							|  |  |  |         while len(content): | 
					
						
							|  |  |  |             ulink.appendChild(content[0]) | 
					
						
							|  |  |  |         ulink.removeChild(text) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  | REFMODINDEX_ELEMENTS = ('refmodindex', 'refbimodindex', | 
					
						
							|  |  |  |                         'refexmodindex', 'refstmodindex') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def fixup_refmodindexes(fragment): | 
					
						
							|  |  |  |     # Locate <ref*modindex>...</> co-located with <module>...</>, and | 
					
						
							|  |  |  |     # remove the <ref*modindex>, replacing it with index=index on the | 
					
						
							|  |  |  |     # <module> element. | 
					
						
							|  |  |  |     nodes = find_all_elements_from_set(fragment, REFMODINDEX_ELEMENTS) | 
					
						
							|  |  |  |     d = {} | 
					
						
							|  |  |  |     for node in nodes: | 
					
						
							|  |  |  |         parent = node.parentNode | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         d[parent.node_id] = parent | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     del nodes | 
					
						
							|  |  |  |     map(fixup_refmodindexes_chunk, d.values()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def fixup_refmodindexes_chunk(container): | 
					
						
							|  |  |  |     # node is probably a <para>; let's see how often it isn't: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |     if container.tagName != PARA_ELEMENT: | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |         bwrite("--- fixup_refmodindexes_chunk(%s)\n" % container) | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     module_entries = find_all_elements(container, "module") | 
					
						
							|  |  |  |     if not module_entries: | 
					
						
							|  |  |  |         return | 
					
						
							|  |  |  |     index_entries = find_all_elements_from_set(container, REFMODINDEX_ELEMENTS) | 
					
						
							|  |  |  |     removes = [] | 
					
						
							|  |  |  |     for entry in index_entries: | 
					
						
							|  |  |  |         children = entry.childNodes | 
					
						
							|  |  |  |         if len(children) != 0: | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |             bwrite("--- unexpected number of children for %s node:\n" | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |                    % entry.tagName) | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |             ewrite(entry.toxml() + "\n") | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |             continue | 
					
						
							|  |  |  |         found = 0 | 
					
						
							| 
									
										
										
										
											1999-08-03 15:32:48 +00:00
										 |  |  |         module_name = entry.getAttribute("module") | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |         for node in module_entries: | 
					
						
							|  |  |  |             if len(node.childNodes) != 1: | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             this_name = node.childNodes[0].data | 
					
						
							|  |  |  |             if this_name == module_name: | 
					
						
							|  |  |  |                 found = 1 | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |                 node.setAttribute("index", "yes") | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |         if found: | 
					
						
							|  |  |  |             removes.append(entry) | 
					
						
							|  |  |  |     for node in removes: | 
					
						
							|  |  |  |         container.removeChild(node) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def fixup_bifuncindexes(fragment): | 
					
						
							|  |  |  |     nodes = find_all_elements(fragment, 'bifuncindex') | 
					
						
							|  |  |  |     d = {} | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |     # make sure that each parent is only processed once: | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     for node in nodes: | 
					
						
							|  |  |  |         parent = node.parentNode | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         d[parent.node_id] = parent | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     del nodes | 
					
						
							|  |  |  |     map(fixup_bifuncindexes_chunk, d.values()) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def fixup_bifuncindexes_chunk(container): | 
					
						
							|  |  |  |     removes = [] | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |     entries = find_all_child_elements(container, "bifuncindex") | 
					
						
							|  |  |  |     function_entries = find_all_child_elements(container, "function") | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     for entry in entries: | 
					
						
							|  |  |  |         function_name = entry.getAttribute("name") | 
					
						
							|  |  |  |         found = 0 | 
					
						
							|  |  |  |         for func_entry in function_entries: | 
					
						
							|  |  |  |             t2 = func_entry.childNodes[0].data | 
					
						
							|  |  |  |             if t2[-2:] != "()": | 
					
						
							|  |  |  |                 continue | 
					
						
							|  |  |  |             t2 = t2[:-2] | 
					
						
							|  |  |  |             if t2 == function_name: | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |                 func_entry.setAttribute("index", "yes") | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |                 func_entry.setAttribute("module", "__builtin__") | 
					
						
							|  |  |  |                 if not found: | 
					
						
							|  |  |  |                     found = 1 | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |                     removes.append(entry) | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     for entry in removes: | 
					
						
							|  |  |  |         container.removeChild(entry) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-11-23 21:52:03 +00:00
										 |  |  | def join_adjacent_elements(container, gi): | 
					
						
							|  |  |  |     queue = [container] | 
					
						
							|  |  |  |     while queue: | 
					
						
							|  |  |  |         parent = queue.pop() | 
					
						
							|  |  |  |         i = 0 | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         children = parent.childNodes | 
					
						
							| 
									
										
										
										
											1999-11-23 21:52:03 +00:00
										 |  |  |         nchildren = len(children) | 
					
						
							|  |  |  |         while i < (nchildren - 1): | 
					
						
							|  |  |  |             child = children[i] | 
					
						
							|  |  |  |             if child.nodeName == gi: | 
					
						
							|  |  |  |                 if children[i+1].nodeName == gi: | 
					
						
							|  |  |  |                     ewrite("--- merging two <%s/> elements\n" % gi) | 
					
						
							|  |  |  |                     child = children[i] | 
					
						
							|  |  |  |                     nextchild = children[i+1] | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |                     nextchildren = nextchild.childNodes | 
					
						
							| 
									
										
										
										
											1999-11-23 21:52:03 +00:00
										 |  |  |                     while len(nextchildren): | 
					
						
							|  |  |  |                         node = nextchildren[0] | 
					
						
							|  |  |  |                         nextchild.removeChild(node) | 
					
						
							|  |  |  |                         child.appendChild(node) | 
					
						
							|  |  |  |                     parent.removeChild(nextchild) | 
					
						
							|  |  |  |                     continue | 
					
						
							|  |  |  |             if child.nodeType == ELEMENT: | 
					
						
							|  |  |  |                 queue.append(child) | 
					
						
							|  |  |  |             i = i + 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  | _token_rx = re.compile(r"[a-zA-Z][a-zA-Z0-9.-]*$") | 
					
						
							| 
									
										
										
										
											1999-01-06 22:50:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  | def write_esis(doc, ofp, knownempty): | 
					
						
							|  |  |  |     for node in doc.childNodes: | 
					
						
							|  |  |  |         nodeType = node.nodeType | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         if nodeType == ELEMENT: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |             gi = node.tagName | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  |             if knownempty(gi): | 
					
						
							|  |  |  |                 if node.hasChildNodes(): | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |                     raise ValueError, \ | 
					
						
							|  |  |  |                           "declared-empty node <%s> has children" % gi | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  |                 ofp.write("e\n") | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |             for k, value in node.attributes.items(): | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  |                 if _token_rx.match(value): | 
					
						
							|  |  |  |                     dtype = "TOKEN" | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     dtype = "CDATA" | 
					
						
							|  |  |  |                 ofp.write("A%s %s %s\n" % (k, dtype, esistools.encode(value))) | 
					
						
							|  |  |  |             ofp.write("(%s\n" % gi) | 
					
						
							|  |  |  |             write_esis(node, ofp, knownempty) | 
					
						
							|  |  |  |             ofp.write(")%s\n" % gi) | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         elif nodeType == TEXT: | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  |             ofp.write("-%s\n" % esistools.encode(node.data)) | 
					
						
							| 
									
										
										
										
											1999-08-26 17:51:56 +00:00
										 |  |  |         elif nodeType == ENTITY_REFERENCE: | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |             ofp.write("&%s\n" % node.nodeName) | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  |         else: | 
					
						
							|  |  |  |             raise RuntimeError, "unsupported node type: %s" % nodeType | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  | def convert(ifp, ofp): | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |     events = esistools.parse(ifp) | 
					
						
							|  |  |  |     toktype, doc = events.getEvent() | 
					
						
							|  |  |  |     fragment = doc.createDocumentFragment() | 
					
						
							|  |  |  |     events.expandNode(fragment) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |     normalize(fragment) | 
					
						
							|  |  |  |     simplify(doc, fragment) | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     handle_labels(doc, fragment) | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |     handle_appendix(doc, fragment) | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |     fixup_trailing_whitespace(doc, fragment, { | 
					
						
							|  |  |  |         # element -> (before-end-tag, after-end-tag) | 
					
						
							|  |  |  |         "abstract": ("\n", "\n"), | 
					
						
							|  |  |  |         "title": ("", "\n"), | 
					
						
							|  |  |  |         "chapter": ("\n", "\n\n\n"), | 
					
						
							|  |  |  |         "section": ("\n", "\n\n\n"), | 
					
						
							|  |  |  |         "subsection": ("\n", "\n\n"), | 
					
						
							|  |  |  |         "subsubsection": ("\n", "\n\n"), | 
					
						
							|  |  |  |         "paragraph": ("\n", "\n\n"), | 
					
						
							|  |  |  |         "subparagraph": ("\n", "\n\n"), | 
					
						
							| 
									
										
										
										
											2001-11-30 19:25:39 +00:00
										 |  |  |         "description": ("\n", "\n\n"), | 
					
						
							| 
									
										
										
										
											2001-09-28 17:14:35 +00:00
										 |  |  |         "enumeration": ("\n", "\n\n"), | 
					
						
							| 
									
										
										
										
											2001-11-30 19:25:39 +00:00
										 |  |  |         "item": ("\n", "\n\n"), | 
					
						
							| 
									
										
										
										
											1998-11-23 23:10:35 +00:00
										 |  |  |         }) | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  |     cleanup_root_text(doc) | 
					
						
							| 
									
										
										
										
											1999-08-02 14:46:15 +00:00
										 |  |  |     cleanup_trailing_parens(fragment, ["function", "method", "cfunction"]) | 
					
						
							|  |  |  |     cleanup_synopses(doc, fragment) | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |     fixup_descriptors(doc, fragment) | 
					
						
							|  |  |  |     fixup_verbatims(fragment) | 
					
						
							|  |  |  |     normalize(fragment) | 
					
						
							|  |  |  |     fixup_paras(doc, fragment) | 
					
						
							|  |  |  |     fixup_sectionauthors(doc, fragment) | 
					
						
							|  |  |  |     fixup_table_structures(doc, fragment) | 
					
						
							|  |  |  |     fixup_rfc_references(doc, fragment) | 
					
						
							|  |  |  |     fixup_signatures(doc, fragment) | 
					
						
							| 
									
										
										
										
											2001-09-27 16:52:22 +00:00
										 |  |  |     fixup_ulink(doc, fragment) | 
					
						
							| 
									
										
										
										
											1999-07-29 22:23:19 +00:00
										 |  |  |     add_node_ids(fragment) | 
					
						
							|  |  |  |     fixup_refmodindexes(fragment) | 
					
						
							|  |  |  |     fixup_bifuncindexes(fragment) | 
					
						
							| 
									
										
										
										
											1999-11-23 21:52:03 +00:00
										 |  |  |     # Take care of ugly hacks in the LaTeX markup to avoid LaTeX and | 
					
						
							|  |  |  |     # LaTeX2HTML screwing with GNU-style long options (the '--' problem). | 
					
						
							|  |  |  |     join_adjacent_elements(fragment, "option") | 
					
						
							| 
									
										
										
										
											2001-11-19 05:28:29 +00:00
										 |  |  |     # Attempt to avoid trailing blank lines: | 
					
						
							|  |  |  |     fragment.normalize() | 
					
						
							|  |  |  |     if fragment.lastChild.data[-1:] == "\n": | 
					
						
							|  |  |  |         fragment.lastChild.data = fragment.lastChild.data.rstrip() + "\n" | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  |     # | 
					
						
							|  |  |  |     d = {} | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |     for gi in events.parser.get_empties(): | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  |         d[gi] = gi | 
					
						
							| 
									
										
										
										
											2001-09-29 19:07:22 +00:00
										 |  |  |     for key in ("author", "pep", "rfc"): | 
					
						
							|  |  |  |         if d.has_key(key): | 
					
						
							|  |  |  |             del d[key] | 
					
						
							| 
									
										
										
										
											1998-12-01 19:03:01 +00:00
										 |  |  |     knownempty = d.has_key | 
					
						
							|  |  |  |     # | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											1999-05-10 19:36:52 +00:00
										 |  |  |         write_esis(fragment, ofp, knownempty) | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  |     except IOError, (err, msg): | 
					
						
							|  |  |  |         # Ignore EPIPE; it just means that whoever we're writing to stopped | 
					
						
							|  |  |  |         # reading.  The rest of the output would be ignored.  All other errors | 
					
						
							|  |  |  |         # should still be reported, | 
					
						
							|  |  |  |         if err != errno.EPIPE: | 
					
						
							|  |  |  |             raise | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def main(): | 
					
						
							|  |  |  |     if len(sys.argv) == 1: | 
					
						
							|  |  |  |         ifp = sys.stdin | 
					
						
							|  |  |  |         ofp = sys.stdout | 
					
						
							|  |  |  |     elif len(sys.argv) == 2: | 
					
						
							|  |  |  |         ifp = open(sys.argv[1]) | 
					
						
							|  |  |  |         ofp = sys.stdout | 
					
						
							|  |  |  |     elif len(sys.argv) == 3: | 
					
						
							|  |  |  |         ifp = open(sys.argv[1]) | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |         import StringIO | 
					
						
							|  |  |  |         ofp = StringIO.StringIO() | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  |     else: | 
					
						
							|  |  |  |         usage() | 
					
						
							|  |  |  |         sys.exit(2) | 
					
						
							|  |  |  |     convert(ifp, ofp) | 
					
						
							| 
									
										
										
										
											2001-03-23 17:01:47 +00:00
										 |  |  |     if len(sys.argv) == 3: | 
					
						
							|  |  |  |         fp = open(sys.argv[2], "w") | 
					
						
							|  |  |  |         fp.write(ofp.getvalue()) | 
					
						
							|  |  |  |         fp.close() | 
					
						
							|  |  |  |         ofp.close() | 
					
						
							| 
									
										
										
										
											1998-11-23 17:02:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     main() |