mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 03:44:55 +00:00 
			
		
		
		
	Use esistools.
Generate ESIS data instead of XML. Misc. changes to some transforms.
This commit is contained in:
		
							parent
							
								
									f077b9d61f
								
							
						
					
					
						commit
						4db5b4636c
					
				
					 1 changed files with 66 additions and 15 deletions
				
			
		| 
						 | 
					@ -12,6 +12,8 @@ __version__ = '$Revision$'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import errno
 | 
					import errno
 | 
				
			||||||
 | 
					import esistools
 | 
				
			||||||
 | 
					import re
 | 
				
			||||||
import string
 | 
					import string
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
import xml.dom.core
 | 
					import xml.dom.core
 | 
				
			||||||
| 
						 | 
					@ -93,7 +95,7 @@ def cleanup_root_text(doc):
 | 
				
			||||||
        skip = 0
 | 
					        skip = 0
 | 
				
			||||||
        if n.nodeType == xml.dom.core.TEXT and not prevskip:
 | 
					        if n.nodeType == xml.dom.core.TEXT and not prevskip:
 | 
				
			||||||
            discards.append(n)
 | 
					            discards.append(n)
 | 
				
			||||||
        elif n.nodeType == xml.dom.core.COMMENT:
 | 
					        elif n.nodeType == xml.dom.core.ELEMENT and n.tagName == "COMMENT":
 | 
				
			||||||
            skip = 1
 | 
					            skip = 1
 | 
				
			||||||
    for node in discards:
 | 
					    for node in discards:
 | 
				
			||||||
        doc.removeChild(node)
 | 
					        doc.removeChild(node)
 | 
				
			||||||
| 
						 | 
					@ -126,16 +128,33 @@ def handle_args(doc):
 | 
				
			||||||
    rewrite_desc_entries(doc, "constructor-args")
 | 
					    rewrite_desc_entries(doc, "constructor-args")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def handle_comments(doc, node=None):
 | 
					def handle_appendix(doc):
 | 
				
			||||||
    if node is None:
 | 
					    # must be called after simplfy() if document is multi-rooted to begin with
 | 
				
			||||||
        node = doc
 | 
					    docelem = doc.documentElement
 | 
				
			||||||
    for n in node.childNodes:
 | 
					    toplevel = docelem.tagName == "manual" and "chapter" or "section"
 | 
				
			||||||
        if n.nodeType == xml.dom.core.ELEMENT:
 | 
					    appendices = 0
 | 
				
			||||||
            if n.tagName == "COMMENT":
 | 
					    nodes = []
 | 
				
			||||||
                comment = doc.createComment(n.childNodes[0].data)
 | 
					    for node in docelem.childNodes:
 | 
				
			||||||
                node.replaceChild(comment, n)
 | 
					        if appendices:
 | 
				
			||||||
            else:
 | 
					            nodes.append(node)
 | 
				
			||||||
                handle_comments(doc, n)
 | 
					        elif node.nodeType == xml.dom.core.ELEMENT:
 | 
				
			||||||
 | 
					            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"))
 | 
				
			||||||
 | 
					        while nodes and nodes[0].nodeType == xml.dom.core.TEXT \
 | 
				
			||||||
 | 
					              and not string.strip(nodes[0].data):
 | 
				
			||||||
 | 
					            del nodes[0]
 | 
				
			||||||
 | 
					        map(back.appendChild, nodes)
 | 
				
			||||||
 | 
					        docelem.appendChild(doc.createTextNode("\n"))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def handle_labels(doc):
 | 
					def handle_labels(doc):
 | 
				
			||||||
| 
						 | 
					@ -208,15 +227,42 @@ def cleanup_trailing_parens(doc, element_names):
 | 
				
			||||||
                    queue.append(child)
 | 
					                    queue.append(child)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					_token_rx = re.compile(r"[a-zA-Z][a-zA-Z0-9.-]*$")
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					def write_esis(doc, ofp, knownempty):
 | 
				
			||||||
 | 
					    for node in doc.childNodes:
 | 
				
			||||||
 | 
					        nodeType = node.nodeType
 | 
				
			||||||
 | 
					        if nodeType == xml.dom.core.ELEMENT:
 | 
				
			||||||
 | 
					            gi = node.tagName
 | 
				
			||||||
 | 
					            if knownempty(gi):
 | 
				
			||||||
 | 
					                if node.hasChildNodes():
 | 
				
			||||||
 | 
					                    raise ValueError, "declared-empty node has children"
 | 
				
			||||||
 | 
					                ofp.write("e\n")
 | 
				
			||||||
 | 
					            for k, v in node.attributes.items():
 | 
				
			||||||
 | 
					                value = v.value
 | 
				
			||||||
 | 
					                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)
 | 
				
			||||||
 | 
					        elif nodeType == xml.dom.core.TEXT:
 | 
				
			||||||
 | 
					            ofp.write("-%s\n" % esistools.encode(node.data))
 | 
				
			||||||
 | 
					        else:
 | 
				
			||||||
 | 
					            raise RuntimeError, "unsupported node type: %s" % nodeType
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def convert(ifp, ofp):
 | 
					def convert(ifp, ofp):
 | 
				
			||||||
    p = xml.dom.esis_builder.EsisBuilder()
 | 
					    p = esistools.ExtendedEsisBuilder()
 | 
				
			||||||
    p.feed(ifp.read())
 | 
					    p.feed(ifp.read())
 | 
				
			||||||
    doc = p.document
 | 
					    doc = p.document
 | 
				
			||||||
    normalize(doc)
 | 
					    normalize(doc)
 | 
				
			||||||
    handle_args(doc)
 | 
					    handle_args(doc)
 | 
				
			||||||
    handle_comments(doc)
 | 
					 | 
				
			||||||
    simplify(doc)
 | 
					    simplify(doc)
 | 
				
			||||||
    handle_labels(doc)
 | 
					    handle_labels(doc)
 | 
				
			||||||
 | 
					    handle_appendix(doc)
 | 
				
			||||||
    fixup_trailing_whitespace(doc, {
 | 
					    fixup_trailing_whitespace(doc, {
 | 
				
			||||||
        "abstract": "\n",
 | 
					        "abstract": "\n",
 | 
				
			||||||
        "title": "",
 | 
					        "title": "",
 | 
				
			||||||
| 
						 | 
					@ -229,9 +275,14 @@ def convert(ifp, ofp):
 | 
				
			||||||
        })
 | 
					        })
 | 
				
			||||||
    cleanup_root_text(doc)
 | 
					    cleanup_root_text(doc)
 | 
				
			||||||
    cleanup_trailing_parens(doc, ["function", "method", "cfunction"])
 | 
					    cleanup_trailing_parens(doc, ["function", "method", "cfunction"])
 | 
				
			||||||
 | 
					    #
 | 
				
			||||||
 | 
					    d = {}
 | 
				
			||||||
 | 
					    for gi in p.get_empties():
 | 
				
			||||||
 | 
					        d[gi] = gi
 | 
				
			||||||
 | 
					    knownempty = d.has_key
 | 
				
			||||||
 | 
					    #
 | 
				
			||||||
    try:
 | 
					    try:
 | 
				
			||||||
        ofp.write(doc.toxml())
 | 
					        write_esis(doc, ofp, knownempty)
 | 
				
			||||||
        ofp.write("\n")
 | 
					 | 
				
			||||||
    except IOError, (err, msg):
 | 
					    except IOError, (err, msg):
 | 
				
			||||||
        # Ignore EPIPE; it just means that whoever we're writing to stopped
 | 
					        # Ignore EPIPE; it just means that whoever we're writing to stopped
 | 
				
			||||||
        # reading.  The rest of the output would be ignored.  All other errors
 | 
					        # reading.  The rest of the output would be ignored.  All other errors
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue