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