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@331 | 6 | Copyright (C) 2017, 2018, 2019, 2020, 2021, 2022, |
paul@331 | 7 | 2023 Paul Boddie <paul@boddie.org.uk> |
paul@11 | 8 | |
paul@11 | 9 | This program is free software; you can redistribute it and/or modify it under |
paul@11 | 10 | the terms of the GNU General Public License as published by the Free Software |
paul@11 | 11 | Foundation; either version 3 of the License, or (at your option) any later |
paul@11 | 12 | version. |
paul@11 | 13 | |
paul@11 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@11 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@11 | 16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@11 | 17 | details. |
paul@11 | 18 | |
paul@11 | 19 | You should have received a copy of the GNU General Public License along with |
paul@11 | 20 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@11 | 21 | """ |
paul@11 | 22 | |
paul@11 | 23 | class Container: |
paul@11 | 24 | |
paul@11 | 25 | "A container of document nodes." |
paul@11 | 26 | |
paul@11 | 27 | def __init__(self, nodes): |
paul@11 | 28 | self.nodes = nodes |
paul@11 | 29 | |
paul@67 | 30 | # In principle, allow blocks within containers. Some nodes may forbid |
paul@67 | 31 | # them to simplify the document structure. |
paul@67 | 32 | |
paul@67 | 33 | self.allow_blocks = True |
paul@67 | 34 | |
paul@11 | 35 | def append(self, node): |
paul@11 | 36 | self.nodes.append(node) |
paul@11 | 37 | |
paul@24 | 38 | def append_many(self, nodes): |
paul@24 | 39 | for node in nodes: |
paul@24 | 40 | self.append(node) |
paul@24 | 41 | |
paul@307 | 42 | def add(self, node): |
paul@307 | 43 | last = self.node(-1) |
paul@307 | 44 | if last and last.empty(): |
paul@307 | 45 | self.nodes[-1] = node |
paul@307 | 46 | else: |
paul@307 | 47 | self.append(node) |
paul@15 | 48 | |
paul@106 | 49 | def append_inline(self, node): |
paul@106 | 50 | |
paul@106 | 51 | "Append 'node' inline within the appropriate container." |
paul@106 | 52 | |
paul@106 | 53 | n = self.append_point() |
paul@106 | 54 | |
paul@106 | 55 | # Redirect the addition if another container is to accept the node. |
paul@106 | 56 | |
paul@106 | 57 | if n is not self: |
paul@106 | 58 | n.append_inline(node) |
paul@106 | 59 | |
paul@106 | 60 | # Otherwise, append within this container. |
paul@106 | 61 | |
paul@106 | 62 | else: |
paul@106 | 63 | n.append(node) |
paul@11 | 64 | |
paul@24 | 65 | def append_inline_many(self, nodes): |
paul@24 | 66 | for node in nodes: |
paul@24 | 67 | self.append_inline(node) |
paul@24 | 68 | |
paul@106 | 69 | def append_point(self): |
paul@106 | 70 | |
paul@106 | 71 | "Return the container to which inline nodes are added." |
paul@106 | 72 | |
paul@271 | 73 | last = self.node(-1) |
paul@271 | 74 | |
paul@271 | 75 | if isinstance(last, Block): |
paul@271 | 76 | return last |
paul@271 | 77 | else: |
paul@271 | 78 | return self |
paul@106 | 79 | |
paul@11 | 80 | def empty(self): |
paul@11 | 81 | return not self.nodes |
paul@11 | 82 | |
paul@162 | 83 | def insert_after(self, old, new): |
paul@162 | 84 | |
paul@162 | 85 | "Insert after 'old' in the children the 'new' node." |
paul@162 | 86 | |
paul@162 | 87 | index = self.nodes.index(old) |
paul@162 | 88 | self.nodes.insert(index + 1, new) |
paul@162 | 89 | |
paul@15 | 90 | def node(self, index): |
paul@15 | 91 | try: |
paul@15 | 92 | return self.nodes[index] |
paul@15 | 93 | except IndexError: |
paul@15 | 94 | return None |
paul@14 | 95 | |
paul@11 | 96 | def normalise(self): |
paul@11 | 97 | |
paul@11 | 98 | "Combine adjacent text nodes." |
paul@11 | 99 | |
paul@11 | 100 | nodes = self.nodes |
paul@11 | 101 | self.nodes = [] |
paul@11 | 102 | text = None |
paul@11 | 103 | |
paul@11 | 104 | for node in nodes: |
paul@11 | 105 | |
paul@11 | 106 | # Open a text node or merge text into an open node. |
paul@11 | 107 | |
paul@11 | 108 | if isinstance(node, Text): |
paul@11 | 109 | if not text: |
paul@11 | 110 | text = node |
paul@11 | 111 | else: |
paul@11 | 112 | text.merge(node) |
paul@11 | 113 | |
paul@11 | 114 | # Close any open text node and append the current node. |
paul@11 | 115 | |
paul@11 | 116 | else: |
paul@11 | 117 | if text: |
paul@11 | 118 | self.append(text) |
paul@11 | 119 | text = None |
paul@11 | 120 | self.append(node) |
paul@11 | 121 | |
paul@11 | 122 | # Add any open text node. |
paul@11 | 123 | |
paul@11 | 124 | if text: |
paul@11 | 125 | self.append(text) |
paul@11 | 126 | |
paul@162 | 127 | def remove(self, node): |
paul@162 | 128 | |
paul@162 | 129 | "Remove 'node' from the children." |
paul@162 | 130 | |
paul@162 | 131 | self.nodes.remove(node) |
paul@162 | 132 | |
paul@110 | 133 | def replace(self, old, new): |
paul@110 | 134 | |
paul@110 | 135 | "Replace 'old' with 'new' in the children." |
paul@110 | 136 | |
paul@110 | 137 | i = self.nodes.index(old) |
paul@110 | 138 | self.nodes[i] = new |
paul@110 | 139 | |
paul@162 | 140 | def split_at(self, node): |
paul@162 | 141 | |
paul@162 | 142 | """ |
paul@162 | 143 | Split the container at 'node', returning a new container holding the |
paul@162 | 144 | nodes following 'node' that are moved from this container. |
paul@162 | 145 | """ |
paul@162 | 146 | |
paul@162 | 147 | i = self.nodes.index(node) |
paul@162 | 148 | following = self.__class__(self.nodes[i+1:]) |
paul@162 | 149 | |
paul@162 | 150 | # Remove the node and the following parts from this container. |
paul@162 | 151 | |
paul@162 | 152 | del self.nodes[i:] |
paul@162 | 153 | return following |
paul@162 | 154 | |
paul@113 | 155 | def text_content(self): |
paul@113 | 156 | |
paul@113 | 157 | """ |
paul@113 | 158 | Return a string containing the content of text nodes within this |
paul@113 | 159 | container. |
paul@113 | 160 | """ |
paul@113 | 161 | |
paul@113 | 162 | l = [] |
paul@113 | 163 | |
paul@113 | 164 | for node in self.nodes: |
paul@113 | 165 | if isinstance(node, Text): |
paul@113 | 166 | l.append(node.s) |
paul@113 | 167 | elif isinstance(node, Container): |
paul@113 | 168 | l.append(node.text_content()) |
paul@113 | 169 | |
paul@113 | 170 | return "".join(l) |
paul@113 | 171 | |
paul@162 | 172 | def whitespace_only(self): |
paul@162 | 173 | |
paul@162 | 174 | "Return whether the container provides only whitespace text." |
paul@162 | 175 | |
paul@162 | 176 | return not self.text_content().strip() |
paul@162 | 177 | |
paul@11 | 178 | class Region(Container): |
paul@11 | 179 | |
paul@11 | 180 | "A region of the page." |
paul@11 | 181 | |
paul@124 | 182 | def __init__(self, nodes, level=0, indent=0, type=None, args=None, |
paul@124 | 183 | transparent=True, extra=None): |
paul@11 | 184 | Container.__init__(self, nodes) |
paul@11 | 185 | self.level = level |
paul@11 | 186 | self.indent = indent |
paul@11 | 187 | self.type = type |
paul@124 | 188 | self.args = args |
paul@32 | 189 | self.transparent = transparent |
paul@67 | 190 | self.extra = extra |
paul@11 | 191 | |
paul@106 | 192 | def append_point(self): |
paul@106 | 193 | |
paul@106 | 194 | "Return the container to which inline nodes are added." |
paul@106 | 195 | |
paul@32 | 196 | if self.transparent: |
paul@106 | 197 | return self.nodes[-1] |
paul@11 | 198 | else: |
paul@106 | 199 | return self |
paul@11 | 200 | |
paul@11 | 201 | def have_end(self, s): |
paul@11 | 202 | return self.level and s.startswith("}") and self.level == len(s) |
paul@11 | 203 | |
paul@11 | 204 | def __repr__(self): |
paul@124 | 205 | return "Region(%r, %r, %r, %r, %r, %r, %r)" % (self.nodes, self.level, |
paul@124 | 206 | self.indent, self.type, self.args, self.transparent, self.extra) |
paul@11 | 207 | |
paul@331 | 208 | def visit(self, visitor): |
paul@346 | 209 | return visitor.region(self) |
paul@11 | 210 | |
paul@17 | 211 | |
paul@17 | 212 | |
paul@72 | 213 | # Block nodes. |
paul@72 | 214 | |
paul@11 | 215 | class Block(Container): |
paul@11 | 216 | |
paul@11 | 217 | "A block in the page." |
paul@11 | 218 | |
paul@11 | 219 | def __repr__(self): |
paul@11 | 220 | return "Block(%r)" % self.nodes |
paul@11 | 221 | |
paul@331 | 222 | def visit(self, visitor): |
paul@346 | 223 | return visitor.block(self) |
paul@11 | 224 | |
paul@16 | 225 | class DefItem(Container): |
paul@16 | 226 | |
paul@16 | 227 | "A definition item." |
paul@16 | 228 | |
paul@16 | 229 | def __init__(self, nodes, pad, extra): |
paul@16 | 230 | Container.__init__(self, nodes) |
paul@16 | 231 | self.pad = pad |
paul@16 | 232 | self.extra = extra |
paul@16 | 233 | |
paul@16 | 234 | def __repr__(self): |
paul@16 | 235 | return "DefItem(%r, %r, %r)" % (self.nodes, self.pad, self.extra) |
paul@16 | 236 | |
paul@331 | 237 | def visit(self, visitor): |
paul@346 | 238 | return visitor.defitem(self) |
paul@16 | 239 | |
paul@16 | 240 | class DefTerm(Container): |
paul@16 | 241 | |
paul@16 | 242 | "A definition term." |
paul@16 | 243 | |
paul@122 | 244 | def __init__(self, nodes, pad, extra=""): |
paul@16 | 245 | Container.__init__(self, nodes) |
paul@16 | 246 | self.pad = pad |
paul@122 | 247 | self.extra = extra |
paul@16 | 248 | |
paul@16 | 249 | def __repr__(self): |
paul@122 | 250 | return "DefTerm(%r, %r, %r)" % (self.nodes, self.pad, self.extra) |
paul@16 | 251 | |
paul@331 | 252 | def visit(self, visitor): |
paul@346 | 253 | return visitor.defterm(self) |
paul@16 | 254 | |
paul@18 | 255 | class FontStyle(Container): |
paul@18 | 256 | |
paul@18 | 257 | "Emphasised and/or strong text." |
paul@18 | 258 | |
paul@18 | 259 | def __init__(self, nodes, emphasis=False, strong=False): |
paul@18 | 260 | Container.__init__(self, nodes) |
paul@18 | 261 | self.emphasis = emphasis |
paul@18 | 262 | self.strong = strong |
paul@17 | 263 | |
paul@18 | 264 | def close_emphasis(self): |
paul@18 | 265 | if self.strong: |
paul@18 | 266 | span = FontStyle(self.nodes, emphasis=True) |
paul@18 | 267 | self.nodes = [span] |
paul@18 | 268 | self.emphasis = False |
paul@18 | 269 | return self.strong |
paul@18 | 270 | |
paul@18 | 271 | def close_strong(self): |
paul@18 | 272 | if self.emphasis: |
paul@18 | 273 | span = FontStyle(self.nodes, strong=True) |
paul@18 | 274 | self.nodes = [span] |
paul@18 | 275 | self.strong = False |
paul@18 | 276 | return self.emphasis |
paul@17 | 277 | |
paul@17 | 278 | def __repr__(self): |
paul@18 | 279 | return "FontStyle(%r, %r, %r)" % (self.nodes, self.emphasis, self.strong) |
paul@17 | 280 | |
paul@331 | 281 | def visit(self, visitor): |
paul@346 | 282 | return visitor.fontstyle(self) |
paul@17 | 283 | |
paul@13 | 284 | class Heading(Container): |
paul@13 | 285 | |
paul@13 | 286 | "A heading." |
paul@13 | 287 | |
paul@128 | 288 | def __init__(self, nodes, level, start_extra="", start_pad="", end_pad="", end_extra="", |
paul@128 | 289 | identifier=None): |
paul@13 | 290 | Container.__init__(self, nodes) |
paul@13 | 291 | self.level = level |
paul@13 | 292 | self.start_extra = start_extra |
paul@13 | 293 | self.start_pad = start_pad |
paul@13 | 294 | self.end_pad = end_pad |
paul@13 | 295 | self.end_extra = end_extra |
paul@128 | 296 | self.identifier = identifier |
paul@13 | 297 | |
paul@13 | 298 | def __repr__(self): |
paul@128 | 299 | return "Heading(%r, %d, %r, %r, %r, %r, %r)" % ( |
paul@128 | 300 | self.nodes, self.level, self.start_extra, self.start_pad, |
paul@128 | 301 | self.end_pad, self.end_extra, self.identifier) |
paul@13 | 302 | |
paul@331 | 303 | def visit(self, visitor): |
paul@346 | 304 | return visitor.heading(self) |
paul@13 | 305 | |
paul@167 | 306 | class LinkLabel(Container): |
paul@167 | 307 | |
paul@167 | 308 | "A link or transclusion label." |
paul@167 | 309 | |
paul@167 | 310 | def __repr__(self): |
paul@167 | 311 | return "LinkLabel(%r)" % self.nodes |
paul@167 | 312 | |
paul@331 | 313 | def visit(self, visitor): |
paul@346 | 314 | return visitor.link_label(self) |
paul@167 | 315 | |
paul@167 | 316 | class LinkParameter(Container): |
paul@167 | 317 | |
paul@167 | 318 | "A link or transclusion parameter." |
paul@167 | 319 | |
paul@167 | 320 | def __repr__(self): |
paul@167 | 321 | return "LinkParameter(%r)" % self.nodes |
paul@167 | 322 | |
paul@331 | 323 | def visit(self, visitor): |
paul@346 | 324 | return visitor.link_parameter(self) |
paul@167 | 325 | |
paul@43 | 326 | class List(Container): |
paul@43 | 327 | |
paul@43 | 328 | "A list." |
paul@43 | 329 | |
paul@229 | 330 | def __init__(self, nodes): |
paul@43 | 331 | Container.__init__(self, nodes) |
paul@233 | 332 | self.init() |
paul@233 | 333 | |
paul@233 | 334 | def init(self): |
paul@233 | 335 | self.first = first = self.nodes and self.nodes[0] or None |
paul@229 | 336 | self.indent = first and first.indent |
paul@229 | 337 | self.marker = first and first.marker |
paul@229 | 338 | self.num = first and first.num |
paul@43 | 339 | |
paul@43 | 340 | def __repr__(self): |
paul@229 | 341 | return "List(%r)" % self.nodes |
paul@43 | 342 | |
paul@331 | 343 | def visit(self, visitor): |
paul@233 | 344 | if not self.first: |
paul@233 | 345 | self.init() |
paul@346 | 346 | return visitor.list(self) |
paul@43 | 347 | |
paul@11 | 348 | class ListItem(Container): |
paul@11 | 349 | |
paul@11 | 350 | "A list item." |
paul@11 | 351 | |
paul@51 | 352 | def __init__(self, nodes, indent, marker, space, num): |
paul@14 | 353 | Container.__init__(self, nodes) |
paul@14 | 354 | self.indent = indent |
paul@14 | 355 | self.marker = marker |
paul@14 | 356 | self.space = space |
paul@51 | 357 | self.num = num |
paul@14 | 358 | |
paul@67 | 359 | # Forbid blocks within list items for simpler structure. |
paul@67 | 360 | |
paul@67 | 361 | self.allow_blocks = False |
paul@67 | 362 | |
paul@11 | 363 | def __repr__(self): |
paul@51 | 364 | return "ListItem(%r, %r, %r, %r, %r)" % (self.nodes, self.indent, self.marker, self.space, self.num) |
paul@11 | 365 | |
paul@333 | 366 | def visit(self, visitor): |
paul@346 | 367 | return visitor.listitem(self) |
paul@333 | 368 | |
paul@333 | 369 | class Table(Container): |
paul@333 | 370 | |
paul@333 | 371 | "A table." |
paul@333 | 372 | |
paul@333 | 373 | def __repr__(self): |
paul@333 | 374 | return "Table(%r)" % self.nodes |
paul@11 | 375 | |
paul@331 | 376 | def visit(self, visitor): |
paul@346 | 377 | return visitor.table(self) |
paul@11 | 378 | |
paul@25 | 379 | class TableAttrs(Container): |
paul@25 | 380 | |
paul@25 | 381 | "A collection of table attributes." |
paul@25 | 382 | |
paul@291 | 383 | def __init__(self, nodes): |
paul@291 | 384 | Container.__init__(self, nodes) |
paul@291 | 385 | |
paul@291 | 386 | # Parsing state flags, inconsequential to any final document tree. |
paul@291 | 387 | # If incomplete remains set, the attributes are discarded. |
paul@291 | 388 | |
paul@291 | 389 | self.incomplete = True |
paul@291 | 390 | self.found_cell = False |
paul@291 | 391 | |
paul@25 | 392 | def __repr__(self): |
paul@25 | 393 | return "TableAttrs(%r)" % self.nodes |
paul@25 | 394 | |
paul@331 | 395 | def visit(self, visitor): |
paul@346 | 396 | return visitor.table_attrs(self) |
paul@25 | 397 | |
paul@24 | 398 | class TableCell(Container): |
paul@24 | 399 | |
paul@24 | 400 | "A table cell." |
paul@24 | 401 | |
paul@305 | 402 | def __init__(self, nodes, attrs=None, leading="", padding=""): |
paul@25 | 403 | Container.__init__(self, nodes) |
paul@25 | 404 | self.attrs = attrs |
paul@305 | 405 | self.leading = leading |
paul@305 | 406 | self.padding = padding |
paul@25 | 407 | |
paul@24 | 408 | def __repr__(self): |
paul@305 | 409 | return "TableCell(%r, %r, %r, %r)" % (self.nodes, self.attrs, |
paul@305 | 410 | self.leading, self.padding) |
paul@24 | 411 | |
paul@331 | 412 | def visit(self, visitor): |
paul@346 | 413 | return visitor.table_cell(self) |
paul@24 | 414 | |
paul@24 | 415 | class TableRow(Container): |
paul@24 | 416 | |
paul@24 | 417 | "A table row." |
paul@24 | 418 | |
paul@305 | 419 | def __init__(self, nodes, trailing="", leading="", padding=""): |
paul@24 | 420 | Container.__init__(self, nodes) |
paul@24 | 421 | self.trailing = trailing |
paul@305 | 422 | self.leading = leading |
paul@305 | 423 | self.padding = padding |
paul@24 | 424 | |
paul@24 | 425 | def __repr__(self): |
paul@305 | 426 | return "TableRow(%r, %r, %r, %r)" % (self.nodes, self.trailing, |
paul@305 | 427 | self.leading, self.padding) |
paul@24 | 428 | |
paul@331 | 429 | def visit(self, visitor): |
paul@346 | 430 | return visitor.table_row(self) |
paul@24 | 431 | |
paul@23 | 432 | |
paul@17 | 433 | |
paul@77 | 434 | # Inline nodes with children. |
paul@72 | 435 | |
paul@23 | 436 | class Inline(Container): |
paul@23 | 437 | |
paul@23 | 438 | "Generic inline formatting." |
paul@17 | 439 | |
paul@17 | 440 | def __repr__(self): |
paul@23 | 441 | return "%s(%r)" % (self.__class__.__name__, self.nodes) |
paul@17 | 442 | |
paul@23 | 443 | class Larger(Inline): |
paul@23 | 444 | |
paul@23 | 445 | "Larger text." |
paul@23 | 446 | |
paul@331 | 447 | def visit(self, visitor): |
paul@346 | 448 | return visitor.larger(self) |
paul@23 | 449 | |
paul@47 | 450 | class Link(Container): |
paul@47 | 451 | |
paul@47 | 452 | "Link details." |
paul@47 | 453 | |
paul@47 | 454 | def __init__(self, nodes, target): |
paul@47 | 455 | Container.__init__(self, nodes) |
paul@47 | 456 | self.target = target |
paul@47 | 457 | |
paul@47 | 458 | def __repr__(self): |
paul@47 | 459 | return "Link(%r, %r)" % (self.nodes, self.target) |
paul@47 | 460 | |
paul@331 | 461 | def visit(self, visitor): |
paul@346 | 462 | return visitor.link(self) |
paul@47 | 463 | |
paul@87 | 464 | class Macro(Container): |
paul@87 | 465 | |
paul@87 | 466 | "Macro details." |
paul@87 | 467 | |
paul@262 | 468 | def __init__(self, name, args, parent, region, nodes=None, inline=True): |
paul@87 | 469 | Container.__init__(self, nodes or []) |
paul@87 | 470 | self.name = name |
paul@162 | 471 | self.args = args |
paul@106 | 472 | self.parent = parent |
paul@162 | 473 | self.region = region |
paul@262 | 474 | self.inline = inline |
paul@87 | 475 | |
paul@87 | 476 | def __repr__(self): |
paul@262 | 477 | return "Macro(%r, %r, %r, %r, %r, %r)" % (self.name, self.args, |
paul@262 | 478 | self.parent, self.region, |
paul@262 | 479 | self.nodes, self.inline) |
paul@87 | 480 | |
paul@331 | 481 | def visit(self, visitor): |
paul@346 | 482 | return visitor.macro(self) |
paul@87 | 483 | |
paul@23 | 484 | class Monospace(Inline): |
paul@23 | 485 | |
paul@23 | 486 | "Monospaced text." |
paul@23 | 487 | |
paul@331 | 488 | def visit(self, visitor): |
paul@346 | 489 | return visitor.monospace(self) |
paul@17 | 490 | |
paul@23 | 491 | class Smaller(Inline): |
paul@23 | 492 | |
paul@23 | 493 | "Smaller text." |
paul@23 | 494 | |
paul@331 | 495 | def visit(self, visitor): |
paul@346 | 496 | return visitor.smaller(self) |
paul@23 | 497 | |
paul@48 | 498 | class Strikethrough(Inline): |
paul@48 | 499 | |
paul@331 | 500 | "Crossed-visitor text." |
paul@48 | 501 | |
paul@331 | 502 | def visit(self, visitor): |
paul@346 | 503 | return visitor.strikethrough(self) |
paul@48 | 504 | |
paul@23 | 505 | class Subscript(Inline): |
paul@20 | 506 | |
paul@20 | 507 | "Subscripted text." |
paul@20 | 508 | |
paul@331 | 509 | def visit(self, visitor): |
paul@346 | 510 | return visitor.subscript(self) |
paul@20 | 511 | |
paul@23 | 512 | class Superscript(Inline): |
paul@20 | 513 | |
paul@20 | 514 | "Superscripted text." |
paul@20 | 515 | |
paul@331 | 516 | def visit(self, visitor): |
paul@346 | 517 | return visitor.superscript(self) |
paul@20 | 518 | |
paul@167 | 519 | class Transclusion(Container): |
paul@167 | 520 | |
paul@167 | 521 | "Transclusion details." |
paul@167 | 522 | |
paul@167 | 523 | def __init__(self, nodes, target): |
paul@167 | 524 | Container.__init__(self, nodes) |
paul@167 | 525 | self.target = target |
paul@167 | 526 | |
paul@167 | 527 | def __repr__(self): |
paul@167 | 528 | return "Transclusion(%r, %r)" % (self.nodes, self.target) |
paul@167 | 529 | |
paul@331 | 530 | def visit(self, visitor): |
paul@346 | 531 | return visitor.transclusion(self) |
paul@167 | 532 | |
paul@23 | 533 | class Underline(Inline): |
paul@19 | 534 | |
paul@19 | 535 | "Underlined text." |
paul@19 | 536 | |
paul@331 | 537 | def visit(self, visitor): |
paul@346 | 538 | return visitor.underline(self) |
paul@19 | 539 | |
paul@12 | 540 | |
paul@12 | 541 | |
paul@72 | 542 | # Nodes without children. |
paul@72 | 543 | |
paul@12 | 544 | class Node: |
paul@12 | 545 | |
paul@12 | 546 | "A document node without children." |
paul@12 | 547 | |
paul@12 | 548 | def empty(self): |
paul@12 | 549 | return False |
paul@12 | 550 | |
paul@116 | 551 | class Anchor(Node): |
paul@116 | 552 | |
paul@116 | 553 | "Anchor details." |
paul@116 | 554 | |
paul@116 | 555 | def __init__(self, target): |
paul@116 | 556 | self.target = target |
paul@116 | 557 | |
paul@116 | 558 | def __repr__(self): |
paul@116 | 559 | return "Anchor(%r)" % self.target |
paul@116 | 560 | |
paul@331 | 561 | def visit(self, visitor): |
paul@346 | 562 | return visitor.anchor(self) |
paul@116 | 563 | |
paul@15 | 564 | class Break(Node): |
paul@15 | 565 | |
paul@15 | 566 | "A paragraph break." |
paul@15 | 567 | |
paul@15 | 568 | def __repr__(self): |
paul@15 | 569 | return "Break()" |
paul@15 | 570 | |
paul@331 | 571 | def visit(self, visitor): |
paul@346 | 572 | return visitor.break_(self) |
paul@15 | 573 | |
paul@151 | 574 | class Comment(Node): |
paul@151 | 575 | |
paul@151 | 576 | "A comment." |
paul@151 | 577 | |
paul@151 | 578 | def __init__(self, comment, extra): |
paul@151 | 579 | self.comment = comment |
paul@151 | 580 | self.extra = extra |
paul@151 | 581 | |
paul@151 | 582 | def __repr__(self): |
paul@151 | 583 | return "Comment(%r, %r)" % (self.comment, self.extra) |
paul@151 | 584 | |
paul@331 | 585 | def visit(self, visitor): |
paul@346 | 586 | return visitor.comment(self) |
paul@151 | 587 | |
paul@151 | 588 | class Directive(Node): |
paul@151 | 589 | |
paul@151 | 590 | "A processing directive." |
paul@151 | 591 | |
paul@151 | 592 | def __init__(self, directive, extra): |
paul@151 | 593 | self.directive = directive |
paul@151 | 594 | self.extra = extra |
paul@151 | 595 | |
paul@151 | 596 | def __repr__(self): |
paul@151 | 597 | return "Directive(%r, %r)" % (self.directive, self.extra) |
paul@151 | 598 | |
paul@331 | 599 | def visit(self, visitor): |
paul@346 | 600 | return visitor.directive(self) |
paul@151 | 601 | |
paul@106 | 602 | class LineBreak(Node): |
paul@106 | 603 | |
paul@106 | 604 | "A line break within a block." |
paul@106 | 605 | |
paul@106 | 606 | def __repr__(self): |
paul@106 | 607 | return "LineBreak()" |
paul@106 | 608 | |
paul@331 | 609 | def visit(self, visitor): |
paul@346 | 610 | return visitor.linebreak(self) |
paul@106 | 611 | |
paul@316 | 612 | class NonBreakingSpace(Node): |
paul@316 | 613 | |
paul@316 | 614 | "A non-breaking space within a block." |
paul@316 | 615 | |
paul@316 | 616 | def __repr__(self): |
paul@316 | 617 | return "NonBreakingSpace()" |
paul@316 | 618 | |
paul@331 | 619 | def visit(self, visitor): |
paul@346 | 620 | return visitor.nbsp(self) |
paul@316 | 621 | |
paul@12 | 622 | class Rule(Node): |
paul@12 | 623 | |
paul@12 | 624 | "A horizontal rule." |
paul@12 | 625 | |
paul@267 | 626 | def __init__(self, height): |
paul@267 | 627 | self.height = height |
paul@12 | 628 | |
paul@12 | 629 | def __repr__(self): |
paul@267 | 630 | return "Rule(%d)" % self.height |
paul@12 | 631 | |
paul@331 | 632 | def visit(self, visitor): |
paul@346 | 633 | return visitor.rule(self) |
paul@12 | 634 | |
paul@25 | 635 | class TableAttr(Node): |
paul@25 | 636 | |
paul@25 | 637 | "A table attribute." |
paul@25 | 638 | |
paul@27 | 639 | def __init__(self, name, value=None, concise=False, quote=None): |
paul@25 | 640 | self.name = name |
paul@25 | 641 | self.value = value |
paul@25 | 642 | self.concise = concise |
paul@27 | 643 | self.quote = quote |
paul@25 | 644 | |
paul@25 | 645 | def __repr__(self): |
paul@27 | 646 | return "TableAttr(%r, %r, %r, %r)" % (self.name, self.value, self.concise, self.quote) |
paul@25 | 647 | |
paul@331 | 648 | def visit(self, visitor): |
paul@346 | 649 | return visitor.table_attr(self) |
paul@25 | 650 | |
paul@12 | 651 | class Text(Node): |
paul@11 | 652 | |
paul@11 | 653 | "A text node." |
paul@11 | 654 | |
paul@11 | 655 | def __init__(self, s): |
paul@11 | 656 | self.s = s |
paul@11 | 657 | |
paul@11 | 658 | def empty(self): |
paul@11 | 659 | return not self.s |
paul@11 | 660 | |
paul@52 | 661 | def multiline(self): |
paul@52 | 662 | return "\n" in self.s |
paul@52 | 663 | |
paul@11 | 664 | def merge(self, text): |
paul@11 | 665 | self.s += text.s |
paul@11 | 666 | |
paul@11 | 667 | def __repr__(self): |
paul@11 | 668 | return "Text(%r)" % self.s |
paul@11 | 669 | |
paul@331 | 670 | def visit(self, visitor): |
paul@346 | 671 | return visitor.text(self) |
paul@11 | 672 | |
paul@169 | 673 | class Verbatim(Node): |
paul@169 | 674 | |
paul@169 | 675 | "Verbatim text." |
paul@169 | 676 | |
paul@169 | 677 | def __init__(self, text): |
paul@169 | 678 | self.text = text |
paul@169 | 679 | |
paul@169 | 680 | def __repr__(self): |
paul@169 | 681 | return "Verbatim(%r)" % self.text |
paul@169 | 682 | |
paul@331 | 683 | def visit(self, visitor): |
paul@346 | 684 | return visitor.verbatim(self) |
paul@169 | 685 | |
paul@11 | 686 | # vim: tabstop=4 expandtab shiftwidth=4 |