Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 080c1b5

Browse files
committed
bwrite(), ewrite(): Helpers for diagnostic output; essentially the
same as sys.stderr.write(), but bwrite() will make it bold if stderr is a tty (for visibility). Use these everywhere instead of accessing sys.stderr.write() directly. find_all_child_elements(): Find all elements by type name that are immediate children of another. cleanup_synopses(): Receive both the document and fragment objects as parameters; if only the document is passed, synopsis information is never found since it's in the fragment! build_para(): Add a chunk of code to ensure that a newline always follows the </para> end-tag. Not really important, but makes presentation consistent for authoring/ maintenance. fixup_bifuncindexes_chunk(): Use find_all_child_elements() instead of find_all_elements() so we don't work with too many levels at once; that could cause DOM exceptions with the <elem>.removeChild() method.
1 parent 4fbdf97 commit 080c1b5

1 file changed

Lines changed: 61 additions & 30 deletions

File tree

Doc/tools/sgmlconv/docfixer.py

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,28 @@ class ConversionError(Exception):
2222
pass
2323

2424

25+
ewrite = sys.stderr.write
26+
try:
27+
# We can only do this trick on Unix (if tput is on $PATH)!
28+
if sys.platform != "posix" or not sys.stderr.isatty():
29+
raise ImportError
30+
import curses
31+
import commands
32+
except ImportError:
33+
bwrite = ewrite
34+
else:
35+
def bwrite(s, BOLDON=commands.getoutput("tput bold"),
36+
BOLDOFF=commands.getoutput("tput sgr0")):
37+
ewrite("%s%s%s" % (BOLDON, s, BOLDOFF))
38+
39+
2540
PARA_ELEMENT = "para"
2641

2742
DEBUG_PARA_FIXER = 0
2843

2944
if DEBUG_PARA_FIXER:
3045
def para_msg(s):
31-
sys.stderr.write("*** %s\n" % s)
46+
ewrite("*** %s\n" % s)
3247
else:
3348
def para_msg(s):
3449
pass
@@ -81,14 +96,23 @@ def find_all_elements(doc, gi):
8196
nodes.append(node)
8297
return nodes
8398

84-
def find_all_elements_from_set(doc, gi_set, nodes=None):
85-
if nodes is None:
86-
nodes = []
99+
def find_all_child_elements(doc, gi):
100+
nodes = []
101+
for child in doc.childNodes:
102+
if child.nodeType == ELEMENT:
103+
if child.tagName == gi:
104+
nodes.append(child)
105+
return nodes
106+
107+
def find_all_elements_from_set(doc, gi_set):
108+
return __find_all_elements_from_set(doc, gi_set, [])
109+
110+
def __find_all_elements_from_set(doc, gi_set, nodes):
87111
if doc.nodeType == ELEMENT and doc.tagName in gi_set:
88112
nodes.append(doc)
89113
for child in doc.childNodes:
90114
if child.nodeType == ELEMENT:
91-
find_all_elements_from_set(child, gi_set, nodes)
115+
__find_all_elements_from_set(child, gi_set, nodes)
92116
return nodes
93117

94118

@@ -165,7 +189,7 @@ def find_and_fix_descriptors(doc, container):
165189
def rewrite_descriptor(doc, descriptor):
166190
#
167191
# Do these things:
168-
# 1. Add an "index=noindex" attribute to the element if the tagName
192+
# 1. Add an "index='no'" attribute to the element if the tagName
169193
# ends in 'ni', removing the 'ni' from the name.
170194
# 2. Create a <signature> from the name attribute and <args>.
171195
# 3. Create additional <signature>s from <*line{,ni}> elements,
@@ -179,7 +203,7 @@ def rewrite_descriptor(doc, descriptor):
179203
index = 1
180204
if descname[-2:] == "ni":
181205
descname = descname[:-2]
182-
descriptor.setAttribute("index", "noindex")
206+
descriptor.setAttribute("index", "no")
183207
descriptor._node.name = descname
184208
index = 0
185209
desctype = descname[:-4] # remove 'desc'
@@ -459,12 +483,10 @@ def create_module_info(doc, section):
459483
title.removeChild(children[0])
460484
modinfo_pos = 0
461485
else:
462-
sys.stderr.write(
463-
"module name in title doesn't match"
464-
" <declaremodule>; no <short-synopsis>\n")
486+
ewrite("module name in title doesn't match"
487+
" <declaremodule/>; no <short-synopsis/>\n")
465488
else:
466-
sys.stderr.write(
467-
"Unexpected condition: <section> without <title>\n")
489+
ewrite("Unexpected condition: <section/> without <title/>\n")
468490
modinfo.appendChild(doc.createTextNode("\n "))
469491
modinfo.appendChild(node)
470492
if title and not contents_match(title, node):
@@ -484,7 +506,7 @@ def create_module_info(doc, section):
484506
#
485507
# The rest of this removes extra newlines from where we cut out
486508
# a lot of elements. A lot of code for minimal value, but keeps
487-
# keeps the generated SGML from being too funny looking.
509+
# keeps the generated *ML from being too funny looking.
488510
#
489511
section.normalize()
490512
children = section.childNodes
@@ -499,8 +521,8 @@ def create_module_info(doc, section):
499521
nextnode.data = "\n\n\n" + string.lstrip(data)
500522

