From 1ff6db484f2d1389d16eb9ba10f637c3a1b2e0c9 Mon Sep 17 00:00:00 2001 From: Fred Drake Date: Mon, 23 Nov 1998 23:10:35 +0000 Subject: Add some additional cleanup transformations. --- Doc/tools/sgmlconv/docfixer.py | 67 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/Doc/tools/sgmlconv/docfixer.py b/Doc/tools/sgmlconv/docfixer.py index 00d4727..dff2076 100755 --- a/Doc/tools/sgmlconv/docfixer.py +++ b/Doc/tools/sgmlconv/docfixer.py @@ -153,15 +153,82 @@ def handle_labels(doc): parent.removeChild(label) +def fixup_trailing_whitespace(doc, wsmap): + queue = [doc] + while queue: + node = queue[0] + del queue[0] + if node.nodeType == xml.dom.core.ELEMENT \ + and wsmap.has_key(node.tagName): + ws = wsmap[node.tagName] + children = node.childNodes + children.reverse() + if children[0].nodeType == xml.dom.core.TEXT: + data = string.rstrip(children[0].data) + ws + children[0].data = data + children.reverse() + # hack to get the title in place: + if node.tagName == "title" \ + and node.parentNode.firstChild.nodeType == xml.dom.core.ELEMENT: + node.parentNode.insertBefore(doc.createText("\n "), + node.parentNode.firstChild) + for child in node.childNodes: + if child.nodeType == xml.dom.core.ELEMENT: + queue.append(child) + + +def normalize(doc): + for node in doc.childNodes: + if node.nodeType == xml.dom.core.ELEMENT: + node.normalize() + + +def cleanup_trailing_parens(doc, element_names): + d = {} + for gi in element_names: + d[gi] = gi + rewrite_element = d.has_key + queue = [] + for node in doc.childNodes: + if node.nodeType == xml.dom.core.ELEMENT: + queue.append(node) + while queue: + node = queue[0] + del queue[0] + if rewrite_element(node.tagName): + children = node.childNodes + if len(children) == 1 \ + and children[0].nodeType == xml.dom.core.TEXT: + data = children[0].data + if data[-2:] == "()": + children[0].data = data[:-2] + else: + for child in node.childNodes: + if child.nodeType == xml.dom.core.ELEMENT: + queue.append(child) + + def convert(ifp, ofp): p = xml.dom.esis_builder.EsisBuilder() p.feed(ifp.read()) doc = p.document + normalize(doc) handle_args(doc) handle_comments(doc) simplify(doc) handle_labels(doc) + fixup_trailing_whitespace(doc, { + "abstract": "\n", + "title": "", + "chapter": "\n\n", + "section": "\n\n", + "subsection": "\n\n", + "subsubsection": "\n\n", + "paragraph": "\n\n", + "subparagraph": "\n\n", + }) cleanup_root_text(doc) + cleanup_trailing_parens(doc, ["function", "method", "cfunction"]) try: ofp.write(doc.toxml()) ofp.write("\n") -- cgit v0.12