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