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