paul@11 | 1 | #!/usr/bin/env python |
paul@11 | 2 | |
paul@11 | 3 | """ |
paul@11 | 4 | Moin wiki format document tree nodes. |
paul@11 | 5 | |
paul@316 | 6 | Copyright (C) 2017, 2018, 2019, 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk> |
paul@11 | 7 | |
paul@11 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@11 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@11 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@11 | 11 | version. |
paul@11 | 12 | |
paul@11 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@11 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@11 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@11 | 16 | details. |
paul@11 | 17 | |
paul@11 | 18 | You should have received a copy of the GNU General Public License along with |
paul@11 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@11 | 20 | """ |
paul@11 | 21 | |
paul@11 | 22 | class Container: |
paul@11 | 23 | |
paul@11 | 24 | "A container of document nodes." |
paul@11 | 25 | |
paul@11 | 26 | def __init__(self, nodes): |
paul@11 | 27 | self.nodes = nodes |
paul@11 | 28 | |
paul@67 | 29 | # In principle, allow blocks within containers. Some nodes may forbid |
paul@67 | 30 | # them to simplify the document structure. |
paul@67 | 31 | |
paul@67 | 32 | self.allow_blocks = True |
paul@67 | 33 | |
paul@11 | 34 | def append(self, node): |
paul@11 | 35 | self.nodes.append(node) |
paul@11 | 36 | |
paul@24 | 37 | def append_many(self, nodes): |
paul@24 | 38 | for node in nodes: |
paul@24 | 39 | self.append(node) |
paul@24 | 40 | |
paul@307 | 41 | def add(self, node): |
paul@307 | 42 | last = self.node(-1) |
paul@307 | 43 | if last and last.empty(): |
paul@307 | 44 | self.nodes[-1] = node |
paul@307 | 45 | else: |
paul@307 | 46 | self.append(node) |
paul@15 | 47 | |
paul@106 | 48 | def append_inline(self, node): |
paul@106 | 49 | |
paul@106 | 50 | "Append 'node' inline within the appropriate container." |
paul@106 | 51 | |
paul@106 | 52 | n = self.append_point() |
paul@106 | 53 | |
paul@106 | 54 | # Redirect the addition if another container is to accept the node. |
paul@106 | 55 | |
paul@106 | 56 | if n is not self: |
paul@106 | 57 | n.append_inline(node) |
paul@106 | 58 | |
paul@106 | 59 | # Otherwise, append within this container. |
paul@106 | 60 | |
paul@106 | 61 | else: |
paul@106 | 62 | n.append(node) |
paul@11 | 63 | |
paul@24 | 64 | def append_inline_many(self, nodes): |
paul@24 | 65 | for node in nodes: |
paul@24 | 66 | self.append_inline(node) |
paul@24 | 67 | |
paul@106 | 68 | def append_point(self): |
paul@106 | 69 | |
paul@106 | 70 | "Return the container to which inline nodes are added." |
paul@106 | 71 | |
paul@271 | 72 | last = self.node(-1) |
paul@271 | 73 | |
paul@271 | 74 | if isinstance(last, Block): |
paul@271 | 75 | return last |
paul@271 | 76 | else: |
paul@271 | 77 | return self |
paul@106 | 78 | |
paul@11 | 79 | def empty(self): |
paul@11 | 80 | return not self.nodes |
paul@11 | 81 | |
paul@162 | 82 | def insert_after(self, old, new): |
paul@162 | 83 | |
paul@162 | 84 | "Insert after 'old' in the children the 'new' node." |
paul@162 | 85 | |
paul@162 | 86 | index = self.nodes.index(old) |
paul@162 | 87 | self.nodes.insert(index + 1, new) |
paul@162 | 88 | |
paul@15 | 89 | def node(self, index): |
paul@15 | 90 | try: |
paul@15 | 91 | return self.nodes[index] |
paul@15 | 92 | except IndexError: |
paul@15 | 93 | return None |
paul@14 | 94 | |
paul@11 | 95 | def normalise(self): |
paul@11 | 96 | |
paul@11 | 97 | "Combine adjacent text nodes." |
paul@11 | 98 | |
paul@11 | 99 | nodes = self.nodes |
paul@11 | 100 | self.nodes = [] |
paul@11 | 101 | text = None |
paul@11 | 102 | |
paul@11 | 103 | for node in nodes: |
paul@11 | 104 | |
paul@11 | 105 | # Open a text node or merge text into an open node. |
paul@11 | 106 | |
paul@11 | 107 | if isinstance(node, Text): |
paul@11 | 108 | if not text: |
paul@11 | 109 | text = node |
paul@11 | 110 | else: |
paul@11 | 111 | text.merge(node) |
paul@11 | 112 | |
paul@11 | 113 | # Close any open text node and append the current node. |
paul@11 | 114 | |
paul@11 | 115 | else: |
paul@11 | 116 | if text: |
paul@11 | 117 | self.append(text) |
paul@11 | 118 | text = None |
paul@11 | 119 | self.append(node) |
paul@11 | 120 | |
paul@11 | 121 | # Add any open text node. |
paul@11 | 122 | |
paul@11 | 123 | if text: |
paul@11 | 124 | self.append(text) |
paul@11 | 125 | |
paul@162 | 126 | def remove(self, node): |
paul@162 | 127 | |
paul@162 | 128 | "Remove 'node' from the children." |
paul@162 | 129 | |
paul@162 | 130 | self.nodes.remove(node) |
paul@162 | 131 | |
paul@110 | 132 | def replace(self, old, new): |
paul@110 | 133 | |
paul@110 | 134 | "Replace 'old' with 'new' in the children." |
paul@110 | 135 | |
paul@110 | 136 | i = self.nodes.index(old) |
paul@110 | 137 | self.nodes[i] = new |
paul@110 | 138 | |
paul@162 | 139 | def split_at(self, node): |
paul@162 | 140 | |
paul@162 | 141 | """ |
paul@162 | 142 | Split the container at 'node', returning a new container holding the |
paul@162 | 143 | nodes following 'node' that are moved from this container. |
paul@162 | 144 | """ |
paul@162 | 145 | |
paul@162 | 146 | i = self.nodes.index(node) |
paul@162 | 147 | following = self.__class__(self.nodes[i+1:]) |
paul@162 | 148 | |
paul@162 | 149 | # Remove the node and the following parts from this container. |
paul@162 | 150 | |
paul@162 | 151 | del self.nodes[i:] |
paul@162 | 152 | return following |
paul@162 | 153 | |
paul@113 | 154 | def text_content(self): |
paul@113 | 155 | |
paul@113 | 156 | """ |
paul@113 | 157 | Return a string containing the content of text nodes within this |
paul@113 | 158 | container. |
paul@113 | 159 | """ |
paul@113 | 160 | |
paul@113 | 161 | l = [] |
paul@113 | 162 | |
paul@113 | 163 | for node in self.nodes: |
paul@113 | 164 | if isinstance(node, Text): |
paul@113 | 165 | l.append(node.s) |
paul@113 | 166 | elif isinstance(node, Container): |
paul@113 | 167 | l.append(node.text_content()) |
paul@113 | 168 | |
paul@113 | 169 | return "".join(l) |
paul@113 | 170 | |
paul@162 | 171 | def whitespace_only(self): |
paul@162 | 172 | |
paul@162 | 173 | "Return whether the container provides only whitespace text." |
paul@162 | 174 | |
paul@162 | 175 | return not self.text_content().strip() |
paul@162 | 176 | |
paul@11 | 177 | def __str__(self): |
paul@11 | 178 | return self.prettyprint() |
paul@11 | 179 | |
paul@16 | 180 | def _prettyprint(self, l, indent=""): |
paul@16 | 181 | for node in self.nodes: |
paul@16 | 182 | l.append(node.prettyprint(indent + " ")) |
paul@16 | 183 | return "\n".join(l) |
paul@16 | 184 | |
paul@16 | 185 | def _to_string(self, out): |
paul@16 | 186 | for node in self.nodes: |
paul@16 | 187 | node.to_string(out) |
paul@11 | 188 | |
paul@11 | 189 | class Region(Container): |
paul@11 | 190 | |
paul@11 | 191 | "A region of the page." |
paul@11 | 192 | |
paul@124 | 193 | def __init__(self, nodes, level=0, indent=0, type=None, args=None, |
paul@124 | 194 | transparent=True, extra=None): |
paul@11 | 195 | Container.__init__(self, nodes) |
paul@11 | 196 | self.level = level |
paul@11 | 197 | self.indent = indent |
paul@11 | 198 | self.type = type |
paul@124 | 199 | self.args = args |
paul@32 | 200 | self.transparent = transparent |
paul@67 | 201 | self.extra = extra |
paul@11 | 202 | |
paul@106 | 203 | def append_point(self): |
paul@106 | 204 | |
paul@106 | 205 | "Return the container to which inline nodes are added." |
paul@106 | 206 | |
paul@32 | 207 | if self.transparent: |
paul@106 | 208 | return self.nodes[-1] |
paul@11 | 209 | else: |
paul@106 | 210 | return self |
paul@11 | 211 | |
paul@11 | 212 | def have_end(self, s): |
paul@11 | 213 | return self.level and s.startswith("}") and self.level == len(s) |
paul@11 | 214 | |
paul@11 | 215 | def __repr__(self): |
paul@124 | 216 | return "Region(%r, %r, %r, %r, %r, %r, %r)" % (self.nodes, self.level, |
paul@124 | 217 | self.indent, self.type, self.args, self.transparent, self.extra) |
paul@11 | 218 | |
paul@11 | 219 | def prettyprint(self, indent=""): |
paul@124 | 220 | l = ["%sRegion: level=%d indent=%d type=%s args=%r extra=%r" % (indent, |
paul@124 | 221 | self.level, self.indent, self.type, self.args, self.extra)] |
paul@16 | 222 | return self._prettyprint(l, indent) |
paul@11 | 223 | |
paul@11 | 224 | def to_string(self, out): |
paul@313 | 225 | out.start_region(self.level, self.indent, self.type, self.args, self.extra) |
paul@41 | 226 | |
paul@100 | 227 | # Obtain a serialiser for the region from the same format family. |
paul@85 | 228 | # Retain the same serialiser if no appropriate serialiser could be |
paul@85 | 229 | # obtained. |
paul@85 | 230 | |
paul@301 | 231 | serialiser_name = "%s.%s" % (out.formats[0], self.type) |
paul@100 | 232 | serialiser = out.get_serialiser(serialiser_name) |
paul@46 | 233 | |
paul@46 | 234 | # Serialise the region. |
paul@46 | 235 | |
paul@100 | 236 | self._to_string(serialiser) |
paul@100 | 237 | |
paul@100 | 238 | # End the region with the previous serialiser. |
paul@41 | 239 | |
paul@313 | 240 | out.end_region(self.level, self.indent, self.type, self.args, self.extra) |
paul@11 | 241 | |
paul@17 | 242 | |
paul@17 | 243 | |
paul@72 | 244 | # Block nodes. |
paul@72 | 245 | |
paul@11 | 246 | class Block(Container): |
paul@11 | 247 | |
paul@11 | 248 | "A block in the page." |
paul@11 | 249 | |
paul@11 | 250 | def __repr__(self): |
paul@11 | 251 | return "Block(%r)" % self.nodes |
paul@11 | 252 | |
paul@11 | 253 | def prettyprint(self, indent=""): |
paul@15 | 254 | l = ["%sBlock" % indent] |
paul@16 | 255 | return self._prettyprint(l, indent) |
paul@11 | 256 | |
paul@11 | 257 | def to_string(self, out): |
paul@15 | 258 | out.start_block() |
paul@16 | 259 | self._to_string(out) |
paul@15 | 260 | out.end_block() |
paul@11 | 261 | |
paul@16 | 262 | class DefItem(Container): |
paul@16 | 263 | |
paul@16 | 264 | "A definition item." |
paul@16 | 265 | |
paul@16 | 266 | def __init__(self, nodes, pad, extra): |
paul@16 | 267 | Container.__init__(self, nodes) |
paul@16 | 268 | self.pad = pad |
paul@16 | 269 | self.extra = extra |
paul@16 | 270 | |
paul@16 | 271 | def __repr__(self): |
paul@16 | 272 | return "DefItem(%r, %r, %r)" % (self.nodes, self.pad, self.extra) |
paul@16 | 273 | |
paul@16 | 274 | def prettyprint(self, indent=""): |
paul@16 | 275 | l = ["%sDefItem: pad=%r extra=%r" % (indent, self.pad, self.extra)] |
paul@16 | 276 | return self._prettyprint(l, indent) |
paul@16 | 277 | |
paul@16 | 278 | def to_string(self, out): |
paul@16 | 279 | out.start_defitem(self.pad, self.extra) |
paul@16 | 280 | self._to_string(out) |
paul@16 | 281 | out.end_defitem(self.pad, self.extra) |
paul@16 | 282 | |
paul@16 | 283 | class DefTerm(Container): |
paul@16 | 284 | |
paul@16 | 285 | "A definition term." |
paul@16 | 286 | |
paul@122 | 287 | def __init__(self, nodes, pad, extra=""): |
paul@16 | 288 | Container.__init__(self, nodes) |
paul@16 | 289 | self.pad = pad |
paul@122 | 290 | self.extra = extra |
paul@16 | 291 | |
paul@16 | 292 | def __repr__(self): |
paul@122 | 293 | return "DefTerm(%r, %r, %r)" % (self.nodes, self.pad, self.extra) |
paul@16 | 294 | |
paul@16 | 295 | def prettyprint(self, indent=""): |
paul@122 | 296 | l = ["%sDefTerm: pad=%r extra=%r" % (indent, self.pad, self.extra)] |
paul@16 | 297 | return self._prettyprint(l, indent) |
paul@16 | 298 | |
paul@16 | 299 | def to_string(self, out): |
paul@122 | 300 | out.start_defterm(self.pad, self.extra) |
paul@16 | 301 | self._to_string(out) |
paul@122 | 302 | out.end_defterm(self.pad, self.extra) |
paul@16 | 303 | |
paul@18 | 304 | class FontStyle(Container): |
paul@18 | 305 | |
paul@18 | 306 | "Emphasised and/or strong text." |
paul@18 | 307 | |
paul@18 | 308 | def __init__(self, nodes, emphasis=False, strong=False): |
paul@18 | 309 | Container.__init__(self, nodes) |
paul@18 | 310 | self.emphasis = emphasis |
paul@18 | 311 | self.strong = strong |
paul@17 | 312 | |
paul@18 | 313 | def close_emphasis(self): |
paul@18 | 314 | if self.strong: |
paul@18 | 315 | span = FontStyle(self.nodes, emphasis=True) |
paul@18 | 316 | self.nodes = [span] |
paul@18 | 317 | self.emphasis = False |
paul@18 | 318 | return self.strong |
paul@18 | 319 | |
paul@18 | 320 | def close_strong(self): |
paul@18 | 321 | if self.emphasis: |
paul@18 | 322 | span = FontStyle(self.nodes, strong=True) |
paul@18 | 323 | self.nodes = [span] |
paul@18 | 324 | self.strong = False |
paul@18 | 325 | return self.emphasis |
paul@17 | 326 | |
paul@17 | 327 | def __repr__(self): |
paul@18 | 328 | return "FontStyle(%r, %r, %r)" % (self.nodes, self.emphasis, self.strong) |
paul@17 | 329 | |
paul@17 | 330 | def prettyprint(self, indent=""): |
paul@18 | 331 | l = ["%sFontStyle: emphasis=%r strong=%r" % (indent, self.emphasis, self.strong)] |
paul@17 | 332 | return self._prettyprint(l, indent) |
paul@17 | 333 | |
paul@17 | 334 | def to_string(self, out): |
paul@18 | 335 | if self.emphasis: |
paul@18 | 336 | out.start_emphasis() |
paul@18 | 337 | elif self.strong: |
paul@18 | 338 | out.start_strong() |
paul@17 | 339 | self._to_string(out) |
paul@18 | 340 | if self.emphasis: |
paul@18 | 341 | out.end_emphasis() |
paul@18 | 342 | elif self.strong: |
paul@18 | 343 | out.end_strong() |
paul@17 | 344 | |
paul@13 | 345 | class Heading(Container): |
paul@13 | 346 | |
paul@13 | 347 | "A heading." |
paul@13 | 348 | |
paul@128 | 349 | def __init__(self, nodes, level, start_extra="", start_pad="", end_pad="", end_extra="", |
paul@128 | 350 | identifier=None): |
paul@13 | 351 | Container.__init__(self, nodes) |
paul@13 | 352 | self.level = level |
paul@13 | 353 | self.start_extra = start_extra |
paul@13 | 354 | self.start_pad = start_pad |
paul@13 | 355 | self.end_pad = end_pad |
paul@13 | 356 | self.end_extra = end_extra |
paul@128 | 357 | self.identifier = identifier |
paul@13 | 358 | |
paul@13 | 359 | def __repr__(self): |
paul@128 | 360 | return "Heading(%r, %d, %r, %r, %r, %r, %r)" % ( |
paul@128 | 361 | self.nodes, self.level, self.start_extra, self.start_pad, |
paul@128 | 362 | self.end_pad, self.end_extra, self.identifier) |
paul@13 | 363 | |
paul@13 | 364 | def prettyprint(self, indent=""): |
paul@128 | 365 | l = ["%sHeading: level=%d start_extra=%r start_pad=%r end_pad=%r" |
paul@128 | 366 | " end_extra=%r identifier=%r" % ( |
paul@128 | 367 | indent, self.level, self.start_extra, self.start_pad, self.end_pad, |
paul@128 | 368 | self.end_extra, self.identifier)] |
paul@16 | 369 | return self._prettyprint(l, indent) |
paul@13 | 370 | |
paul@13 | 371 | def to_string(self, out): |
paul@128 | 372 | out.start_heading(self.level, self.start_extra, self.start_pad, self.identifier) |
paul@16 | 373 | self._to_string(out) |
paul@13 | 374 | out.end_heading(self.level, self.end_pad, self.end_extra) |
paul@13 | 375 | |
paul@167 | 376 | class LinkLabel(Container): |
paul@167 | 377 | |
paul@167 | 378 | "A link or transclusion label." |
paul@167 | 379 | |
paul@167 | 380 | def __repr__(self): |
paul@167 | 381 | return "LinkLabel(%r)" % self.nodes |
paul@167 | 382 | |
paul@167 | 383 | def prettyprint(self, indent=""): |
paul@167 | 384 | l = ["%sLinkLabel" % indent] |
paul@167 | 385 | return self._prettyprint(l, indent) |
paul@167 | 386 | |
paul@167 | 387 | def to_string(self, out): |
paul@167 | 388 | out.link_label(self.nodes) |
paul@167 | 389 | |
paul@167 | 390 | class LinkParameter(Container): |
paul@167 | 391 | |
paul@167 | 392 | "A link or transclusion parameter." |
paul@167 | 393 | |
paul@167 | 394 | def __repr__(self): |
paul@167 | 395 | return "LinkParameter(%r)" % self.nodes |
paul@167 | 396 | |
paul@167 | 397 | def prettyprint(self, indent=""): |
paul@167 | 398 | l = ["%sLinkParameter" % indent] |
paul@167 | 399 | return self._prettyprint(l, indent) |
paul@167 | 400 | |
paul@167 | 401 | def to_string(self, out): |
paul@167 | 402 | s = self.text_content() |
paul@167 | 403 | t = s.split("=", 1) |
paul@167 | 404 | out.link_parameter(t) |
paul@167 | 405 | |
paul@43 | 406 | class List(Container): |
paul@43 | 407 | |
paul@43 | 408 | "A list." |
paul@43 | 409 | |
paul@229 | 410 | def __init__(self, nodes): |
paul@43 | 411 | Container.__init__(self, nodes) |
paul@233 | 412 | self.init() |
paul@233 | 413 | |
paul@233 | 414 | def init(self): |
paul@233 | 415 | self.first = first = self.nodes and self.nodes[0] or None |
paul@229 | 416 | self.indent = first and first.indent |
paul@229 | 417 | self.marker = first and first.marker |
paul@229 | 418 | self.num = first and first.num |
paul@43 | 419 | |
paul@43 | 420 | def __repr__(self): |
paul@229 | 421 | return "List(%r)" % self.nodes |
paul@43 | 422 | |
paul@43 | 423 | def prettyprint(self, indent=""): |
paul@242 | 424 | if not self.first: |
paul@242 | 425 | self.init() |
paul@242 | 426 | l = ["%sList: indent=%r marker=%r num=%r" % (indent, self.indent, self.marker, self.num)] |
paul@43 | 427 | return self._prettyprint(l, indent) |
paul@43 | 428 | |
paul@43 | 429 | def to_string(self, out): |
paul@233 | 430 | if not self.first: |
paul@233 | 431 | self.init() |
paul@51 | 432 | out.start_list(self.indent, self.marker, self.num) |
paul@43 | 433 | self._to_string(out) |
paul@51 | 434 | out.end_list(self.indent, self.marker, self.num) |
paul@43 | 435 | |
paul@11 | 436 | class ListItem(Container): |
paul@11 | 437 | |
paul@11 | 438 | "A list item." |
paul@11 | 439 | |
paul@51 | 440 | def __init__(self, nodes, indent, marker, space, num): |
paul@14 | 441 | Container.__init__(self, nodes) |
paul@14 | 442 | self.indent = indent |
paul@14 | 443 | self.marker = marker |
paul@14 | 444 | self.space = space |
paul@51 | 445 | self.num = num |
paul@14 | 446 | |
paul@67 | 447 | # Forbid blocks within list items for simpler structure. |
paul@67 | 448 | |
paul@67 | 449 | self.allow_blocks = False |
paul@67 | 450 | |
paul@11 | 451 | def __repr__(self): |
paul@51 | 452 | return "ListItem(%r, %r, %r, %r, %r)" % (self.nodes, self.indent, self.marker, self.space, self.num) |
paul@11 | 453 | |
paul@11 | 454 | def prettyprint(self, indent=""): |
paul@51 | 455 | l = ["%sListItem: indent=%d marker=%r space=%r num=%r" % (indent, self.indent, self.marker, self.space, self.num)] |
paul@16 | 456 | return self._prettyprint(l, indent) |
paul@11 | 457 | |
paul@11 | 458 | def to_string(self, out): |
paul@51 | 459 | out.start_listitem(self.indent, self.marker, self.space, self.num) |
paul@16 | 460 | self._to_string(out) |
paul@51 | 461 | out.end_listitem(self.indent, self.marker, self.space, self.num) |
paul@11 | 462 | |
paul@25 | 463 | class TableAttrs(Container): |
paul@25 | 464 | |
paul@25 | 465 | "A collection of table attributes." |
paul@25 | 466 | |
paul@291 | 467 | def __init__(self, nodes): |
paul@291 | 468 | Container.__init__(self, nodes) |
paul@291 | 469 | |
paul@291 | 470 | # Parsing state flags, inconsequential to any final document tree. |
paul@291 | 471 | # If incomplete remains set, the attributes are discarded. |
paul@291 | 472 | |
paul@291 | 473 | self.incomplete = True |
paul@291 | 474 | self.found_cell = False |
paul@291 | 475 | |
paul@25 | 476 | def __repr__(self): |
paul@25 | 477 | return "TableAttrs(%r)" % self.nodes |
paul@25 | 478 | |
paul@25 | 479 | def prettyprint(self, indent=""): |
paul@25 | 480 | l = ["%sTableAttrs:" % indent] |
paul@25 | 481 | return self._prettyprint(l, indent) |
paul@25 | 482 | |
paul@25 | 483 | def to_string(self, out): |
paul@25 | 484 | out.start_table_attrs() |
paul@102 | 485 | out.table_attrs(self.nodes) |
paul@291 | 486 | if not self.incomplete: |
paul@291 | 487 | out.end_table_attrs() |
paul@25 | 488 | |
paul@36 | 489 | class Table(Container): |
paul@36 | 490 | |
paul@36 | 491 | "A table." |
paul@36 | 492 | |
paul@36 | 493 | def __repr__(self): |
paul@36 | 494 | return "Table(%r)" % self.nodes |
paul@36 | 495 | |
paul@36 | 496 | def prettyprint(self, indent=""): |
paul@36 | 497 | l = ["%sTable:" % indent] |
paul@36 | 498 | return self._prettyprint(l, indent) |
paul@36 | 499 | |
paul@36 | 500 | def to_string(self, out): |
paul@36 | 501 | out.start_table() |
paul@36 | 502 | self._to_string(out) |
paul@36 | 503 | out.end_table() |
paul@36 | 504 | |
paul@24 | 505 | class TableCell(Container): |
paul@24 | 506 | |
paul@24 | 507 | "A table cell." |
paul@24 | 508 | |
paul@305 | 509 | def __init__(self, nodes, attrs=None, leading="", padding=""): |
paul@25 | 510 | Container.__init__(self, nodes) |
paul@25 | 511 | self.attrs = attrs |
paul@305 | 512 | self.leading = leading |
paul@305 | 513 | self.padding = padding |
paul@25 | 514 | |
paul@24 | 515 | def __repr__(self): |
paul@305 | 516 | return "TableCell(%r, %r, %r, %r)" % (self.nodes, self.attrs, |
paul@305 | 517 | self.leading, self.padding) |
paul@24 | 518 | |
paul@24 | 519 | def prettyprint(self, indent=""): |
paul@305 | 520 | l = ["%sTableCell: leading=%r padding=%r" % (indent, self.leading, |
paul@305 | 521 | self.padding)] |
paul@24 | 522 | return self._prettyprint(l, indent) |
paul@24 | 523 | |
paul@24 | 524 | def to_string(self, out): |
paul@305 | 525 | out.start_table_cell(self.attrs, self.leading, self.padding) |
paul@102 | 526 | self._to_string(out) |
paul@24 | 527 | out.end_table_cell() |
paul@24 | 528 | |
paul@24 | 529 | class TableRow(Container): |
paul@24 | 530 | |
paul@24 | 531 | "A table row." |
paul@24 | 532 | |
paul@305 | 533 | def __init__(self, nodes, trailing="", leading="", padding=""): |
paul@24 | 534 | Container.__init__(self, nodes) |
paul@24 | 535 | self.trailing = trailing |
paul@305 | 536 | self.leading = leading |
paul@305 | 537 | self.padding = padding |
paul@24 | 538 | |
paul@24 | 539 | def __repr__(self): |
paul@305 | 540 | return "TableRow(%r, %r, %r, %r)" % (self.nodes, self.trailing, |
paul@305 | 541 | self.leading, self.padding) |
paul@24 | 542 | |
paul@24 | 543 | def prettyprint(self, indent=""): |
paul@305 | 544 | l = ["%sTableRow: trailing=%r leading=%r padding=%r" % ( |
paul@305 | 545 | indent, self.trailing, self.leading, self.padding)] |
paul@24 | 546 | return self._prettyprint(l, indent) |
paul@24 | 547 | |
paul@24 | 548 | def to_string(self, out): |
paul@305 | 549 | out.start_table_row(self.leading, self.padding) |
paul@24 | 550 | self._to_string(out) |
paul@24 | 551 | out.end_table_row(self.trailing) |
paul@24 | 552 | |
paul@23 | 553 | |
paul@17 | 554 | |
paul@77 | 555 | # Inline nodes with children. |
paul@72 | 556 | |
paul@23 | 557 | class Inline(Container): |
paul@23 | 558 | |
paul@23 | 559 | "Generic inline formatting." |
paul@17 | 560 | |
paul@17 | 561 | def __repr__(self): |
paul@23 | 562 | return "%s(%r)" % (self.__class__.__name__, self.nodes) |
paul@17 | 563 | |
paul@17 | 564 | def prettyprint(self, indent=""): |
paul@23 | 565 | l = ["%s%s" % (indent, self.__class__.__name__)] |
paul@17 | 566 | return self._prettyprint(l, indent) |
paul@17 | 567 | |
paul@23 | 568 | class Larger(Inline): |
paul@23 | 569 | |
paul@23 | 570 | "Larger text." |
paul@23 | 571 | |
paul@23 | 572 | def to_string(self, out): |
paul@23 | 573 | out.start_larger() |
paul@23 | 574 | self._to_string(out) |
paul@23 | 575 | out.end_larger() |
paul@23 | 576 | |
paul@47 | 577 | class Link(Container): |
paul@47 | 578 | |
paul@47 | 579 | "Link details." |
paul@47 | 580 | |
paul@47 | 581 | def __init__(self, nodes, target): |
paul@47 | 582 | Container.__init__(self, nodes) |
paul@47 | 583 | self.target = target |
paul@47 | 584 | |
paul@47 | 585 | def __repr__(self): |
paul@47 | 586 | return "Link(%r, %r)" % (self.nodes, self.target) |
paul@47 | 587 | |
paul@47 | 588 | def prettyprint(self, indent=""): |
paul@47 | 589 | l = ["%sLink: target=%r" % (indent, self.target)] |
paul@47 | 590 | return self._prettyprint(l, indent) |
paul@47 | 591 | |
paul@47 | 592 | def to_string(self, out): |
paul@167 | 593 | out.link(self.target, self.nodes) |
paul@47 | 594 | |
paul@87 | 595 | class Macro(Container): |
paul@87 | 596 | |
paul@87 | 597 | "Macro details." |
paul@87 | 598 | |
paul@262 | 599 | def __init__(self, name, args, parent, region, nodes=None, inline=True): |
paul@87 | 600 | Container.__init__(self, nodes or []) |
paul@87 | 601 | self.name = name |
paul@162 | 602 | self.args = args |
paul@106 | 603 | self.parent = parent |
paul@162 | 604 | self.region = region |
paul@262 | 605 | self.inline = inline |
paul@87 | 606 | |
paul@87 | 607 | def __repr__(self): |
paul@262 | 608 | return "Macro(%r, %r, %r, %r, %r, %r)" % (self.name, self.args, |
paul@262 | 609 | self.parent, self.region, |
paul@262 | 610 | self.nodes, self.inline) |
paul@87 | 611 | |
paul@87 | 612 | def prettyprint(self, indent=""): |
paul@87 | 613 | l = ["%sMacro: name=%r args=%r" % (indent, self.name, self.args)] |
paul@87 | 614 | return self._prettyprint(l, indent) |
paul@87 | 615 | |
paul@87 | 616 | def to_string(self, out): |
paul@262 | 617 | out.start_macro(self.name, self.args, self.nodes, self.inline) |
paul@87 | 618 | if self.nodes: |
paul@87 | 619 | self._to_string(out) |
paul@262 | 620 | out.end_macro(self.inline) |
paul@87 | 621 | |
paul@23 | 622 | class Monospace(Inline): |
paul@23 | 623 | |
paul@23 | 624 | "Monospaced text." |
paul@23 | 625 | |
paul@17 | 626 | def to_string(self, out): |
paul@18 | 627 | out.start_monospace() |
paul@17 | 628 | self._to_string(out) |
paul@18 | 629 | out.end_monospace() |
paul@17 | 630 | |
paul@23 | 631 | class Smaller(Inline): |
paul@23 | 632 | |
paul@23 | 633 | "Smaller text." |
paul@23 | 634 | |
paul@23 | 635 | def to_string(self, out): |
paul@23 | 636 | out.start_smaller() |
paul@23 | 637 | self._to_string(out) |
paul@23 | 638 | out.end_smaller() |
paul@23 | 639 | |
paul@48 | 640 | class Strikethrough(Inline): |
paul@48 | 641 | |
paul@48 | 642 | "Crossed-out text." |
paul@48 | 643 | |
paul@48 | 644 | def to_string(self, out): |
paul@48 | 645 | out.start_strikethrough() |
paul@48 | 646 | self._to_string(out) |
paul@48 | 647 | out.end_strikethrough() |
paul@48 | 648 | |
paul@23 | 649 | class Subscript(Inline): |
paul@20 | 650 | |
paul@20 | 651 | "Subscripted text." |
paul@20 | 652 | |
paul@20 | 653 | def to_string(self, out): |
paul@20 | 654 | out.start_subscript() |
paul@20 | 655 | self._to_string(out) |
paul@20 | 656 | out.end_subscript() |
paul@20 | 657 | |
paul@23 | 658 | class Superscript(Inline): |
paul@20 | 659 | |
paul@20 | 660 | "Superscripted text." |
paul@20 | 661 | |
paul@20 | 662 | def to_string(self, out): |
paul@20 | 663 | out.start_superscript() |
paul@20 | 664 | self._to_string(out) |
paul@20 | 665 | out.end_superscript() |
paul@20 | 666 | |
paul@167 | 667 | class Transclusion(Container): |
paul@167 | 668 | |
paul@167 | 669 | "Transclusion details." |
paul@167 | 670 | |
paul@167 | 671 | def __init__(self, nodes, target): |
paul@167 | 672 | Container.__init__(self, nodes) |
paul@167 | 673 | self.target = target |
paul@167 | 674 | |
paul@167 | 675 | def __repr__(self): |
paul@167 | 676 | return "Transclusion(%r, %r)" % (self.nodes, self.target) |
paul@167 | 677 | |
paul@167 | 678 | def prettyprint(self, indent=""): |
paul@167 | 679 | l = ["%sTransclusion: target=%r" % (indent, self.target)] |
paul@167 | 680 | return self._prettyprint(l, indent) |
paul@167 | 681 | |
paul@167 | 682 | def to_string(self, out): |
paul@167 | 683 | out.transclusion(self.target, self.nodes) |
paul@167 | 684 | |
paul@23 | 685 | class Underline(Inline): |
paul@19 | 686 | |
paul@19 | 687 | "Underlined text." |
paul@19 | 688 | |
paul@19 | 689 | def to_string(self, out): |
paul@19 | 690 | out.start_underline() |
paul@19 | 691 | self._to_string(out) |
paul@19 | 692 | out.end_underline() |
paul@19 | 693 | |
paul@12 | 694 | |
paul@12 | 695 | |
paul@72 | 696 | # Nodes without children. |
paul@72 | 697 | |
paul@12 | 698 | class Node: |
paul@12 | 699 | |
paul@12 | 700 | "A document node without children." |
paul@12 | 701 | |
paul@12 | 702 | def empty(self): |
paul@12 | 703 | return False |
paul@12 | 704 | |
paul@116 | 705 | class Anchor(Node): |
paul@116 | 706 | |
paul@116 | 707 | "Anchor details." |
paul@116 | 708 | |
paul@116 | 709 | def __init__(self, target): |
paul@116 | 710 | self.target = target |
paul@116 | 711 | |
paul@116 | 712 | def __repr__(self): |
paul@116 | 713 | return "Anchor(%r)" % self.target |
paul@116 | 714 | |
paul@116 | 715 | def prettyprint(self, indent=""): |
paul@116 | 716 | return "%sAnchor: target=%r" % (indent, self.target) |
paul@116 | 717 | |
paul@116 | 718 | def to_string(self, out): |
paul@116 | 719 | out.anchor(self.target) |
paul@116 | 720 | |
paul@15 | 721 | class Break(Node): |
paul@15 | 722 | |
paul@15 | 723 | "A paragraph break." |
paul@15 | 724 | |
paul@15 | 725 | def __repr__(self): |
paul@15 | 726 | return "Break()" |
paul@15 | 727 | |
paul@15 | 728 | def prettyprint(self, indent=""): |
paul@15 | 729 | return "%sBreak" % indent |
paul@15 | 730 | |
paul@15 | 731 | def to_string(self, out): |
paul@15 | 732 | out.break_() |
paul@15 | 733 | |
paul@151 | 734 | class Comment(Node): |
paul@151 | 735 | |
paul@151 | 736 | "A comment." |
paul@151 | 737 | |
paul@151 | 738 | def __init__(self, comment, extra): |
paul@151 | 739 | self.comment = comment |
paul@151 | 740 | self.extra = extra |
paul@151 | 741 | |
paul@151 | 742 | def __repr__(self): |
paul@151 | 743 | return "Comment(%r, %r)" % (self.comment, self.extra) |
paul@151 | 744 | |
paul@151 | 745 | def prettyprint(self, indent=""): |
paul@151 | 746 | return "%sComment: comment=%r extra=%r" % (indent, self.comment, self.extra) |
paul@151 | 747 | |
paul@151 | 748 | def to_string(self, out): |
paul@151 | 749 | out.comment(self.comment, self.extra) |
paul@151 | 750 | |
paul@151 | 751 | class Directive(Node): |
paul@151 | 752 | |
paul@151 | 753 | "A processing directive." |
paul@151 | 754 | |
paul@151 | 755 | def __init__(self, directive, extra): |
paul@151 | 756 | self.directive = directive |
paul@151 | 757 | self.extra = extra |
paul@151 | 758 | |
paul@151 | 759 | def __repr__(self): |
paul@151 | 760 | return "Directive(%r, %r)" % (self.directive, self.extra) |
paul@151 | 761 | |
paul@151 | 762 | def prettyprint(self, indent=""): |
paul@151 | 763 | return "%sDirective: directive=%r extra=%r" % (indent, self.directive, self.extra) |
paul@151 | 764 | |
paul@151 | 765 | def to_string(self, out): |
paul@151 | 766 | out.directive(self.directive, self.extra) |
paul@151 | 767 | |
paul@106 | 768 | class LineBreak(Node): |
paul@106 | 769 | |
paul@106 | 770 | "A line break within a block." |
paul@106 | 771 | |
paul@106 | 772 | def __repr__(self): |
paul@106 | 773 | return "LineBreak()" |
paul@106 | 774 | |
paul@106 | 775 | def prettyprint(self, indent=""): |
paul@106 | 776 | return "%sLineBreak" % indent |
paul@106 | 777 | |
paul@106 | 778 | def to_string(self, out): |
paul@106 | 779 | out.linebreak() |
paul@106 | 780 | |
paul@316 | 781 | class NonBreakingSpace(Node): |
paul@316 | 782 | |
paul@316 | 783 | "A non-breaking space within a block." |
paul@316 | 784 | |
paul@316 | 785 | def __repr__(self): |
paul@316 | 786 | return "NonBreakingSpace()" |
paul@316 | 787 | |
paul@316 | 788 | def prettyprint(self, indent=""): |
paul@316 | 789 | return "%sNonBreakingSpace" % indent |
paul@316 | 790 | |
paul@316 | 791 | def to_string(self, out): |
paul@316 | 792 | out.nbsp() |
paul@316 | 793 | |
paul@12 | 794 | class Rule(Node): |
paul@12 | 795 | |
paul@12 | 796 | "A horizontal rule." |
paul@12 | 797 | |
paul@267 | 798 | def __init__(self, height): |
paul@267 | 799 | self.height = height |
paul@12 | 800 | |
paul@12 | 801 | def __repr__(self): |
paul@267 | 802 | return "Rule(%d)" % self.height |
paul@12 | 803 | |
paul@12 | 804 | def prettyprint(self, indent=""): |
paul@267 | 805 | return "%sRule: height=%d" % (indent, self.height) |
paul@12 | 806 | |
paul@12 | 807 | def to_string(self, out): |
paul@267 | 808 | out.rule(self.height) |
paul@12 | 809 | |
paul@25 | 810 | class TableAttr(Node): |
paul@25 | 811 | |
paul@25 | 812 | "A table attribute." |
paul@25 | 813 | |
paul@27 | 814 | def __init__(self, name, value=None, concise=False, quote=None): |
paul@25 | 815 | self.name = name |
paul@25 | 816 | self.value = value |
paul@25 | 817 | self.concise = concise |
paul@27 | 818 | self.quote = quote |
paul@25 | 819 | |
paul@25 | 820 | def __repr__(self): |
paul@27 | 821 | return "TableAttr(%r, %r, %r, %r)" % (self.name, self.value, self.concise, self.quote) |
paul@25 | 822 | |
paul@25 | 823 | def prettyprint(self, indent=""): |
paul@27 | 824 | return "%sTableAttr: name=%r value=%r concise=%r quote=%r" % (indent, self.name, self.value, self.concise, self.quote) |
paul@25 | 825 | |
paul@25 | 826 | def to_string(self, out): |
paul@27 | 827 | out.table_attr(self.name, self.value, self.concise, self.quote) |
paul@25 | 828 | |
paul@12 | 829 | class Text(Node): |
paul@11 | 830 | |
paul@11 | 831 | "A text node." |
paul@11 | 832 | |
paul@11 | 833 | def __init__(self, s): |
paul@11 | 834 | self.s = s |
paul@11 | 835 | |
paul@11 | 836 | def empty(self): |
paul@11 | 837 | return not self.s |
paul@11 | 838 | |
paul@52 | 839 | def multiline(self): |
paul@52 | 840 | return "\n" in self.s |
paul@52 | 841 | |
paul@11 | 842 | def merge(self, text): |
paul@11 | 843 | self.s += text.s |
paul@11 | 844 | |
paul@11 | 845 | def __repr__(self): |
paul@11 | 846 | return "Text(%r)" % self.s |
paul@11 | 847 | |
paul@11 | 848 | def prettyprint(self, indent=""): |
paul@11 | 849 | return "%sText: %r" % (indent, self.s) |
paul@11 | 850 | |
paul@11 | 851 | def to_string(self, out): |
paul@11 | 852 | out.text(self.s) |
paul@11 | 853 | |
paul@169 | 854 | class Verbatim(Node): |
paul@169 | 855 | |
paul@169 | 856 | "Verbatim text." |
paul@169 | 857 | |
paul@169 | 858 | def __init__(self, text): |
paul@169 | 859 | self.text = text |
paul@169 | 860 | |
paul@169 | 861 | def __repr__(self): |
paul@169 | 862 | return "Verbatim(%r)" % self.text |
paul@169 | 863 | |
paul@169 | 864 | def prettyprint(self, indent=""): |
paul@169 | 865 | return "%sVerbatim: text=%r" % (indent, self.text) |
paul@169 | 866 | |
paul@169 | 867 | def to_string(self, out): |
paul@169 | 868 | out.verbatim(self.text) |
paul@169 | 869 | |
paul@11 | 870 | # vim: tabstop=4 expandtab shiftwidth=4 |