501523

502-
def cleanup_synopses(doc):
503-
for node in find_all_elements(doc, "section"):
524+
def cleanup_synopses(doc, fragment):
525+
for node in find_all_elements(fragment, "section"):
504526
create_module_info(doc, node)
505527

506528

@@ -624,7 +646,7 @@ def move_elements_by_name(doc, source, dest, name, sep=None):
624646
"index", "indexii", "indexiii", "indexiv", "setindexsubitem",
625647
"stindex", "obindex", "COMMENT", "label", "input", "title",
626648
"versionadded", "versionchanged", "declaremodule", "modulesynopsis",
627-
"moduleauthor",
649+
"moduleauthor", "indexterm",
628650
)
629651

630652

@@ -710,9 +732,19 @@ def build_para(doc, parent, start, i):
710732
prev = node
711733
if have_last:
712734
parent.appendChild(para)
735+
parent.appendChild(doc.createTextNode("\n\n"))
713736
return len(parent.childNodes)
714737
else:
715-
parent.insertBefore(para, parent.childNodes[start])
738+
nextnode = parent.childNodes[start]
739+
if nextnode.nodeType == TEXT:
740+
if nextnode.data and nextnode.data[0] != "\n":
741+
nextnode.data = "\n" + nextnode.data
742+
else:
743+
newnode = doc.createTextNode("\n")
744+
parent.insertBefore(newnode, nextnode)
745+
nextnode = newnode
746+
start = start + 1
747+
parent.insertBefore(para, nextnode)
716748
return start + 1
717749

718750

@@ -838,7 +870,7 @@ def fixup_refmodindexes(fragment):
838870
def fixup_refmodindexes_chunk(container):
839871
# node is probably a <para>; let's see how often it isn't:
840872
if container.tagName != PARA_ELEMENT:
841-
sys.stderr.write("--- fixup_refmodindexes_chunk(%s)\n" % container)
873+
bwrite("--- fixup_refmodindexes_chunk(%s)\n" % container)
842874
module_entries = find_all_elements(container, "module")
843875
if not module_entries:
844876
return
@@ -847,10 +879,9 @@ def fixup_refmodindexes_chunk(container):
847879
for entry in index_entries:
848880
children = entry.childNodes
849881
if len(children) != 0:
850-
sys.stderr.write(
851-
"--- unexpected number of children for %s node:\n"
852-
% entry.tagName)
853-
sys.stderr.write(entry.toxml() + "\n")
882+
bwrite("--- unexpected number of children for %s node:\n"
883+
% entry.tagName)
884+
ewrite(entry.toxml() + "\n")
854885
continue
855886
found = 0
856887
module_name = entry.getAttribute("name")
@@ -860,7 +891,7 @@ def fixup_refmodindexes_chunk(container):
860891
this_name = node.childNodes[0].data
861892
if this_name == module_name:
862893
found = 1
863-
node.setAttribute("index", "index")
894+
node.setAttribute("index", "yes")
864895
if found:
865896
removes.append(entry)
866897
for node in removes:
@@ -870,6 +901,7 @@ def fixup_refmodindexes_chunk(container):
870901
def fixup_bifuncindexes(fragment):
871902
nodes = find_all_elements(fragment, 'bifuncindex')
872903
d = {}
904+
# make sure that each parent is only processed once:
873905
for node in nodes:
874906
parent = node.parentNode
875907
d[parent._node.node_id] = parent
@@ -879,8 +911,8 @@ def fixup_bifuncindexes(fragment):
879911

880912
def fixup_bifuncindexes_chunk(container):
881913
removes = []
882-
entries = find_all_elements(container, "bifuncindex")
883-
function_entries = find_all_elements(container, "function")
914+
entries = find_all_child_elements(container, "bifuncindex")
915+
function_entries = find_all_child_elements(container, "function")
884916
for entry in entries:
885917
function_name = entry.getAttribute("name")
886918
found = 0
@@ -890,12 +922,11 @@ def fixup_bifuncindexes_chunk(container):
890922
continue
891923
t2 = t2[:-2]
892924
if t2 == function_name:
893-
894-
func_entry.setAttribute("index", "index")
925+
func_entry.setAttribute("index", "yes")
895926
func_entry.setAttribute("module", "__builtin__")
896927
if not found:
897-
removes.append(entry)
898928
found = 1
929+
removes.append(entry)
899930
for entry in removes:
900931
container.removeChild(entry)
901932

@@ -948,8 +979,8 @@ def convert(ifp, ofp):
948979
"subparagraph": "\n\n",
949980
})
950981
cleanup_root_text(doc)
951-
cleanup_trailing_parens(doc, ["function", "method", "cfunction"])
952-
cleanup_synopses(doc)
982+
cleanup_trailing_parens(fragment, ["function", "method", "cfunction"])
983+
cleanup_synopses(doc, fragment)
953984
fixup_descriptors(doc, fragment)
954985
fixup_verbatims(fragment)
955986
normalize(fragment)

0 commit comments

Comments
 (0)