Skip to content

Commit aaed971

Browse files
committed
create_module_info(): New function: Modify a <section> to create a
<moduleinfo> element based on various meta information, and strip some cruftiness. This is more usable for information extraction, and organizes the information more clearly. cleanup_synopses(): Rewrite to use create_module_info(), so this will work with multi-rooted "documents".
1 parent fee6abe commit aaed971

1 file changed

Lines changed: 99 additions & 13 deletions

File tree

Doc/tools/sgmlconv/docfixer.py

Lines changed: 99 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,20 +227,104 @@ def cleanup_trailing_parens(doc, element_names):
227227
queue.append(child)
228228

229229

230+
def contents_match(left, right):
231+
left_children = left.childNodes
232+
right_children = right.childNodes
233+
if len(left_children) != len(right_children):
234+
return 0
235+
for l, r in map(None, left_children, right_children):
236+
nodeType = l.nodeType
237+
if nodeType != r.nodeType:
238+
return 0
239+
if nodeType == xml.dom.core.ELEMENT:
240+
if l.tagName != r.tagName:
241+
return 0
242+
# should check attributes, but that's not a problem here
243+
if not contents_match(l, r):
244+
return 0
245+
elif nodeType == xml.dom.core.TEXT:
246+
if l.data != r.data:
247+
return 0
248+
else:
249+
# not quite right, but good enough
250+
return 0
251+
return 1
252+
253+
254+
def create_module_info(doc, section):
255+
# Heavy.
256+
node = extract_first_element(section, "modulesynopsis")
257+
if node is None:
258+
return
259+
node._node.name = "synopsis"
260+
lastchild = node.childNodes[-1]
261+
if lastchild.nodeType == xml.dom.core.TEXT \
262+
and lastchild.data[-1:] == ".":
263+
lastchild.data = lastchild.data[:-1]
264+
if section.tagName == "section":
265+
modinfo_pos = 2
266+
modinfo = doc.createElement("moduleinfo")
267+
moddecl = extract_first_element(section, "declaremodule")
268+
name = None
269+
if moddecl:
270+
modinfo.appendChild(doc.createTextNode("\n "))
271+
name = moddecl.attributes["name"].value
272+
namenode = doc.createElement("name")
273+
namenode.appendChild(doc.createTextNode(name))
274+
modinfo.appendChild(namenode)
275+
type = moddecl.attributes.get("type")
276+
if type:
277+
type = type.value
278+
modinfo.appendChild(doc.createTextNode("\n "))
279+
typenode = doc.createElement("type")
280+
typenode.appendChild(doc.createTextNode(type))
281+
modinfo.appendChild(typenode)
282+
title = get_first_element(section, "title")
283+
if title:
284+
children = title.childNodes
285+
if len(children) >= 2 \
286+
and children[0].nodeType == xml.dom.core.ELEMENT \
287+
and children[0].tagName == "module" \
288+
and children[0].childNodes[0].data == name:
289+
# this is it; morph the <title> into <short-synopsis>
290+
first_data = children[1]
291+
if first_data.data[:4] == " ---":
292+
first_data.data = string.lstrip(first_data.data[4:])
293+
title._node.name = "short-synopsis"
294+
if children[-1].data[-1:] == ".":
295+
children[-1].data = children[-1].data[:-1]
296+
section.removeChild(title)
297+
section.removeChild(section.childNodes[0])
298+
title.removeChild(children[0])
299+
modinfo_pos = 0
300+
else:
301+
sys.stderr.write(
302+
"module name in title doesn't match"
303+
" <declaremodule>; no <short-synopsis>\n")
304+
else:
305+
sys.stderr.write(
306+
"Unexpected condition: <section> without <title>\n")
307+
modinfo.appendChild(doc.createTextNode("\n "))
308+
modinfo.appendChild(node)
309+
if title and not contents_match(title, node):
310+
# The short synopsis is actually different,
311+
# and needs to be stored:
312+
modinfo.appendChild(doc.createTextNode("\n "))
313+
modinfo.appendChild(title)
314+
modinfo.appendChild(doc.createTextNode("\n "))
315+
section.insertBefore(modinfo, section.childNodes[modinfo_pos])
316+
section.insertBefore(doc.createTextNode("\n "), modinfo)
317+
318+
230319
def cleanup_synopses(doc):
231-
# Actually, this should build a "moduleinfo" element from various
232-
# parts of the meta-information in the section. <moduleinfo> needs
233-
# some design work before we can really do anything real.
234-
synopses = doc.getElementsByTagName("modulesynopsis")
235-
for node in synopses:
236-
node._node.name = "synopsis"
237-
parent = node.parentNode
238-
if parent.tagName == "section":
239-
children = parent.childNodes
240-
parent.removeChild(node)
241-
parent.insertBefore(node, children[2])
242-
text = doc.createTextNode("\n ")
243-
parent.insertBefore(text, node)
320+
for node in doc.childNodes:
321+
if node.nodeType == xml.dom.core.ELEMENT \
322+
and node.tagName == "section":
323+
create_module_info(doc, node)
324+
325+
326+
def fixup_paras(doc):
327+
pass
244328

245329

246330
_token_rx = re.compile(r"[a-zA-Z][a-zA-Z0-9.-]*$")
@@ -292,6 +376,8 @@ def convert(ifp, ofp):
292376
cleanup_root_text(doc)
293377
cleanup_trailing_parens(doc, ["function", "method", "cfunction"])
294378
cleanup_synopses(doc)
379+
normalize(doc)
380+
fixup_paras(doc)
295381
#
296382
d = {}
297383
for gi in p.get_empties():

0 commit comments

Comments
 (0)