write_esis(): Added support for ENTITY_REFERENCE nodes.

This commit is contained in:
Fred Drake 1999-08-26 17:51:56 +00:00
parent 53eae8e81b
commit a20581c1e5
1 changed files with 44 additions and 40 deletions

View File

@ -15,6 +15,7 @@ import xml.dom.core
from xml.dom.core import \ from xml.dom.core import \
ELEMENT, \ ELEMENT, \
ENTITY_REFERENCE, \
TEXT TEXT
@ -74,7 +75,7 @@ xml.dom.core.Document.get_childNodes = get_childNodes
def get_first_element(doc, gi): def get_first_element(doc, gi):
for n in doc.childNodes: for n in doc.childNodes:
if n.nodeName == gi: if n.get_nodeName() == gi:
return n return n
def extract_first_element(doc, gi): def extract_first_element(doc, gi):
@ -86,11 +87,11 @@ def extract_first_element(doc, gi):
def find_all_elements(doc, gi): def find_all_elements(doc, gi):
nodes = [] nodes = []
if doc.nodeName == gi: if doc.get_nodeName() == gi:
nodes.append(doc) nodes.append(doc)
for child in doc.childNodes: for child in doc.childNodes:
if child.nodeType == ELEMENT: if child.nodeType == ELEMENT:
if child.tagName == gi: if child.get_tagName() == gi:
nodes.append(child) nodes.append(child)
for node in child.getElementsByTagName(gi): for node in child.getElementsByTagName(gi):
nodes.append(node) nodes.append(node)
@ -99,7 +100,7 @@ def find_all_elements(doc, gi):
def find_all_child_elements(doc, gi): def find_all_child_elements(doc, gi):
nodes = [] nodes = []
for child in doc.childNodes: for child in doc.childNodes:
if child.nodeName == gi: if child.get_nodeName() == gi:
nodes.append(child) nodes.append(child)
return nodes return nodes
@ -107,10 +108,10 @@ def find_all_elements_from_set(doc, gi_set):
return __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): def __find_all_elements_from_set(doc, gi_set, nodes):
if doc.nodeName in gi_set: if doc.get_nodeName() in gi_set:
nodes.append(doc) nodes.append(doc)
for child in doc.childNodes: for child in doc.childNodes:
if child.nodeType == ELEMENT: if child.get_nodeType() == ELEMENT:
__find_all_elements_from_set(child, gi_set, nodes) __find_all_elements_from_set(child, gi_set, nodes)
return nodes return nodes
@ -143,7 +144,7 @@ def simplify(doc, fragment):
docelem.insertBefore(text, docelem.firstChild) docelem.insertBefore(text, docelem.firstChild)
docelem.insertBefore(node, text) docelem.insertBefore(node, text)
docelem.insertBefore(doc.createTextNode("\n"), docelem.firstChild) docelem.insertBefore(doc.createTextNode("\n"), docelem.firstChild)
while fragment.firstChild and fragment.firstChild.nodeType == TEXT: while fragment.firstChild and fragment.firstChild.get_nodeType() == TEXT:
fragment.removeChild(fragment.firstChild) fragment.removeChild(fragment.firstChild)
@ -153,9 +154,9 @@ def cleanup_root_text(doc):
for n in doc.childNodes: for n in doc.childNodes:
prevskip = skip prevskip = skip
skip = 0 skip = 0
if n.nodeType == TEXT and not prevskip: if n.get_nodeType() == TEXT and not prevskip:
discards.append(n) discards.append(n)
elif n.nodeName == "COMMENT": elif n.get_nodeName() == "COMMENT":
skip = 1 skip = 1
for node in discards: for node in discards:
doc.removeChild(node) doc.removeChild(node)
@ -177,8 +178,8 @@ def fixup_descriptors(doc, fragment):
def find_and_fix_descriptors(doc, container): def find_and_fix_descriptors(doc, container):
children = container.childNodes children = container.childNodes
for child in children: for child in children:
if child.nodeType == ELEMENT: if child.get_nodeType() == ELEMENT:
tagName = child.tagName tagName = child.get_tagName()
if tagName in DESCRIPTOR_ELEMENTS: if tagName in DESCRIPTOR_ELEMENTS:
rewrite_descriptor(doc, child) rewrite_descriptor(doc, child)
elif tagName == "subsection": elif tagName == "subsection":
@ -200,7 +201,7 @@ def rewrite_descriptor(doc, descriptor):
# 6. Put it back together. # 6. Put it back together.
# #
# 1. # 1.
descname = descriptor.tagName descname = descriptor.get_tagName()
index = 1 index = 1
if descname[-2:] == "ni": if descname[-2:] == "ni":
descname = descname[:-2] descname = descname[:-2]
@ -235,7 +236,7 @@ def rewrite_descriptor(doc, descriptor):
pos = skip_leading_nodes(children) pos = skip_leading_nodes(children)
if pos < len(children): if pos < len(children):
child = children[pos] child = children[pos]
if child.nodeName == "args": if child.get_nodeName() == "args":
## bwrite("found <args> in descriptor, moving to <signature>\n") ## bwrite("found <args> in descriptor, moving to <signature>\n")
## ewrite(descriptor.toxml() + "\n---\n") ## ewrite(descriptor.toxml() + "\n---\n")
# create an <args> in <signature>: # create an <args> in <signature>:
@ -251,8 +252,8 @@ def rewrite_descriptor(doc, descriptor):
# 3, 4. # 3, 4.
pos = skip_leading_nodes(children, pos) pos = skip_leading_nodes(children, pos)
while pos < len(children) \ while pos < len(children) \
and children[pos].nodeName in (linename, "versionadded"): and children[pos].get_nodeName() in (linename, "versionadded"):
if children[pos].tagName == linename: if children[pos].get_tagName() == linename:
# this is really a supplemental signature, create <signature> # this is really a supplemental signature, create <signature>
sig = methodline_to_signature(doc, children[pos]) sig = methodline_to_signature(doc, children[pos])
newchildren.append(sig) newchildren.append(sig)
@ -307,7 +308,7 @@ def move_children(origin, dest, start=0):
def handle_appendix(doc, fragment): def handle_appendix(doc, fragment):
# must be called after simplfy() if document is multi-rooted to begin with # must be called after simplfy() if document is multi-rooted to begin with
docelem = get_documentElement(fragment) docelem = get_documentElement(fragment)
toplevel = docelem.tagName == "manual" and "chapter" or "section" toplevel = docelem.get_tagName() == "manual" and "chapter" or "section"
appendices = 0 appendices = 0
nodes = [] nodes = []
for node in docelem.childNodes: for node in docelem.childNodes:
@ -339,13 +340,14 @@ def handle_labels(doc, fragment):
if not id: if not id:
continue continue
parent = label.parentNode parent = label.parentNode
if parent.tagName == "title": parentTagName = parent.get_tagName()
if parentTagName == "title":
parent.parentNode.setAttribute("id", id) parent.parentNode.setAttribute("id", id)
else: else:
parent.setAttribute("id", id) parent.setAttribute("id", id)
# now, remove <label id="..."/> from parent: # now, remove <label id="..."/> from parent:
parent.removeChild(label) parent.removeChild(label)
if parent.tagName == "title": if parentTagName == "title":
parent.normalize() parent.normalize()
children = parent.childNodes children = parent.childNodes
if children[-1].nodeType == TEXT: if children[-1].nodeType == TEXT:
@ -357,8 +359,8 @@ def fixup_trailing_whitespace(doc, wsmap):
while queue: while queue:
node = queue[0] node = queue[0]
del queue[0] del queue[0]
if wsmap.has_key(node.nodeName): if wsmap.has_key(node.get_nodeName()):
ws = wsmap[node.tagName] ws = wsmap[node.get_tagName()]
children = node.childNodes children = node.childNodes
children.reverse() children.reverse()
if children[0].nodeType == TEXT: if children[0].nodeType == TEXT:
@ -366,8 +368,8 @@ def fixup_trailing_whitespace(doc, wsmap):
children[0].data = data children[0].data = data
children.reverse() children.reverse()
# hack to get the title in place: # hack to get the title in place:
if node.tagName == "title" \ if node.get_tagName() == "title" \
and node.parentNode.firstChild.nodeType == ELEMENT: and node.parentNode.firstChild.get_nodeType() == ELEMENT:
node.parentNode.insertBefore(doc.createText("\n "), node.parentNode.insertBefore(doc.createText("\n "),
node.parentNode.firstChild) node.parentNode.firstChild)
for child in node.childNodes: for child in node.childNodes:
@ -393,7 +395,7 @@ def cleanup_trailing_parens(doc, element_names):
while queue: while queue:
node = queue[0] node = queue[0]
del queue[0] del queue[0]
if rewrite_element(node.tagName): if rewrite_element(node.get_tagName()):
children = node.childNodes children = node.childNodes
if len(children) == 1 \ if len(children) == 1 \
and children[0].nodeType == TEXT: and children[0].nodeType == TEXT:
@ -416,7 +418,7 @@ def contents_match(left, right):
if nodeType != r.nodeType: if nodeType != r.nodeType:
return 0 return 0
if nodeType == ELEMENT: if nodeType == ELEMENT:
if l.tagName != r.tagName: if l.get_tagName() != r.get_tagName():
return 0 return 0
# should check attributes, but that's not a problem here # should check attributes, but that's not a problem here
if not contents_match(l, r): if not contents_match(l, r):
@ -447,7 +449,7 @@ def create_module_info(doc, section):
modauthor.getAttribute("name"))) modauthor.getAttribute("name")))
modauthor.removeAttribute("name") modauthor.removeAttribute("name")
platform = extract_first_element(section, "platform") platform = extract_first_element(section, "platform")
if section.tagName == "section": if section.get_tagName() == "section":
modinfo_pos = 2 modinfo_pos = 2
modinfo = doc.createElement("moduleinfo") modinfo = doc.createElement("moduleinfo")
moddecl = extract_first_element(section, "declaremodule") moddecl = extract_first_element(section, "declaremodule")
@ -472,7 +474,7 @@ def create_module_info(doc, section):
if title: if title:
children = title.childNodes children = title.childNodes
if len(children) >= 2 \ if len(children) >= 2 \
and children[0].nodeName == "module" \ and children[0].get_nodeName() == "module" \
and children[0].childNodes[0].data == name: and children[0].childNodes[0].data == name:
# this is it; morph the <title> into <short-synopsis> # this is it; morph the <title> into <short-synopsis>
first_data = children[1] first_data = children[1]
@ -516,7 +518,7 @@ def create_module_info(doc, section):
children = section.childNodes children = section.childNodes
for i in range(len(children)): for i in range(len(children)):
node = children[i] node = children[i]
if node.nodeName == "moduleinfo": if node.get_nodeName() == "moduleinfo":
nextnode = children[i+1] nextnode = children[i+1]
if nextnode.nodeType == TEXT: if nextnode.nodeType == TEXT:
data = nextnode.data data = nextnode.data
@ -549,7 +551,7 @@ def fixup_table(doc, table):
children = table.childNodes children = table.childNodes
for child in children: for child in children:
if child.nodeType == ELEMENT: if child.nodeType == ELEMENT:
tagName = child.tagName tagName = child.get_tagName()
if tagName == "hline" and prev_row is not None: if tagName == "hline" and prev_row is not None:
prev_row.setAttribute("rowsep", "1") prev_row.setAttribute("rowsep", "1")
elif tagName == "row": elif tagName == "row":
@ -567,9 +569,9 @@ def fixup_table(doc, table):
table.removeChild(child) table.removeChild(child)
continue continue
if nodeType == ELEMENT: if nodeType == ELEMENT:
if child.tagName != "hline": if child.get_tagName() != "hline":
raise ConversionError( raise ConversionError(
"unexpected <%s> in table" % child.tagName) "unexpected <%s> in table" % child.get_tagName())
table.removeChild(child) table.removeChild(child)
continue continue
raise ConversionError( raise ConversionError(
@ -598,7 +600,7 @@ def fixup_row(doc, row):
def move_elements_by_name(doc, source, dest, name, sep=None): def move_elements_by_name(doc, source, dest, name, sep=None):
nodes = [] nodes = []
for child in source.childNodes: for child in source.childNodes:
if child.nodeName == name: if child.get_nodeName() == name:
nodes.append(child) nodes.append(child)
for node in nodes: for node in nodes:
source.removeChild(node) source.removeChild(node)
@ -638,7 +640,7 @@ PARA_LEVEL_PRECEEDERS = (
def fixup_paras(doc, fragment): def fixup_paras(doc, fragment):
for child in fragment.childNodes: for child in fragment.childNodes:
if child.nodeName in RECURSE_INTO_PARA_CONTAINERS: if child.get_nodeName() in RECURSE_INTO_PARA_CONTAINERS:
fixup_paras_helper(doc, child) fixup_paras_helper(doc, child)
descriptions = find_all_elements(fragment, "description") descriptions = find_all_elements(fragment, "description")
for description in descriptions: for description in descriptions:
@ -650,7 +652,7 @@ def fixup_paras_helper(doc, container, depth=0):
children = container.childNodes children = container.childNodes
start = skip_leading_nodes(children) start = skip_leading_nodes(children)
while len(children) > start: while len(children) > start:
if children[start].nodeName in RECURSE_INTO_PARA_CONTAINERS: if children[start].get_nodeName() in RECURSE_INTO_PARA_CONTAINERS:
# Something to recurse into: # Something to recurse into:
fixup_paras_helper(doc, children[start]) fixup_paras_helper(doc, children[start])
else: else:
@ -673,7 +675,7 @@ def build_para(doc, parent, start, i):
child = children[j] child = children[j]
nodeType = child.nodeType nodeType = child.nodeType
if nodeType == ELEMENT: if nodeType == ELEMENT:
if child.tagName in BREAK_ELEMENTS: if child.get_tagName() in BREAK_ELEMENTS:
after = j after = j
break break
elif nodeType == TEXT: elif nodeType == TEXT:
@ -747,7 +749,7 @@ def skip_leading_nodes(children, start=0):
return start return start
# all whitespace, just skip # all whitespace, just skip
elif nodeType == ELEMENT: elif nodeType == ELEMENT:
tagName = child.tagName tagName = child.get_tagName()
if tagName in RECURSE_INTO_PARA_CONTAINERS: if tagName in RECURSE_INTO_PARA_CONTAINERS:
return start return start
if tagName not in PARA_LEVEL_ELEMENTS + PARA_LEVEL_PRECEEDERS: if tagName not in PARA_LEVEL_ELEMENTS + PARA_LEVEL_PRECEEDERS:
@ -777,7 +779,7 @@ def fixup_signatures(doc, fragment):
def fixup_args(doc, arglist): def fixup_args(doc, arglist):
for child in arglist.childNodes: for child in arglist.childNodes:
if child.nodeName == "optional": if child.get_nodeName() == "optional":
# found it; fix and return # found it; fix and return
arglist.insertBefore(doc.createTextNode("["), child) arglist.insertBefore(doc.createTextNode("["), child)
optkids = child.childNodes optkids = child.childNodes
@ -800,7 +802,7 @@ def fixup_sectionauthors(doc, fragment):
sectauth.removeAttribute("name") sectauth.removeAttribute("name")
after = section.childNodes[2] after = section.childNodes[2]
title = section.childNodes[1] title = section.childNodes[1]
if title.nodeName != "title": if title.get_nodeName() != "title":
after = section.childNodes[0] after = section.childNodes[0]
section.insertBefore(doc.createTextNode("\n "), after) section.insertBefore(doc.createTextNode("\n "), after)
section.insertBefore(sectauth, after) section.insertBefore(sectauth, after)
@ -843,7 +845,7 @@ def fixup_refmodindexes(fragment):
def fixup_refmodindexes_chunk(container): def fixup_refmodindexes_chunk(container):
# node is probably a <para>; let's see how often it isn't: # node is probably a <para>; let's see how often it isn't:
if container.tagName != PARA_ELEMENT: if container.get_tagName() != PARA_ELEMENT:
bwrite("--- fixup_refmodindexes_chunk(%s)\n" % container) bwrite("--- fixup_refmodindexes_chunk(%s)\n" % container)
module_entries = find_all_elements(container, "module") module_entries = find_all_elements(container, "module")
if not module_entries: if not module_entries:
@ -854,7 +856,7 @@ def fixup_refmodindexes_chunk(container):
children = entry.childNodes children = entry.childNodes
if len(children) != 0: if len(children) != 0:
bwrite("--- unexpected number of children for %s node:\n" bwrite("--- unexpected number of children for %s node:\n"
% entry.tagName) % entry.get_tagName())
ewrite(entry.toxml() + "\n") ewrite(entry.toxml() + "\n")
continue continue
found = 0 found = 0
@ -911,7 +913,7 @@ def write_esis(doc, ofp, knownempty):
for node in doc.childNodes: for node in doc.childNodes:
nodeType = node.nodeType nodeType = node.nodeType
if nodeType == ELEMENT: if nodeType == ELEMENT:
gi = node.tagName gi = node.get_tagName()
if knownempty(gi): if knownempty(gi):
if node.hasChildNodes(): if node.hasChildNodes():
raise ValueError, \ raise ValueError, \
@ -929,6 +931,8 @@ def write_esis(doc, ofp, knownempty):
ofp.write(")%s\n" % gi) ofp.write(")%s\n" % gi)
elif nodeType == TEXT: elif nodeType == TEXT:
ofp.write("-%s\n" % esistools.encode(node.data)) ofp.write("-%s\n" % esistools.encode(node.data))
elif nodeType == ENTITY_REFERENCE:
ofp.write("&%s\n" % node.get_nodeName())
else: else:
raise RuntimeError, "unsupported node type: %s" % nodeType raise RuntimeError, "unsupported node type: %s" % nodeType