Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | |
| 3 | """Promote the IDs from <label/> elements to the enclosing section / chapter / |
| 4 | whatever, then remove the <label/> elements. This allows *ML style internal |
| 5 | linking rather than the bogus LaTeX model. |
| 6 | |
| 7 | Note that <label/>s in <title> elements are promoted two steps, since the |
| 8 | <title> elements are artificially created from the section parameter, and the |
| 9 | label really refers to the sectioning construct. |
| 10 | """ |
| 11 | __version__ = '$Revision$' |
| 12 | |
| 13 | |
| 14 | import errno |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 15 | import esistools |
| 16 | import re |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 17 | import string |
| 18 | import sys |
| 19 | import xml.dom.core |
| 20 | import xml.dom.esis_builder |
| 21 | |
| 22 | |
Fred Drake | f8ebb55 | 1999-01-14 19:45:38 +0000 | [diff] [blame] | 23 | class ConversionError(Exception): |
| 24 | pass |
| 25 | |
| 26 | |
Fred Drake | fcc5910 | 1999-01-06 22:50:52 +0000 | [diff] [blame] | 27 | DEBUG_PARA_FIXER = 0 |
| 28 | |
| 29 | |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 30 | # Workaround to deal with invalid documents (multiple root elements). This |
| 31 | # does not indicate a bug in the DOM implementation. |
| 32 | # |
| 33 | def get_documentElement(self): |
| 34 | docelem = None |
| 35 | for n in self._node.children: |
| 36 | if n.type == xml.dom.core.ELEMENT: |
| 37 | docelem = xml.dom.core.Element(n, self, self) |
| 38 | return docelem |
| 39 | |
| 40 | xml.dom.core.Document.get_documentElement = get_documentElement |
| 41 | |
| 42 | |
| 43 | # Replace get_childNodes for the Document class; without this, children |
| 44 | # accessed from the Document object via .childNodes (no matter how many |
| 45 | # levels of access are used) will be given an ownerDocument of None. |
| 46 | # |
| 47 | def get_childNodes(self): |
| 48 | return xml.dom.core.NodeList(self._node.children, self, self) |
| 49 | |
| 50 | xml.dom.core.Document.get_childNodes = get_childNodes |
| 51 | |
| 52 | |
| 53 | def get_first_element(doc, gi): |
| 54 | for n in doc.childNodes: |
| 55 | if n.nodeType == xml.dom.core.ELEMENT and n.tagName == gi: |
| 56 | return n |
| 57 | |
| 58 | def extract_first_element(doc, gi): |
| 59 | node = get_first_element(doc, gi) |
| 60 | if node is not None: |
| 61 | doc.removeChild(node) |
| 62 | return node |
| 63 | |
| 64 | |
| 65 | def simplify(doc): |
| 66 | # Try to rationalize the document a bit, since these things are simply |
| 67 | # not valid SGML/XML documents as they stand, and need a little work. |
| 68 | documentclass = "document" |
| 69 | inputs = [] |
| 70 | node = extract_first_element(doc, "documentclass") |
| 71 | if node is not None: |
| 72 | documentclass = node.getAttribute("classname") |
| 73 | node = extract_first_element(doc, "title") |
| 74 | if node is not None: |
| 75 | inputs.append(node) |
| 76 | # update the name of the root element |
| 77 | node = get_first_element(doc, "document") |
| 78 | if node is not None: |
| 79 | node._node.name = documentclass |
| 80 | while 1: |
| 81 | node = extract_first_element(doc, "input") |
| 82 | if node is None: |
| 83 | break |
| 84 | inputs.append(node) |
| 85 | if inputs: |
| 86 | docelem = doc.documentElement |
| 87 | inputs.reverse() |
| 88 | for node in inputs: |
| 89 | text = doc.createTextNode("\n") |
| 90 | docelem.insertBefore(text, docelem.firstChild) |
| 91 | docelem.insertBefore(node, text) |
| 92 | docelem.insertBefore(doc.createTextNode("\n"), docelem.firstChild) |
| 93 | while doc.firstChild.nodeType == xml.dom.core.TEXT: |
| 94 | doc.removeChild(doc.firstChild) |
| 95 | |
| 96 | |
| 97 | def cleanup_root_text(doc): |
| 98 | discards = [] |
| 99 | skip = 0 |
| 100 | for n in doc.childNodes: |
| 101 | prevskip = skip |
| 102 | skip = 0 |
| 103 | if n.nodeType == xml.dom.core.TEXT and not prevskip: |
| 104 | discards.append(n) |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 105 | elif n.nodeType == xml.dom.core.ELEMENT and n.tagName == "COMMENT": |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 106 | skip = 1 |
| 107 | for node in discards: |
| 108 | doc.removeChild(node) |
| 109 | |
| 110 | |
| 111 | def rewrite_desc_entries(doc, argname_gi): |
| 112 | argnodes = doc.getElementsByTagName(argname_gi) |
| 113 | for node in argnodes: |
| 114 | parent = node.parentNode |
| 115 | nodes = [] |
| 116 | for n in parent.childNodes: |
| 117 | if n.nodeType != xml.dom.core.ELEMENT or n.tagName != argname_gi: |
| 118 | nodes.append(n) |
| 119 | desc = doc.createElement("description") |
| 120 | for n in nodes: |
| 121 | parent.removeChild(n) |
| 122 | desc.appendChild(n) |
| 123 | if node.childNodes: |
| 124 | # keep the <args>...</args>, newline & indent |
| 125 | parent.insertBefore(doc.createText("\n "), node) |
| 126 | else: |
| 127 | # no arguments, remove the <args/> node |
| 128 | parent.removeChild(node) |
| 129 | parent.appendChild(doc.createText("\n ")) |
| 130 | parent.appendChild(desc) |
| 131 | parent.appendChild(doc.createText("\n")) |
| 132 | |
| 133 | def handle_args(doc): |
| 134 | rewrite_desc_entries(doc, "args") |
| 135 | rewrite_desc_entries(doc, "constructor-args") |
| 136 | |
| 137 | |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 138 | def handle_appendix(doc): |
| 139 | # must be called after simplfy() if document is multi-rooted to begin with |
| 140 | docelem = doc.documentElement |
| 141 | toplevel = docelem.tagName == "manual" and "chapter" or "section" |
| 142 | appendices = 0 |
| 143 | nodes = [] |
| 144 | for node in docelem.childNodes: |
| 145 | if appendices: |
| 146 | nodes.append(node) |
| 147 | elif node.nodeType == xml.dom.core.ELEMENT: |
| 148 | appnodes = node.getElementsByTagName("appendix") |
| 149 | if appnodes: |
| 150 | appendices = 1 |
| 151 | parent = appnodes[0].parentNode |
| 152 | parent.removeChild(appnodes[0]) |
| 153 | parent.normalize() |
| 154 | if nodes: |
| 155 | map(docelem.removeChild, nodes) |
| 156 | docelem.appendChild(doc.createTextNode("\n\n\n")) |
| 157 | back = doc.createElement("back-matter") |
| 158 | docelem.appendChild(back) |
| 159 | back.appendChild(doc.createTextNode("\n")) |
| 160 | while nodes and nodes[0].nodeType == xml.dom.core.TEXT \ |
| 161 | and not string.strip(nodes[0].data): |
| 162 | del nodes[0] |
| 163 | map(back.appendChild, nodes) |
| 164 | docelem.appendChild(doc.createTextNode("\n")) |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 165 | |
| 166 | |
| 167 | def handle_labels(doc): |
| 168 | labels = doc.getElementsByTagName("label") |
| 169 | for label in labels: |
| 170 | id = label.getAttribute("id") |
| 171 | if not id: |
| 172 | continue |
| 173 | parent = label.parentNode |
| 174 | if parent.tagName == "title": |
| 175 | parent.parentNode.setAttribute("id", id) |
| 176 | else: |
| 177 | parent.setAttribute("id", id) |
| 178 | # now, remove <label id="..."/> from parent: |
| 179 | parent.removeChild(label) |
| 180 | |
| 181 | |
Fred Drake | 1ff6db4 | 1998-11-23 23:10:35 +0000 | [diff] [blame] | 182 | def fixup_trailing_whitespace(doc, wsmap): |
| 183 | queue = [doc] |
| 184 | while queue: |
| 185 | node = queue[0] |
| 186 | del queue[0] |
| 187 | if node.nodeType == xml.dom.core.ELEMENT \ |
| 188 | and wsmap.has_key(node.tagName): |
| 189 | ws = wsmap[node.tagName] |
| 190 | children = node.childNodes |
| 191 | children.reverse() |
| 192 | if children[0].nodeType == xml.dom.core.TEXT: |
| 193 | data = string.rstrip(children[0].data) + ws |
| 194 | children[0].data = data |
| 195 | children.reverse() |
| 196 | # hack to get the title in place: |
| 197 | if node.tagName == "title" \ |
| 198 | and node.parentNode.firstChild.nodeType == xml.dom.core.ELEMENT: |
| 199 | node.parentNode.insertBefore(doc.createText("\n "), |
| 200 | node.parentNode.firstChild) |
| 201 | for child in node.childNodes: |
| 202 | if child.nodeType == xml.dom.core.ELEMENT: |
| 203 | queue.append(child) |
| 204 | |
| 205 | |
| 206 | def normalize(doc): |
| 207 | for node in doc.childNodes: |
| 208 | if node.nodeType == xml.dom.core.ELEMENT: |
| 209 | node.normalize() |
| 210 | |
| 211 | |
| 212 | def cleanup_trailing_parens(doc, element_names): |
| 213 | d = {} |
| 214 | for gi in element_names: |
| 215 | d[gi] = gi |
| 216 | rewrite_element = d.has_key |
| 217 | queue = [] |
| 218 | for node in doc.childNodes: |
| 219 | if node.nodeType == xml.dom.core.ELEMENT: |
| 220 | queue.append(node) |
| 221 | while queue: |
| 222 | node = queue[0] |
| 223 | del queue[0] |
| 224 | if rewrite_element(node.tagName): |
| 225 | children = node.childNodes |
| 226 | if len(children) == 1 \ |
| 227 | and children[0].nodeType == xml.dom.core.TEXT: |
| 228 | data = children[0].data |
| 229 | if data[-2:] == "()": |
| 230 | children[0].data = data[:-2] |
| 231 | else: |
| 232 | for child in node.childNodes: |
| 233 | if child.nodeType == xml.dom.core.ELEMENT: |
| 234 | queue.append(child) |
| 235 | |
| 236 | |
Fred Drake | aaed971 | 1998-12-10 20:25:30 +0000 | [diff] [blame] | 237 | def contents_match(left, right): |
| 238 | left_children = left.childNodes |
| 239 | right_children = right.childNodes |
| 240 | if len(left_children) != len(right_children): |
| 241 | return 0 |
| 242 | for l, r in map(None, left_children, right_children): |
| 243 | nodeType = l.nodeType |
| 244 | if nodeType != r.nodeType: |
| 245 | return 0 |
| 246 | if nodeType == xml.dom.core.ELEMENT: |
| 247 | if l.tagName != r.tagName: |
| 248 | return 0 |
| 249 | # should check attributes, but that's not a problem here |
| 250 | if not contents_match(l, r): |
| 251 | return 0 |
| 252 | elif nodeType == xml.dom.core.TEXT: |
| 253 | if l.data != r.data: |
| 254 | return 0 |
| 255 | else: |
| 256 | # not quite right, but good enough |
| 257 | return 0 |
| 258 | return 1 |
| 259 | |
| 260 | |
| 261 | def create_module_info(doc, section): |
| 262 | # Heavy. |
| 263 | node = extract_first_element(section, "modulesynopsis") |
| 264 | if node is None: |
| 265 | return |
| 266 | node._node.name = "synopsis" |
| 267 | lastchild = node.childNodes[-1] |
| 268 | if lastchild.nodeType == xml.dom.core.TEXT \ |
| 269 | and lastchild.data[-1:] == ".": |
| 270 | lastchild.data = lastchild.data[:-1] |
| 271 | if section.tagName == "section": |
| 272 | modinfo_pos = 2 |
| 273 | modinfo = doc.createElement("moduleinfo") |
| 274 | moddecl = extract_first_element(section, "declaremodule") |
| 275 | name = None |
| 276 | if moddecl: |
| 277 | modinfo.appendChild(doc.createTextNode("\n ")) |
| 278 | name = moddecl.attributes["name"].value |
| 279 | namenode = doc.createElement("name") |
| 280 | namenode.appendChild(doc.createTextNode(name)) |
| 281 | modinfo.appendChild(namenode) |
| 282 | type = moddecl.attributes.get("type") |
| 283 | if type: |
| 284 | type = type.value |
| 285 | modinfo.appendChild(doc.createTextNode("\n ")) |
| 286 | typenode = doc.createElement("type") |
| 287 | typenode.appendChild(doc.createTextNode(type)) |
| 288 | modinfo.appendChild(typenode) |
| 289 | title = get_first_element(section, "title") |
| 290 | if title: |
| 291 | children = title.childNodes |
| 292 | if len(children) >= 2 \ |
| 293 | and children[0].nodeType == xml.dom.core.ELEMENT \ |
| 294 | and children[0].tagName == "module" \ |
| 295 | and children[0].childNodes[0].data == name: |
| 296 | # this is it; morph the <title> into <short-synopsis> |
| 297 | first_data = children[1] |
| 298 | if first_data.data[:4] == " ---": |
| 299 | first_data.data = string.lstrip(first_data.data[4:]) |
| 300 | title._node.name = "short-synopsis" |
| 301 | if children[-1].data[-1:] == ".": |
| 302 | children[-1].data = children[-1].data[:-1] |
| 303 | section.removeChild(title) |
| 304 | section.removeChild(section.childNodes[0]) |
| 305 | title.removeChild(children[0]) |
| 306 | modinfo_pos = 0 |
| 307 | else: |
| 308 | sys.stderr.write( |
| 309 | "module name in title doesn't match" |
| 310 | " <declaremodule>; no <short-synopsis>\n") |
| 311 | else: |
| 312 | sys.stderr.write( |
| 313 | "Unexpected condition: <section> without <title>\n") |
| 314 | modinfo.appendChild(doc.createTextNode("\n ")) |
| 315 | modinfo.appendChild(node) |
| 316 | if title and not contents_match(title, node): |
| 317 | # The short synopsis is actually different, |
| 318 | # and needs to be stored: |
| 319 | modinfo.appendChild(doc.createTextNode("\n ")) |
| 320 | modinfo.appendChild(title) |
| 321 | modinfo.appendChild(doc.createTextNode("\n ")) |
| 322 | section.insertBefore(modinfo, section.childNodes[modinfo_pos]) |
| 323 | section.insertBefore(doc.createTextNode("\n "), modinfo) |
| 324 | |
| 325 | |
Fred Drake | fba0ba2 | 1998-12-10 05:07:09 +0000 | [diff] [blame] | 326 | def cleanup_synopses(doc): |
Fred Drake | aaed971 | 1998-12-10 20:25:30 +0000 | [diff] [blame] | 327 | for node in doc.childNodes: |
| 328 | if node.nodeType == xml.dom.core.ELEMENT \ |
| 329 | and node.tagName == "section": |
| 330 | create_module_info(doc, node) |
| 331 | |
| 332 | |
Fred Drake | f8ebb55 | 1999-01-14 19:45:38 +0000 | [diff] [blame] | 333 | def remap_element_names(root, name_map): |
| 334 | queue = [] |
| 335 | for child in root.childNodes: |
| 336 | if child.nodeType == xml.dom.core.ELEMENT: |
| 337 | queue.append(child) |
| 338 | while queue: |
| 339 | node = queue.pop() |
| 340 | tagName = node.tagName |
| 341 | if name_map.has_key(tagName): |
| 342 | name, attrs = name_map[tagName] |
| 343 | node._node.name = name |
| 344 | for attr, value in attrs.items(): |
| 345 | node.setAttribute(attr, value) |
| 346 | for child in node.childNodes: |
| 347 | if child.nodeType == xml.dom.core.ELEMENT: |
| 348 | queue.append(child) |
| 349 | |
| 350 | |
| 351 | def fixup_table_structures(doc): |
| 352 | # must be done after remap_element_names(), or the tables won't be found |
| 353 | for child in doc.childNodes: |
| 354 | if child.nodeType == xml.dom.core.ELEMENT: |
| 355 | tables = child.getElementsByTagName("table") |
| 356 | for table in tables: |
| 357 | fixup_table(doc, table) |
| 358 | |
| 359 | def fixup_table(doc, table): |
| 360 | # create the table head |
| 361 | thead = doc.createElement("thead") |
| 362 | row = doc.createElement("row") |
| 363 | move_elements_by_name(doc, table, row, "entry") |
| 364 | thead.appendChild(doc.createTextNode("\n ")) |
| 365 | thead.appendChild(row) |
| 366 | thead.appendChild(doc.createTextNode("\n ")) |
| 367 | # create the table body |
| 368 | tbody = doc.createElement("tbody") |
| 369 | prev_row = None |
| 370 | last_was_hline = 0 |
| 371 | children = table.childNodes |
| 372 | for child in children: |
| 373 | if child.nodeType == xml.dom.core.ELEMENT: |
| 374 | tagName = child.tagName |
| 375 | if tagName == "hline" and prev_row is not None: |
| 376 | prev_row.setAttribute("rowsep", "1") |
| 377 | elif tagName == "row": |
| 378 | prev_row = child |
| 379 | # save the rows: |
| 380 | tbody.appendChild(doc.createTextNode("\n ")) |
| 381 | move_elements_by_name(doc, table, tbody, "row", sep="\n ") |
| 382 | # and toss the rest: |
| 383 | while children: |
| 384 | child = children[0] |
| 385 | nodeType = child.nodeType |
| 386 | if nodeType == xml.dom.core.TEXT: |
| 387 | if string.strip(child.data): |
| 388 | raise ConversionError("unexpected free data in table") |
| 389 | table.removeChild(child) |
| 390 | continue |
| 391 | if nodeType == xml.dom.core.ELEMENT: |
| 392 | if child.tagName != "hline": |
| 393 | raise ConversionError( |
| 394 | "unexpected <%s> in table" % child.tagName) |
| 395 | table.removeChild(child) |
| 396 | continue |
| 397 | raise ConversionError( |
| 398 | "unexpected %s node in table" % child.__class__.__name__) |
| 399 | # nothing left in the <table>; add the <thead> and <tbody> |
| 400 | tgroup = doc.createElement("tgroup") |
| 401 | tgroup.appendChild(doc.createTextNode("\n ")) |
| 402 | tgroup.appendChild(thead) |
| 403 | tgroup.appendChild(doc.createTextNode("\n ")) |
| 404 | tgroup.appendChild(tbody) |
| 405 | tgroup.appendChild(doc.createTextNode("\n ")) |
| 406 | table.appendChild(tgroup) |
| 407 | # now make the <entry>s look nice: |
| 408 | for row in table.getElementsByTagName("row"): |
| 409 | fixup_row(doc, row) |
| 410 | |
| 411 | |
| 412 | def fixup_row(doc, row): |
| 413 | entries = [] |
| 414 | map(entries.append, row.childNodes[1:]) |
| 415 | for entry in entries: |
| 416 | row.insertBefore(doc.createTextNode("\n "), entry) |
| 417 | # row.appendChild(doc.createTextNode("\n ")) |
| 418 | |
| 419 | |
| 420 | def move_elements_by_name(doc, source, dest, name, sep=None): |
| 421 | nodes = [] |
| 422 | for child in source.childNodes: |
| 423 | if child.nodeType == xml.dom.core.ELEMENT and child.tagName == name: |
| 424 | nodes.append(child) |
| 425 | for node in nodes: |
| 426 | source.removeChild(node) |
| 427 | dest.appendChild(node) |
| 428 | if sep: |
| 429 | dest.appendChild(doc.createTextNode(sep)) |
| 430 | |
| 431 | |
Fred Drake | fcc5910 | 1999-01-06 22:50:52 +0000 | [diff] [blame] | 432 | FIXUP_PARA_ELEMENTS = ( |
| 433 | "chapter", |
| 434 | "section", "subsection", "subsubsection", |
| 435 | "paragraph", "subparagraph") |
| 436 | |
| 437 | PARA_LEVEL_ELEMENTS = ( |
| 438 | "moduleinfo", "title", "opcodedesc", |
| 439 | "verbatim", "funcdesc", "methoddesc", "excdesc", "datadesc", |
| 440 | "funcdescni", "methoddescni", "excdescni", "datadescni", |
| 441 | "tableii", "tableiii", "tableiv", "localmoduletable", |
| 442 | "sectionauthor", |
| 443 | # include <para>, so we can just do it again to get subsequent paras: |
| 444 | "para", |
| 445 | ) |
| 446 | |
| 447 | PARA_LEVEL_PRECEEDERS = ( |
| 448 | "index", "indexii", "indexiii", "indexiv", |
| 449 | "stindex", "obindex", "COMMENT", "label", |
| 450 | ) |
| 451 | |
Fred Drake | aaed971 | 1998-12-10 20:25:30 +0000 | [diff] [blame] | 452 | def fixup_paras(doc): |
Fred Drake | fcc5910 | 1999-01-06 22:50:52 +0000 | [diff] [blame] | 453 | for child in doc.childNodes: |
| 454 | if child.nodeType == xml.dom.core.ELEMENT \ |
| 455 | and child.tagName in FIXUP_PARA_ELEMENTS: |
| 456 | fixup_paras_helper(doc, child) |
| 457 | descriptions = child.getElementsByTagName("description") |
| 458 | for description in descriptions: |
| 459 | if DEBUG_PARA_FIXER: |
| 460 | sys.stderr.write("-- Fixing up <description> element...\n") |
| 461 | fixup_paras_helper(doc, description) |
| 462 | |
| 463 | |
| 464 | def fixup_paras_helper(doc, container): |
| 465 | # document is already normalized |
| 466 | children = container.childNodes |
| 467 | start = 0 |
| 468 | start_fixed = 0 |
| 469 | i = 0 |
| 470 | SKIP_ELEMENTS = PARA_LEVEL_ELEMENTS + PARA_LEVEL_PRECEEDERS |
| 471 | for child in children: |
| 472 | if child.nodeType == xml.dom.core.ELEMENT: |
| 473 | if child.tagName in FIXUP_PARA_ELEMENTS: |
| 474 | fixup_paras_helper(doc, child) |
| 475 | break |
| 476 | elif child.tagName in SKIP_ELEMENTS: |
| 477 | if not start_fixed: |
| 478 | start = i + 1 |
| 479 | elif not start_fixed: |
| 480 | start_fixed = 1 |
| 481 | i = i + 1 |
| 482 | else: |
| 483 | if child.nodeType == xml.dom.core.TEXT \ |
| 484 | and string.strip(child.data) and not start_fixed: |
| 485 | start_fixed = 1 |
| 486 | i = i + 1 |
| 487 | if DEBUG_PARA_FIXER: |
| 488 | sys.stderr.write("fixup_paras_helper() called on <%s>; %d, %d\n" |
| 489 | % (container.tagName, start, i)) |
| 490 | if i > start: |
| 491 | # the first [start:i] children shoudl be rewritten as <para> elements |
| 492 | # start by breaking text nodes that contain \n\n+ into multiple nodes |
| 493 | nstart, i = skip_leading_nodes(container.childNodes, start, i) |
| 494 | if i > nstart: |
| 495 | build_para(doc, container, nstart, i) |
| 496 | fixup_paras_helper(doc, container) |
| 497 | |
| 498 | |
| 499 | def build_para(doc, parent, start, i): |
| 500 | children = parent.childNodes |
| 501 | # collect all children until \n\n+ is found in a text node or a |
| 502 | # PARA_LEVEL_ELEMENT is found. |
| 503 | after = start + 1 |
| 504 | have_last = 0 |
| 505 | BREAK_ELEMENTS = PARA_LEVEL_ELEMENTS + FIXUP_PARA_ELEMENTS |
| 506 | for j in range(start, i): |
| 507 | after = j + 1 |
| 508 | child = children[j] |
| 509 | nodeType = child.nodeType |
| 510 | if nodeType == xml.dom.core.ELEMENT: |
| 511 | if child.tagName in BREAK_ELEMENTS: |
| 512 | after = j |
| 513 | break |
| 514 | elif nodeType == xml.dom.core.TEXT: |
| 515 | pos = string.find(child.data, "\n\n") |
| 516 | if pos == 0: |
| 517 | after = j |
| 518 | break |
| 519 | if pos >= 1: |
| 520 | child.splitText(pos) |
| 521 | break |
| 522 | else: |
| 523 | have_last = 1 |
| 524 | if children[after - 1].nodeType == xml.dom.core.TEXT: |
| 525 | # we may need to split off trailing white space: |
| 526 | child = children[after - 1] |
| 527 | data = child.data |
| 528 | if string.rstrip(data) != data: |
| 529 | have_last = 0 |
| 530 | child.splitText(len(string.rstrip(data))) |
| 531 | children = parent.childNodes |
| 532 | para = doc.createElement("para") |
| 533 | prev = None |
| 534 | indexes = range(start, after) |
| 535 | indexes.reverse() |
| 536 | for j in indexes: |
| 537 | node = children[j] |
| 538 | parent.removeChild(node) |
| 539 | para.insertBefore(node, prev) |
| 540 | prev = node |
| 541 | if have_last: |
| 542 | parent.appendChild(para) |
| 543 | else: |
| 544 | parent.insertBefore(para, parent.childNodes[start]) |
| 545 | |
| 546 | |
| 547 | def skip_leading_nodes(children, start, i): |
| 548 | i = min(i, len(children)) |
| 549 | while i > start: |
| 550 | # skip over leading comments and whitespace: |
| 551 | try: |
| 552 | child = children[start] |
| 553 | except IndexError: |
| 554 | sys.stderr.write( |
| 555 | "skip_leading_nodes() failed at index %d\n" % start) |
| 556 | raise |
| 557 | nodeType = child.nodeType |
| 558 | if nodeType == xml.dom.core.COMMENT: |
| 559 | start = start + 1 |
| 560 | elif nodeType == xml.dom.core.TEXT: |
| 561 | data = child.data |
| 562 | shortened = string.lstrip(data) |
| 563 | if shortened: |
| 564 | if data != shortened: |
| 565 | # break into two nodes: whitespace and non-whitespace |
| 566 | child.splitText(len(data) - len(shortened)) |
| 567 | return start + 1, i + 1 |
| 568 | break |
| 569 | # all whitespace, just skip |
| 570 | start = start + 1 |
| 571 | elif nodeType == xml.dom.core.ELEMENT: |
| 572 | if child.tagName in PARA_LEVEL_ELEMENTS + PARA_LEVEL_PRECEEDERS: |
| 573 | start = start + 1 |
| 574 | else: |
| 575 | break |
| 576 | else: |
| 577 | break |
| 578 | return start, i |
Fred Drake | fba0ba2 | 1998-12-10 05:07:09 +0000 | [diff] [blame] | 579 | |
| 580 | |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 581 | _token_rx = re.compile(r"[a-zA-Z][a-zA-Z0-9.-]*$") |
Fred Drake | fcc5910 | 1999-01-06 22:50:52 +0000 | [diff] [blame] | 582 | |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 583 | def write_esis(doc, ofp, knownempty): |
| 584 | for node in doc.childNodes: |
| 585 | nodeType = node.nodeType |
| 586 | if nodeType == xml.dom.core.ELEMENT: |
| 587 | gi = node.tagName |
| 588 | if knownempty(gi): |
| 589 | if node.hasChildNodes(): |
| 590 | raise ValueError, "declared-empty node has children" |
| 591 | ofp.write("e\n") |
| 592 | for k, v in node.attributes.items(): |
| 593 | value = v.value |
| 594 | if _token_rx.match(value): |
| 595 | dtype = "TOKEN" |
| 596 | else: |
| 597 | dtype = "CDATA" |
| 598 | ofp.write("A%s %s %s\n" % (k, dtype, esistools.encode(value))) |
| 599 | ofp.write("(%s\n" % gi) |
| 600 | write_esis(node, ofp, knownempty) |
| 601 | ofp.write(")%s\n" % gi) |
| 602 | elif nodeType == xml.dom.core.TEXT: |
| 603 | ofp.write("-%s\n" % esistools.encode(node.data)) |
| 604 | else: |
| 605 | raise RuntimeError, "unsupported node type: %s" % nodeType |
| 606 | |
| 607 | |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 608 | def convert(ifp, ofp): |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 609 | p = esistools.ExtendedEsisBuilder() |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 610 | p.feed(ifp.read()) |
| 611 | doc = p.document |
Fred Drake | 1ff6db4 | 1998-11-23 23:10:35 +0000 | [diff] [blame] | 612 | normalize(doc) |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 613 | handle_args(doc) |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 614 | simplify(doc) |
| 615 | handle_labels(doc) |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 616 | handle_appendix(doc) |
Fred Drake | 1ff6db4 | 1998-11-23 23:10:35 +0000 | [diff] [blame] | 617 | fixup_trailing_whitespace(doc, { |
| 618 | "abstract": "\n", |
| 619 | "title": "", |
| 620 | "chapter": "\n\n", |
| 621 | "section": "\n\n", |
| 622 | "subsection": "\n\n", |
| 623 | "subsubsection": "\n\n", |
| 624 | "paragraph": "\n\n", |
| 625 | "subparagraph": "\n\n", |
| 626 | }) |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 627 | cleanup_root_text(doc) |
Fred Drake | 1ff6db4 | 1998-11-23 23:10:35 +0000 | [diff] [blame] | 628 | cleanup_trailing_parens(doc, ["function", "method", "cfunction"]) |
Fred Drake | fba0ba2 | 1998-12-10 05:07:09 +0000 | [diff] [blame] | 629 | cleanup_synopses(doc) |
Fred Drake | aaed971 | 1998-12-10 20:25:30 +0000 | [diff] [blame] | 630 | normalize(doc) |
| 631 | fixup_paras(doc) |
Fred Drake | f8ebb55 | 1999-01-14 19:45:38 +0000 | [diff] [blame] | 632 | remap_element_names(doc, { |
| 633 | "tableii": ("table", {"cols": "2"}), |
| 634 | "tableiii": ("table", {"cols": "3"}), |
| 635 | "tableiv": ("table", {"cols": "4"}), |
| 636 | "lineii": ("row", {}), |
| 637 | "lineiii": ("row", {}), |
| 638 | "lineiv": ("row", {}), |
| 639 | }) |
| 640 | fixup_table_structures(doc) |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 641 | # |
| 642 | d = {} |
| 643 | for gi in p.get_empties(): |
| 644 | d[gi] = gi |
| 645 | knownempty = d.has_key |
| 646 | # |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 647 | try: |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 648 | write_esis(doc, ofp, knownempty) |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 649 | except IOError, (err, msg): |
| 650 | # Ignore EPIPE; it just means that whoever we're writing to stopped |
| 651 | # reading. The rest of the output would be ignored. All other errors |
| 652 | # should still be reported, |
| 653 | if err != errno.EPIPE: |
| 654 | raise |
| 655 | |
| 656 | |
| 657 | def main(): |
| 658 | if len(sys.argv) == 1: |
| 659 | ifp = sys.stdin |
| 660 | ofp = sys.stdout |
| 661 | elif len(sys.argv) == 2: |
| 662 | ifp = open(sys.argv[1]) |
| 663 | ofp = sys.stdout |
| 664 | elif len(sys.argv) == 3: |
| 665 | ifp = open(sys.argv[1]) |
| 666 | ofp = open(sys.argv[2], "w") |
| 667 | else: |
| 668 | usage() |
| 669 | sys.exit(2) |
| 670 | convert(ifp, ofp) |
| 671 | |
| 672 | |
| 673 | if __name__ == "__main__": |
| 674 | main() |