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): |
Fred Drake | 2664db9 | 1999-01-19 21:46:48 +0000 | [diff] [blame] | 168 | for node in doc.childNodes: |
| 169 | if node.nodeType == xml.dom.core.ELEMENT: |
| 170 | labels = node.getElementsByTagName("label") |
| 171 | for label in labels: |
| 172 | id = label.getAttribute("id") |
| 173 | if not id: |
| 174 | continue |
| 175 | parent = label.parentNode |
| 176 | if parent.tagName == "title": |
| 177 | parent.parentNode.setAttribute("id", id) |
| 178 | else: |
| 179 | parent.setAttribute("id", id) |
| 180 | # now, remove <label id="..."/> from parent: |
| 181 | parent.removeChild(label) |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 182 | |
| 183 | |
Fred Drake | 1ff6db4 | 1998-11-23 23:10:35 +0000 | [diff] [blame] | 184 | def fixup_trailing_whitespace(doc, wsmap): |
| 185 | queue = [doc] |
| 186 | while queue: |
| 187 | node = queue[0] |
| 188 | del queue[0] |
| 189 | if node.nodeType == xml.dom.core.ELEMENT \ |
| 190 | and wsmap.has_key(node.tagName): |
| 191 | ws = wsmap[node.tagName] |
| 192 | children = node.childNodes |
| 193 | children.reverse() |
| 194 | if children[0].nodeType == xml.dom.core.TEXT: |
| 195 | data = string.rstrip(children[0].data) + ws |
| 196 | children[0].data = data |
| 197 | children.reverse() |
| 198 | # hack to get the title in place: |
| 199 | if node.tagName == "title" \ |
| 200 | and node.parentNode.firstChild.nodeType == xml.dom.core.ELEMENT: |
| 201 | node.parentNode.insertBefore(doc.createText("\n "), |
| 202 | node.parentNode.firstChild) |
| 203 | for child in node.childNodes: |
| 204 | if child.nodeType == xml.dom.core.ELEMENT: |
| 205 | queue.append(child) |
| 206 | |
| 207 | |
| 208 | def normalize(doc): |
| 209 | for node in doc.childNodes: |
| 210 | if node.nodeType == xml.dom.core.ELEMENT: |
| 211 | node.normalize() |
| 212 | |
| 213 | |
| 214 | def cleanup_trailing_parens(doc, element_names): |
| 215 | d = {} |
| 216 | for gi in element_names: |
| 217 | d[gi] = gi |
| 218 | rewrite_element = d.has_key |
| 219 | queue = [] |
| 220 | for node in doc.childNodes: |
| 221 | if node.nodeType == xml.dom.core.ELEMENT: |
| 222 | queue.append(node) |
| 223 | while queue: |
| 224 | node = queue[0] |
| 225 | del queue[0] |
| 226 | if rewrite_element(node.tagName): |
| 227 | children = node.childNodes |
| 228 | if len(children) == 1 \ |
| 229 | and children[0].nodeType == xml.dom.core.TEXT: |
| 230 | data = children[0].data |
| 231 | if data[-2:] == "()": |
| 232 | children[0].data = data[:-2] |
| 233 | else: |
| 234 | for child in node.childNodes: |
| 235 | if child.nodeType == xml.dom.core.ELEMENT: |
| 236 | queue.append(child) |
| 237 | |
| 238 | |
Fred Drake | aaed971 | 1998-12-10 20:25:30 +0000 | [diff] [blame] | 239 | def contents_match(left, right): |
| 240 | left_children = left.childNodes |
| 241 | right_children = right.childNodes |
| 242 | if len(left_children) != len(right_children): |
| 243 | return 0 |
| 244 | for l, r in map(None, left_children, right_children): |
| 245 | nodeType = l.nodeType |
| 246 | if nodeType != r.nodeType: |
| 247 | return 0 |
| 248 | if nodeType == xml.dom.core.ELEMENT: |
| 249 | if l.tagName != r.tagName: |
| 250 | return 0 |
| 251 | # should check attributes, but that's not a problem here |
| 252 | if not contents_match(l, r): |
| 253 | return 0 |
| 254 | elif nodeType == xml.dom.core.TEXT: |
| 255 | if l.data != r.data: |
| 256 | return 0 |
| 257 | else: |
| 258 | # not quite right, but good enough |
| 259 | return 0 |
| 260 | return 1 |
| 261 | |
| 262 | |
| 263 | def create_module_info(doc, section): |
| 264 | # Heavy. |
| 265 | node = extract_first_element(section, "modulesynopsis") |
| 266 | if node is None: |
| 267 | return |
| 268 | node._node.name = "synopsis" |
| 269 | lastchild = node.childNodes[-1] |
| 270 | if lastchild.nodeType == xml.dom.core.TEXT \ |
| 271 | and lastchild.data[-1:] == ".": |
| 272 | lastchild.data = lastchild.data[:-1] |
| 273 | if section.tagName == "section": |
| 274 | modinfo_pos = 2 |
| 275 | modinfo = doc.createElement("moduleinfo") |
| 276 | moddecl = extract_first_element(section, "declaremodule") |
| 277 | name = None |
| 278 | if moddecl: |
| 279 | modinfo.appendChild(doc.createTextNode("\n ")) |
| 280 | name = moddecl.attributes["name"].value |
| 281 | namenode = doc.createElement("name") |
| 282 | namenode.appendChild(doc.createTextNode(name)) |
| 283 | modinfo.appendChild(namenode) |
| 284 | type = moddecl.attributes.get("type") |
| 285 | if type: |
| 286 | type = type.value |
| 287 | modinfo.appendChild(doc.createTextNode("\n ")) |
| 288 | typenode = doc.createElement("type") |
| 289 | typenode.appendChild(doc.createTextNode(type)) |
| 290 | modinfo.appendChild(typenode) |
| 291 | title = get_first_element(section, "title") |
| 292 | if title: |
| 293 | children = title.childNodes |
| 294 | if len(children) >= 2 \ |
| 295 | and children[0].nodeType == xml.dom.core.ELEMENT \ |
| 296 | and children[0].tagName == "module" \ |
| 297 | and children[0].childNodes[0].data == name: |
| 298 | # this is it; morph the <title> into <short-synopsis> |
| 299 | first_data = children[1] |
| 300 | if first_data.data[:4] == " ---": |
| 301 | first_data.data = string.lstrip(first_data.data[4:]) |
| 302 | title._node.name = "short-synopsis" |
| 303 | if children[-1].data[-1:] == ".": |
| 304 | children[-1].data = children[-1].data[:-1] |
| 305 | section.removeChild(title) |
| 306 | section.removeChild(section.childNodes[0]) |
| 307 | title.removeChild(children[0]) |
| 308 | modinfo_pos = 0 |
| 309 | else: |
| 310 | sys.stderr.write( |
| 311 | "module name in title doesn't match" |
| 312 | " <declaremodule>; no <short-synopsis>\n") |
| 313 | else: |
| 314 | sys.stderr.write( |
| 315 | "Unexpected condition: <section> without <title>\n") |
| 316 | modinfo.appendChild(doc.createTextNode("\n ")) |
| 317 | modinfo.appendChild(node) |
| 318 | if title and not contents_match(title, node): |
| 319 | # The short synopsis is actually different, |
| 320 | # and needs to be stored: |
| 321 | modinfo.appendChild(doc.createTextNode("\n ")) |
| 322 | modinfo.appendChild(title) |
| 323 | modinfo.appendChild(doc.createTextNode("\n ")) |
| 324 | section.insertBefore(modinfo, section.childNodes[modinfo_pos]) |
| 325 | section.insertBefore(doc.createTextNode("\n "), modinfo) |
| 326 | |
| 327 | |
Fred Drake | fba0ba2 | 1998-12-10 05:07:09 +0000 | [diff] [blame] | 328 | def cleanup_synopses(doc): |
Fred Drake | aaed971 | 1998-12-10 20:25:30 +0000 | [diff] [blame] | 329 | for node in doc.childNodes: |
| 330 | if node.nodeType == xml.dom.core.ELEMENT \ |
| 331 | and node.tagName == "section": |
| 332 | create_module_info(doc, node) |
| 333 | |
| 334 | |
Fred Drake | f8ebb55 | 1999-01-14 19:45:38 +0000 | [diff] [blame] | 335 | def remap_element_names(root, name_map): |
| 336 | queue = [] |
| 337 | for child in root.childNodes: |
| 338 | if child.nodeType == xml.dom.core.ELEMENT: |
| 339 | queue.append(child) |
| 340 | while queue: |
| 341 | node = queue.pop() |
| 342 | tagName = node.tagName |
| 343 | if name_map.has_key(tagName): |
| 344 | name, attrs = name_map[tagName] |
| 345 | node._node.name = name |
| 346 | for attr, value in attrs.items(): |
| 347 | node.setAttribute(attr, value) |
| 348 | for child in node.childNodes: |
| 349 | if child.nodeType == xml.dom.core.ELEMENT: |
| 350 | queue.append(child) |
| 351 | |
| 352 | |
| 353 | def fixup_table_structures(doc): |
| 354 | # must be done after remap_element_names(), or the tables won't be found |
| 355 | for child in doc.childNodes: |
| 356 | if child.nodeType == xml.dom.core.ELEMENT: |
| 357 | tables = child.getElementsByTagName("table") |
| 358 | for table in tables: |
| 359 | fixup_table(doc, table) |
| 360 | |
| 361 | def fixup_table(doc, table): |
| 362 | # create the table head |
| 363 | thead = doc.createElement("thead") |
| 364 | row = doc.createElement("row") |
| 365 | move_elements_by_name(doc, table, row, "entry") |
| 366 | thead.appendChild(doc.createTextNode("\n ")) |
| 367 | thead.appendChild(row) |
| 368 | thead.appendChild(doc.createTextNode("\n ")) |
| 369 | # create the table body |
| 370 | tbody = doc.createElement("tbody") |
| 371 | prev_row = None |
| 372 | last_was_hline = 0 |
| 373 | children = table.childNodes |
| 374 | for child in children: |
| 375 | if child.nodeType == xml.dom.core.ELEMENT: |
| 376 | tagName = child.tagName |
| 377 | if tagName == "hline" and prev_row is not None: |
| 378 | prev_row.setAttribute("rowsep", "1") |
| 379 | elif tagName == "row": |
| 380 | prev_row = child |
| 381 | # save the rows: |
| 382 | tbody.appendChild(doc.createTextNode("\n ")) |
| 383 | move_elements_by_name(doc, table, tbody, "row", sep="\n ") |
| 384 | # and toss the rest: |
| 385 | while children: |
| 386 | child = children[0] |
| 387 | nodeType = child.nodeType |
| 388 | if nodeType == xml.dom.core.TEXT: |
| 389 | if string.strip(child.data): |
| 390 | raise ConversionError("unexpected free data in table") |
| 391 | table.removeChild(child) |
| 392 | continue |
| 393 | if nodeType == xml.dom.core.ELEMENT: |
| 394 | if child.tagName != "hline": |
| 395 | raise ConversionError( |
| 396 | "unexpected <%s> in table" % child.tagName) |
| 397 | table.removeChild(child) |
| 398 | continue |
| 399 | raise ConversionError( |
| 400 | "unexpected %s node in table" % child.__class__.__name__) |
| 401 | # nothing left in the <table>; add the <thead> and <tbody> |
| 402 | tgroup = doc.createElement("tgroup") |
| 403 | tgroup.appendChild(doc.createTextNode("\n ")) |
| 404 | tgroup.appendChild(thead) |
| 405 | tgroup.appendChild(doc.createTextNode("\n ")) |
| 406 | tgroup.appendChild(tbody) |
| 407 | tgroup.appendChild(doc.createTextNode("\n ")) |
| 408 | table.appendChild(tgroup) |
| 409 | # now make the <entry>s look nice: |
| 410 | for row in table.getElementsByTagName("row"): |
| 411 | fixup_row(doc, row) |
| 412 | |
| 413 | |
| 414 | def fixup_row(doc, row): |
| 415 | entries = [] |
| 416 | map(entries.append, row.childNodes[1:]) |
| 417 | for entry in entries: |
| 418 | row.insertBefore(doc.createTextNode("\n "), entry) |
| 419 | # row.appendChild(doc.createTextNode("\n ")) |
| 420 | |
| 421 | |
| 422 | def move_elements_by_name(doc, source, dest, name, sep=None): |
| 423 | nodes = [] |
| 424 | for child in source.childNodes: |
| 425 | if child.nodeType == xml.dom.core.ELEMENT and child.tagName == name: |
| 426 | nodes.append(child) |
| 427 | for node in nodes: |
| 428 | source.removeChild(node) |
| 429 | dest.appendChild(node) |
| 430 | if sep: |
| 431 | dest.appendChild(doc.createTextNode(sep)) |
| 432 | |
| 433 | |
Fred Drake | fcc5910 | 1999-01-06 22:50:52 +0000 | [diff] [blame] | 434 | FIXUP_PARA_ELEMENTS = ( |
| 435 | "chapter", |
| 436 | "section", "subsection", "subsubsection", |
| 437 | "paragraph", "subparagraph") |
| 438 | |
| 439 | PARA_LEVEL_ELEMENTS = ( |
| 440 | "moduleinfo", "title", "opcodedesc", |
| 441 | "verbatim", "funcdesc", "methoddesc", "excdesc", "datadesc", |
| 442 | "funcdescni", "methoddescni", "excdescni", "datadescni", |
| 443 | "tableii", "tableiii", "tableiv", "localmoduletable", |
| 444 | "sectionauthor", |
| 445 | # include <para>, so we can just do it again to get subsequent paras: |
| 446 | "para", |
| 447 | ) |
| 448 | |
| 449 | PARA_LEVEL_PRECEEDERS = ( |
| 450 | "index", "indexii", "indexiii", "indexiv", |
| 451 | "stindex", "obindex", "COMMENT", "label", |
| 452 | ) |
| 453 | |
Fred Drake | aaed971 | 1998-12-10 20:25:30 +0000 | [diff] [blame] | 454 | def fixup_paras(doc): |
Fred Drake | fcc5910 | 1999-01-06 22:50:52 +0000 | [diff] [blame] | 455 | for child in doc.childNodes: |
| 456 | if child.nodeType == xml.dom.core.ELEMENT \ |
| 457 | and child.tagName in FIXUP_PARA_ELEMENTS: |
| 458 | fixup_paras_helper(doc, child) |
| 459 | descriptions = child.getElementsByTagName("description") |
| 460 | for description in descriptions: |
| 461 | if DEBUG_PARA_FIXER: |
| 462 | sys.stderr.write("-- Fixing up <description> element...\n") |
| 463 | fixup_paras_helper(doc, description) |
| 464 | |
| 465 | |
| 466 | def fixup_paras_helper(doc, container): |
| 467 | # document is already normalized |
| 468 | children = container.childNodes |
| 469 | start = 0 |
| 470 | start_fixed = 0 |
| 471 | i = 0 |
| 472 | SKIP_ELEMENTS = PARA_LEVEL_ELEMENTS + PARA_LEVEL_PRECEEDERS |
| 473 | for child in children: |
| 474 | if child.nodeType == xml.dom.core.ELEMENT: |
| 475 | if child.tagName in FIXUP_PARA_ELEMENTS: |
| 476 | fixup_paras_helper(doc, child) |
| 477 | break |
| 478 | elif child.tagName in SKIP_ELEMENTS: |
| 479 | if not start_fixed: |
| 480 | start = i + 1 |
| 481 | elif not start_fixed: |
| 482 | start_fixed = 1 |
| 483 | i = i + 1 |
| 484 | else: |
| 485 | if child.nodeType == xml.dom.core.TEXT \ |
| 486 | and string.strip(child.data) and not start_fixed: |
| 487 | start_fixed = 1 |
| 488 | i = i + 1 |
| 489 | if DEBUG_PARA_FIXER: |
| 490 | sys.stderr.write("fixup_paras_helper() called on <%s>; %d, %d\n" |
| 491 | % (container.tagName, start, i)) |
| 492 | if i > start: |
| 493 | # the first [start:i] children shoudl be rewritten as <para> elements |
| 494 | # start by breaking text nodes that contain \n\n+ into multiple nodes |
| 495 | nstart, i = skip_leading_nodes(container.childNodes, start, i) |
| 496 | if i > nstart: |
| 497 | build_para(doc, container, nstart, i) |
| 498 | fixup_paras_helper(doc, container) |
| 499 | |
| 500 | |
| 501 | def build_para(doc, parent, start, i): |
| 502 | children = parent.childNodes |
| 503 | # collect all children until \n\n+ is found in a text node or a |
| 504 | # PARA_LEVEL_ELEMENT is found. |
| 505 | after = start + 1 |
| 506 | have_last = 0 |
| 507 | BREAK_ELEMENTS = PARA_LEVEL_ELEMENTS + FIXUP_PARA_ELEMENTS |
| 508 | for j in range(start, i): |
| 509 | after = j + 1 |
| 510 | child = children[j] |
| 511 | nodeType = child.nodeType |
| 512 | if nodeType == xml.dom.core.ELEMENT: |
| 513 | if child.tagName in BREAK_ELEMENTS: |
| 514 | after = j |
| 515 | break |
| 516 | elif nodeType == xml.dom.core.TEXT: |
| 517 | pos = string.find(child.data, "\n\n") |
| 518 | if pos == 0: |
| 519 | after = j |
| 520 | break |
| 521 | if pos >= 1: |
| 522 | child.splitText(pos) |
| 523 | break |
| 524 | else: |
| 525 | have_last = 1 |
| 526 | if children[after - 1].nodeType == xml.dom.core.TEXT: |
| 527 | # we may need to split off trailing white space: |
| 528 | child = children[after - 1] |
| 529 | data = child.data |
| 530 | if string.rstrip(data) != data: |
| 531 | have_last = 0 |
| 532 | child.splitText(len(string.rstrip(data))) |
| 533 | children = parent.childNodes |
| 534 | para = doc.createElement("para") |
| 535 | prev = None |
| 536 | indexes = range(start, after) |
| 537 | indexes.reverse() |
| 538 | for j in indexes: |
| 539 | node = children[j] |
| 540 | parent.removeChild(node) |
| 541 | para.insertBefore(node, prev) |
| 542 | prev = node |
| 543 | if have_last: |
| 544 | parent.appendChild(para) |
| 545 | else: |
| 546 | parent.insertBefore(para, parent.childNodes[start]) |
| 547 | |
| 548 | |
| 549 | def skip_leading_nodes(children, start, i): |
| 550 | i = min(i, len(children)) |
| 551 | while i > start: |
| 552 | # skip over leading comments and whitespace: |
| 553 | try: |
| 554 | child = children[start] |
| 555 | except IndexError: |
| 556 | sys.stderr.write( |
| 557 | "skip_leading_nodes() failed at index %d\n" % start) |
| 558 | raise |
| 559 | nodeType = child.nodeType |
| 560 | if nodeType == xml.dom.core.COMMENT: |
| 561 | start = start + 1 |
| 562 | elif nodeType == xml.dom.core.TEXT: |
| 563 | data = child.data |
| 564 | shortened = string.lstrip(data) |
| 565 | if shortened: |
| 566 | if data != shortened: |
| 567 | # break into two nodes: whitespace and non-whitespace |
| 568 | child.splitText(len(data) - len(shortened)) |
| 569 | return start + 1, i + 1 |
| 570 | break |
| 571 | # all whitespace, just skip |
| 572 | start = start + 1 |
| 573 | elif nodeType == xml.dom.core.ELEMENT: |
| 574 | if child.tagName in PARA_LEVEL_ELEMENTS + PARA_LEVEL_PRECEEDERS: |
| 575 | start = start + 1 |
| 576 | else: |
| 577 | break |
| 578 | else: |
| 579 | break |
| 580 | return start, i |
Fred Drake | fba0ba2 | 1998-12-10 05:07:09 +0000 | [diff] [blame] | 581 | |
| 582 | |
Fred Drake | d24167b | 1999-01-14 21:18:03 +0000 | [diff] [blame] | 583 | def fixup_rfc_references(doc): |
| 584 | rfc_nodes = [] |
| 585 | for child in doc.childNodes: |
| 586 | if child.nodeType == xml.dom.core.ELEMENT: |
| 587 | kids = child.getElementsByTagName("rfc") |
| 588 | for k in kids: |
| 589 | rfc_nodes.append(k) |
| 590 | for rfc_node in rfc_nodes: |
| 591 | rfc_node.appendChild(doc.createTextNode( |
| 592 | "RFC " + rfc_node.getAttribute("num"))) |
| 593 | |
| 594 | |
| 595 | def fixup_signatures(doc): |
| 596 | for child in doc.childNodes: |
| 597 | if child.nodeType == xml.dom.core.ELEMENT: |
| 598 | args = child.getElementsByTagName("args") |
| 599 | for arg in args: |
| 600 | fixup_args(doc, arg) |
| 601 | args = child.getElementsByTagName("constructor-args") |
| 602 | for arg in args: |
| 603 | fixup_args(doc, arg) |
| 604 | arg.normalize() |
| 605 | |
| 606 | |
| 607 | def fixup_args(doc, arglist): |
| 608 | for child in arglist.childNodes: |
| 609 | if child.nodeType == xml.dom.core.ELEMENT \ |
| 610 | and child.tagName == "optional": |
| 611 | # found it; fix and return |
| 612 | arglist.insertBefore(doc.createTextNode("["), child) |
| 613 | optkids = child.childNodes |
| 614 | while optkids: |
| 615 | k = optkids[0] |
| 616 | child.removeChild(k) |
| 617 | arglist.insertBefore(k, child) |
| 618 | arglist.insertBefore(doc.createTextNode("]"), child) |
| 619 | arglist.removeChild(child) |
| 620 | return fixup_args(doc, arglist) |
| 621 | |
| 622 | |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 623 | _token_rx = re.compile(r"[a-zA-Z][a-zA-Z0-9.-]*$") |
Fred Drake | fcc5910 | 1999-01-06 22:50:52 +0000 | [diff] [blame] | 624 | |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 625 | def write_esis(doc, ofp, knownempty): |
| 626 | for node in doc.childNodes: |
| 627 | nodeType = node.nodeType |
| 628 | if nodeType == xml.dom.core.ELEMENT: |
| 629 | gi = node.tagName |
| 630 | if knownempty(gi): |
| 631 | if node.hasChildNodes(): |
| 632 | raise ValueError, "declared-empty node has children" |
| 633 | ofp.write("e\n") |
| 634 | for k, v in node.attributes.items(): |
| 635 | value = v.value |
| 636 | if _token_rx.match(value): |
| 637 | dtype = "TOKEN" |
| 638 | else: |
| 639 | dtype = "CDATA" |
| 640 | ofp.write("A%s %s %s\n" % (k, dtype, esistools.encode(value))) |
| 641 | ofp.write("(%s\n" % gi) |
| 642 | write_esis(node, ofp, knownempty) |
| 643 | ofp.write(")%s\n" % gi) |
| 644 | elif nodeType == xml.dom.core.TEXT: |
| 645 | ofp.write("-%s\n" % esistools.encode(node.data)) |
| 646 | else: |
| 647 | raise RuntimeError, "unsupported node type: %s" % nodeType |
| 648 | |
| 649 | |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 650 | def convert(ifp, ofp): |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 651 | p = esistools.ExtendedEsisBuilder() |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 652 | p.feed(ifp.read()) |
| 653 | doc = p.document |
Fred Drake | 1ff6db4 | 1998-11-23 23:10:35 +0000 | [diff] [blame] | 654 | normalize(doc) |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 655 | handle_args(doc) |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 656 | simplify(doc) |
| 657 | handle_labels(doc) |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 658 | handle_appendix(doc) |
Fred Drake | 1ff6db4 | 1998-11-23 23:10:35 +0000 | [diff] [blame] | 659 | fixup_trailing_whitespace(doc, { |
| 660 | "abstract": "\n", |
| 661 | "title": "", |
| 662 | "chapter": "\n\n", |
| 663 | "section": "\n\n", |
| 664 | "subsection": "\n\n", |
| 665 | "subsubsection": "\n\n", |
| 666 | "paragraph": "\n\n", |
| 667 | "subparagraph": "\n\n", |
| 668 | }) |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 669 | cleanup_root_text(doc) |
Fred Drake | 1ff6db4 | 1998-11-23 23:10:35 +0000 | [diff] [blame] | 670 | cleanup_trailing_parens(doc, ["function", "method", "cfunction"]) |
Fred Drake | fba0ba2 | 1998-12-10 05:07:09 +0000 | [diff] [blame] | 671 | cleanup_synopses(doc) |
Fred Drake | aaed971 | 1998-12-10 20:25:30 +0000 | [diff] [blame] | 672 | normalize(doc) |
| 673 | fixup_paras(doc) |
Fred Drake | f8ebb55 | 1999-01-14 19:45:38 +0000 | [diff] [blame] | 674 | remap_element_names(doc, { |
| 675 | "tableii": ("table", {"cols": "2"}), |
| 676 | "tableiii": ("table", {"cols": "3"}), |
| 677 | "tableiv": ("table", {"cols": "4"}), |
| 678 | "lineii": ("row", {}), |
| 679 | "lineiii": ("row", {}), |
| 680 | "lineiv": ("row", {}), |
Fred Drake | d6ced7d | 1999-01-19 17:11:23 +0000 | [diff] [blame] | 681 | "refmodule": ("module", {"link": "link"}), |
Fred Drake | f8ebb55 | 1999-01-14 19:45:38 +0000 | [diff] [blame] | 682 | }) |
| 683 | fixup_table_structures(doc) |
Fred Drake | d24167b | 1999-01-14 21:18:03 +0000 | [diff] [blame] | 684 | fixup_rfc_references(doc) |
| 685 | fixup_signatures(doc) |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 686 | # |
| 687 | d = {} |
| 688 | for gi in p.get_empties(): |
| 689 | d[gi] = gi |
Fred Drake | d24167b | 1999-01-14 21:18:03 +0000 | [diff] [blame] | 690 | if d.has_key("rfc"): |
| 691 | del d["rfc"] |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 692 | knownempty = d.has_key |
| 693 | # |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 694 | try: |
Fred Drake | 4db5b46 | 1998-12-01 19:03:01 +0000 | [diff] [blame] | 695 | write_esis(doc, ofp, knownempty) |
Fred Drake | 0320473 | 1998-11-23 17:02:03 +0000 | [diff] [blame] | 696 | except IOError, (err, msg): |
| 697 | # Ignore EPIPE; it just means that whoever we're writing to stopped |
| 698 | # reading. The rest of the output would be ignored. All other errors |
| 699 | # should still be reported, |
| 700 | if err != errno.EPIPE: |
| 701 | raise |
| 702 | |
| 703 | |
| 704 | def main(): |
| 705 | if len(sys.argv) == 1: |
| 706 | ifp = sys.stdin |
| 707 | ofp = sys.stdout |
| 708 | elif len(sys.argv) == 2: |
| 709 | ifp = open(sys.argv[1]) |
| 710 | ofp = sys.stdout |
| 711 | elif len(sys.argv) == 3: |
| 712 | ifp = open(sys.argv[1]) |
| 713 | ofp = open(sys.argv[2], "w") |
| 714 | else: |
| 715 | usage() |
| 716 | sys.exit(2) |
| 717 | convert(ifp, ofp) |
| 718 | |
| 719 | |
| 720 | if __name__ == "__main__": |
| 721 | main() |