@@ -153,15 +153,82 @@ def handle_labels(doc):
153153 parent .removeChild (label )
154154
155155
156+ def fixup_trailing_whitespace (doc , wsmap ):
157+ queue = [doc ]
158+ while queue :
159+ node = queue [0 ]
160+ del queue [0 ]
161+ if node .nodeType == xml .dom .core .ELEMENT \
162+ and wsmap .has_key (node .tagName ):
163+ ws = wsmap [node .tagName ]
164+ children = node .childNodes
165+ children .reverse ()
166+ if children [0 ].nodeType == xml .dom .core .TEXT :
167+ data = string .rstrip (children [0 ].data ) + ws
168+ children [0 ].data = data
169+ children .reverse ()
170+ # hack to get the title in place:
171+ if node .tagName == "title" \
172+ and node .parentNode .firstChild .nodeType == xml .dom .core .ELEMENT :
173+ node .parentNode .insertBefore (doc .createText ("\n " ),
174+ node .parentNode .firstChild )
175+ for child in node .childNodes :
176+ if child .nodeType == xml .dom .core .ELEMENT :
177+ queue .append (child )
178+
179+
180+ def normalize (doc ):
181+ for node in doc .childNodes :
182+ if node .nodeType == xml .dom .core .ELEMENT :
183+ node .normalize ()
184+
185+
186+ def cleanup_trailing_parens (doc , element_names ):
187+ d = {}
188+ for gi in element_names :
189+ d [gi ] = gi
190+ rewrite_element = d .has_key
191+ queue = []
192+ for node in doc .childNodes :
193+ if node .nodeType == xml .dom .core .ELEMENT :
194+ queue .append (node )
195+ while queue :
196+ node = queue [0 ]
197+ del queue [0 ]
198+ if rewrite_element (node .tagName ):
199+ children = node .childNodes
200+ if len (children ) == 1 \
201+ and children [0 ].nodeType == xml .dom .core .TEXT :
202+ data = children [0 ].data
203+ if data [- 2 :] == "()" :
204+ children [0 ].data = data [:- 2 ]
205+ else :
206+ for child in node .childNodes :
207+ if child .nodeType == xml .dom .core .ELEMENT :
208+ queue .append (child )
209+
210+
156211def convert (ifp , ofp ):
157212 p = xml .dom .esis_builder .EsisBuilder ()
158213 p .feed (ifp .read ())
159214 doc = p .document
215+ normalize (doc )
160216 handle_args (doc )
161217 handle_comments (doc )
162218 simplify (doc )
163219 handle_labels (doc )
220+ fixup_trailing_whitespace (doc , {
221+ "abstract" : "\n " ,
222+ "title" : "" ,
223+ "chapter" : "\n \n " ,
224+ "section" : "\n \n " ,
225+ "subsection" : "\n \n " ,
226+ "subsubsection" : "\n \n " ,
227+ "paragraph" : "\n \n " ,
228+ "subparagraph" : "\n \n " ,
229+ })
164230 cleanup_root_text (doc )
231+ cleanup_trailing_parens (doc , ["function" , "method" , "cfunction" ])
165232 try :
166233 ofp .write (doc .toxml ())
167234 ofp .write ("\n " )
0 commit comments