Commit 3c171d1b authored by Fred Drake's avatar Fred Drake

Convert to string methods.

For the real document element, make sure the prolog is migrated into
the document element so it isn't left stranded.

Make fixup_trailing_whitespace() whitespace do what was really intended.

Add the *desc environments used in the C API manual to the list of
things that can exist at the paragraph level so they don't get wrapped
in <para>...</para>.
parent 647d5e8f
...@@ -8,7 +8,6 @@ of the Python documentation, and dump the ESIS data for the transformed tree. ...@@ -8,7 +8,6 @@ of the Python documentation, and dump the ESIS data for the transformed tree.
import errno import errno
import esistools import esistools
import re import re
import string
import sys import sys
import xml.dom import xml.dom
import xml.dom.minidom import xml.dom.minidom
...@@ -119,6 +118,12 @@ def simplify(doc, fragment): ...@@ -119,6 +118,12 @@ def simplify(doc, fragment):
node = get_first_element(fragment, "document") node = get_first_element(fragment, "document")
if node is not None: if node is not None:
set_tagName(node, documentclass) set_tagName(node, documentclass)
# 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)
while 1: while 1:
node = extract_first_element(fragment, "input") node = extract_first_element(fragment, "input")
if node is None: if node is None:
...@@ -251,7 +256,7 @@ def rewrite_descriptor(doc, descriptor): ...@@ -251,7 +256,7 @@ def rewrite_descriptor(doc, descriptor):
move_children(descriptor, description, pos) move_children(descriptor, description, pos)
last = description.childNodes[-1] last = description.childNodes[-1]
if last.nodeType == TEXT: if last.nodeType == TEXT:
last.data = string.rstrip(last.data) + "\n " last.data = last.data.rstrip() + "\n "
# 6. # 6.
# should have nothing but whitespace and signature lines in <descriptor>; # should have nothing but whitespace and signature lines in <descriptor>;
# discard them # discard them
...@@ -310,7 +315,7 @@ def handle_appendix(doc, fragment): ...@@ -310,7 +315,7 @@ def handle_appendix(doc, fragment):
docelem.appendChild(back) docelem.appendChild(back)
back.appendChild(doc.createTextNode("\n")) back.appendChild(doc.createTextNode("\n"))
while nodes and nodes[0].nodeType == TEXT \ while nodes and nodes[0].nodeType == TEXT \
and not string.strip(nodes[0].data): and not nodes[0].data.strip():
del nodes[0] del nodes[0]
map(back.appendChild, nodes) map(back.appendChild, nodes)
docelem.appendChild(doc.createTextNode("\n")) docelem.appendChild(doc.createTextNode("\n"))
...@@ -333,30 +338,44 @@ def handle_labels(doc, fragment): ...@@ -333,30 +338,44 @@ def handle_labels(doc, fragment):
parent.normalize() parent.normalize()
children = parent.childNodes children = parent.childNodes
if children[-1].nodeType == TEXT: if children[-1].nodeType == TEXT:
children[-1].data = string.rstrip(children[-1].data) children[-1].data = children[-1].data.rstrip()
def fixup_trailing_whitespace(doc, wsmap): def fixup_trailing_whitespace(doc, fragment, wsmap):
queue = [doc] queue = [fragment]
fixups = []
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.nodeName):
ws = wsmap[node.tagName] fixups.append(node)
children = node.childNodes for child in node.childNodes:
children.reverse() if child.nodeType == ELEMENT:
if children[0].nodeType == TEXT: queue.append(child)
data = string.rstrip(children[0].data) + ws
children[0].data = data # reverse the list to process from the inside out
children.reverse() 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: # hack to get the title in place:
if node.tagName == "title" \ if node.tagName == "title" \
and node.parentNode.firstChild.nodeType == ELEMENT: and node.parentNode.firstChild.nodeType == ELEMENT:
node.parentNode.insertBefore(doc.createText("\n "), node.parentNode.insertBefore(doc.createTextNode("\n "),
node.parentNode.firstChild) node.parentNode.firstChild)
for child in node.childNodes: node.parentNode.normalize()
if child.nodeType == ELEMENT:
queue.append(child)
def normalize(doc): def normalize(doc):
...@@ -461,7 +480,7 @@ def create_module_info(doc, section): ...@@ -461,7 +480,7 @@ def create_module_info(doc, section):
# 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]
if first_data.data[:4] == " ---": if first_data.data[:4] == " ---":
first_data.data = string.lstrip(first_data.data[4:]) first_data.data = first_data.data[4:].lstrip()
set_tagName(title, "short-synopsis") set_tagName(title, "short-synopsis")
if children[-1].nodeType == TEXT \ if children[-1].nodeType == TEXT \
and children[-1].data[-1:] == ".": and children[-1].data[-1:] == ".":
...@@ -504,8 +523,9 @@ def create_module_info(doc, section): ...@@ -504,8 +523,9 @@ def create_module_info(doc, section):
nextnode = children[i+1] nextnode = children[i+1]
if nextnode.nodeType == TEXT: if nextnode.nodeType == TEXT:
data = nextnode.data data = nextnode.data
if len(string.lstrip(data)) < (len(data) - 4): s = data.lstrip()
nextnode.data = "\n\n\n" + string.lstrip(data) if len(s) < (len(data) - 4):
nextnode.data = "\n\n\n" + s
def cleanup_synopses(doc, fragment): def cleanup_synopses(doc, fragment):
...@@ -546,7 +566,7 @@ def fixup_table(doc, table): ...@@ -546,7 +566,7 @@ def fixup_table(doc, table):
child = children[0] child = children[0]
nodeType = child.nodeType nodeType = child.nodeType
if nodeType == TEXT: if nodeType == TEXT:
if string.strip(child.data): if child.data.strip():
raise ConversionError("unexpected free data in <%s>: %r" raise ConversionError("unexpected free data in <%s>: %r"
% (table.tagName, child.data)) % (table.tagName, child.data))
table.removeChild(child) table.removeChild(child)
...@@ -605,6 +625,7 @@ PARA_LEVEL_ELEMENTS = ( ...@@ -605,6 +625,7 @@ PARA_LEVEL_ELEMENTS = (
"moduleinfo", "title", "verbatim", "enumerate", "item", "moduleinfo", "title", "verbatim", "enumerate", "item",
"interpreter-session", "back-matter", "interactive-session", "interpreter-session", "back-matter", "interactive-session",
"opcodedesc", "classdesc", "datadesc", "opcodedesc", "classdesc", "datadesc",
"cfuncdesc", "ctypedesc", "cvardesc",
"funcdesc", "methoddesc", "excdesc", "memberdesc", "membderdescni", "funcdesc", "methoddesc", "excdesc", "memberdesc", "membderdescni",
"funcdescni", "methoddescni", "excdescni", "funcdescni", "methoddescni", "excdescni",
"tableii", "tableiii", "tableiv", "localmoduletable", "tableii", "tableiii", "tableiv", "localmoduletable",
...@@ -662,7 +683,7 @@ def build_para(doc, parent, start, i): ...@@ -662,7 +683,7 @@ def build_para(doc, parent, start, i):
after = j after = j
break break
elif nodeType == TEXT: elif nodeType == TEXT:
pos = string.find(child.data, "\n\n") pos = child.data.find("\n\n")
if pos == 0: if pos == 0:
after = j after = j
break break
...@@ -678,9 +699,9 @@ def build_para(doc, parent, start, i): ...@@ -678,9 +699,9 @@ def build_para(doc, parent, start, i):
# we may need to split off trailing white space: # we may need to split off trailing white space:
child = children[after - 1] child = children[after - 1]
data = child.data data = child.data
if string.rstrip(data) != data: if data.rstrip() != data:
have_last = 0 have_last = 0
child.splitText(len(string.rstrip(data))) child.splitText(len(data.rstrip()))
para = doc.createElement(PARA_ELEMENT) para = doc.createElement(PARA_ELEMENT)
prev = None prev = None
indexes = range(start, after) indexes = range(start, after)
...@@ -723,7 +744,7 @@ def skip_leading_nodes(children, start=0): ...@@ -723,7 +744,7 @@ def skip_leading_nodes(children, start=0):
nodeType = child.nodeType nodeType = child.nodeType
if nodeType == TEXT: if nodeType == TEXT:
data = child.data data = child.data
shortened = string.lstrip(data) shortened = data.lstrip()
if shortened: if shortened:
if data != shortened: if data != shortened:
# break into two nodes: whitespace and non-whitespace # break into two nodes: whitespace and non-whitespace
...@@ -795,7 +816,7 @@ def fixup_verbatims(doc): ...@@ -795,7 +816,7 @@ def fixup_verbatims(doc):
for verbatim in find_all_elements(doc, "verbatim"): for verbatim in find_all_elements(doc, "verbatim"):
child = verbatim.childNodes[0] child = verbatim.childNodes[0]
if child.nodeType == TEXT \ if child.nodeType == TEXT \
and string.lstrip(child.data)[:3] == ">>>": and child.data.lstrip().startswith(">>>"):
set_tagName(verbatim, "interactive-session") set_tagName(verbatim, "interactive-session")
...@@ -973,15 +994,17 @@ def convert(ifp, ofp): ...@@ -973,15 +994,17 @@ def convert(ifp, ofp):
simplify(doc, fragment) simplify(doc, fragment)
handle_labels(doc, fragment) handle_labels(doc, fragment)
handle_appendix(doc, fragment) handle_appendix(doc, fragment)
fixup_trailing_whitespace(doc, { fixup_trailing_whitespace(doc, fragment, {
"abstract": "\n", # element -> (before-end-tag, after-end-tag)
"title": "", "abstract": ("\n", "\n"),
"chapter": "\n\n", "title": ("", "\n"),
"section": "\n\n", "chapter": ("\n", "\n\n\n"),
"subsection": "\n\n", "section": ("\n", "\n\n\n"),
"subsubsection": "\n\n", "subsection": ("\n", "\n\n"),
"paragraph": "\n\n", "subsubsection": ("\n", "\n\n"),
"subparagraph": "\n\n", "paragraph": ("\n", "\n\n"),
"subparagraph": ("\n", "\n\n"),
"enumeration": ("\n", "\n\n"),
}) })
cleanup_root_text(doc) cleanup_root_text(doc)
cleanup_trailing_parens(fragment, ["function", "method", "cfunction"]) cleanup_trailing_parens(fragment, ["function", "method", "cfunction"])
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment