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