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@11 | 6 | Copyright (C) 2017 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@11 | 29 | def append(self, node): |
paul@11 | 30 | self.nodes.append(node) |
paul@11 | 31 | |
paul@24 | 32 | def append_many(self, nodes): |
paul@24 | 33 | for node in nodes: |
paul@24 | 34 | self.append(node) |
paul@24 | 35 | |
paul@15 | 36 | add = append |
paul@15 | 37 | |
paul@17 | 38 | append_inline = append |
paul@11 | 39 | |
paul@24 | 40 | def append_inline_many(self, nodes): |
paul@24 | 41 | for node in nodes: |
paul@24 | 42 | self.append_inline(node) |
paul@24 | 43 | |
paul@11 | 44 | def empty(self): |
paul@11 | 45 | return not self.nodes |
paul@11 | 46 | |
paul@15 | 47 | def node(self, index): |
paul@15 | 48 | try: |
paul@15 | 49 | return self.nodes[index] |
paul@15 | 50 | except IndexError: |
paul@15 | 51 | return None |
paul@14 | 52 | |
paul@11 | 53 | def normalise(self): |
paul@11 | 54 | |
paul@11 | 55 | "Combine adjacent text nodes." |
paul@11 | 56 | |
paul@11 | 57 | nodes = self.nodes |
paul@11 | 58 | self.nodes = [] |
paul@11 | 59 | text = None |
paul@11 | 60 | |
paul@11 | 61 | for node in nodes: |
paul@11 | 62 | |
paul@11 | 63 | # Open a text node or merge text into an open node. |
paul@11 | 64 | |
paul@11 | 65 | if isinstance(node, Text): |
paul@11 | 66 | if not text: |
paul@11 | 67 | text = node |
paul@11 | 68 | else: |
paul@11 | 69 | text.merge(node) |
paul@11 | 70 | |
paul@11 | 71 | # Close any open text node and append the current node. |
paul@11 | 72 | |
paul@11 | 73 | else: |
paul@11 | 74 | if text: |
paul@11 | 75 | self.append(text) |
paul@11 | 76 | text = None |
paul@11 | 77 | self.append(node) |
paul@11 | 78 | |
paul@11 | 79 | # Add any open text node. |
paul@11 | 80 | |
paul@11 | 81 | if text: |
paul@11 | 82 | self.append(text) |
paul@11 | 83 | |
paul@11 | 84 | def __str__(self): |
paul@11 | 85 | return self.prettyprint() |
paul@11 | 86 | |
paul@16 | 87 | def _prettyprint(self, l, indent=""): |
paul@16 | 88 | for node in self.nodes: |
paul@16 | 89 | l.append(node.prettyprint(indent + " ")) |
paul@16 | 90 | return "\n".join(l) |
paul@16 | 91 | |
paul@16 | 92 | def _to_string(self, out): |
paul@16 | 93 | for node in self.nodes: |
paul@16 | 94 | node.to_string(out) |
paul@11 | 95 | |
paul@11 | 96 | class Region(Container): |
paul@11 | 97 | |
paul@11 | 98 | "A region of the page." |
paul@11 | 99 | |
paul@11 | 100 | transparent_region_types = ["wiki"] |
paul@11 | 101 | |
paul@11 | 102 | def __init__(self, nodes, level=0, indent=0, type=None): |
paul@11 | 103 | Container.__init__(self, nodes) |
paul@11 | 104 | self.level = level |
paul@11 | 105 | self.indent = indent |
paul@11 | 106 | self.type = type |
paul@11 | 107 | |
paul@15 | 108 | def add(self, node): |
paul@15 | 109 | last = self.node(-1) |
paul@11 | 110 | if last and last.empty(): |
paul@11 | 111 | self.nodes[-1] = node |
paul@11 | 112 | else: |
paul@22 | 113 | self.append(node) |
paul@11 | 114 | |
paul@24 | 115 | def append_inline(self, node): |
paul@11 | 116 | if self.is_transparent(): |
paul@24 | 117 | self.nodes[-1].append(node) |
paul@11 | 118 | else: |
paul@24 | 119 | self.append(node) |
paul@11 | 120 | |
paul@11 | 121 | def have_end(self, s): |
paul@11 | 122 | return self.level and s.startswith("}") and self.level == len(s) |
paul@11 | 123 | |
paul@11 | 124 | def is_transparent(self): |
paul@11 | 125 | return not self.level or self.type in self.transparent_region_types |
paul@11 | 126 | |
paul@11 | 127 | def __repr__(self): |
paul@11 | 128 | return "Region(%r, %r, %r, %r)" % (self.nodes, self.level, self.indent, self.type) |
paul@11 | 129 | |
paul@11 | 130 | def prettyprint(self, indent=""): |
paul@11 | 131 | l = ["%sRegion: level=%d indent=%d type=%s" % (indent, self.level, self.indent, self.type)] |
paul@16 | 132 | return self._prettyprint(l, indent) |
paul@11 | 133 | |
paul@11 | 134 | def to_string(self, out): |
paul@11 | 135 | out.start_region(self.level, self.indent, self.type) |
paul@16 | 136 | self._to_string(out) |
paul@11 | 137 | out.end_region(self.level, self.indent, self.type) |
paul@11 | 138 | |
paul@17 | 139 | |
paul@17 | 140 | |
paul@11 | 141 | class Block(Container): |
paul@11 | 142 | |
paul@11 | 143 | "A block in the page." |
paul@11 | 144 | |
paul@11 | 145 | def __repr__(self): |
paul@11 | 146 | return "Block(%r)" % self.nodes |
paul@11 | 147 | |
paul@11 | 148 | def prettyprint(self, indent=""): |
paul@15 | 149 | l = ["%sBlock" % indent] |
paul@16 | 150 | return self._prettyprint(l, indent) |
paul@11 | 151 | |
paul@11 | 152 | def to_string(self, out): |
paul@15 | 153 | out.start_block() |
paul@16 | 154 | self._to_string(out) |
paul@15 | 155 | out.end_block() |
paul@11 | 156 | |
paul@16 | 157 | class DefItem(Container): |
paul@16 | 158 | |
paul@16 | 159 | "A definition item." |
paul@16 | 160 | |
paul@16 | 161 | def __init__(self, nodes, pad, extra): |
paul@16 | 162 | Container.__init__(self, nodes) |
paul@16 | 163 | self.pad = pad |
paul@16 | 164 | self.extra = extra |
paul@16 | 165 | |
paul@16 | 166 | def __repr__(self): |
paul@16 | 167 | return "DefItem(%r, %r, %r)" % (self.nodes, self.pad, self.extra) |
paul@16 | 168 | |
paul@16 | 169 | def prettyprint(self, indent=""): |
paul@16 | 170 | l = ["%sDefItem: pad=%r extra=%r" % (indent, self.pad, self.extra)] |
paul@16 | 171 | return self._prettyprint(l, indent) |
paul@16 | 172 | |
paul@16 | 173 | def to_string(self, out): |
paul@16 | 174 | out.start_defitem(self.pad, self.extra) |
paul@16 | 175 | self._to_string(out) |
paul@16 | 176 | out.end_defitem(self.pad, self.extra) |
paul@16 | 177 | |
paul@16 | 178 | class DefTerm(Container): |
paul@16 | 179 | |
paul@16 | 180 | "A definition term." |
paul@16 | 181 | |
paul@16 | 182 | def __init__(self, nodes, pad): |
paul@16 | 183 | Container.__init__(self, nodes) |
paul@16 | 184 | self.pad = pad |
paul@16 | 185 | |
paul@16 | 186 | def __repr__(self): |
paul@16 | 187 | return "DefTerm(%r, %r)" % (self.nodes, self.pad) |
paul@16 | 188 | |
paul@16 | 189 | def prettyprint(self, indent=""): |
paul@16 | 190 | l = ["%sDefTerm: pad=%r" % (indent, self.pad)] |
paul@16 | 191 | return self._prettyprint(l, indent) |
paul@16 | 192 | |
paul@16 | 193 | def to_string(self, out): |
paul@16 | 194 | out.start_defterm(self.pad) |
paul@16 | 195 | self._to_string(out) |
paul@16 | 196 | out.end_defterm(self.pad) |
paul@16 | 197 | |
paul@18 | 198 | class FontStyle(Container): |
paul@18 | 199 | |
paul@18 | 200 | "Emphasised and/or strong text." |
paul@18 | 201 | |
paul@18 | 202 | def __init__(self, nodes, emphasis=False, strong=False): |
paul@18 | 203 | Container.__init__(self, nodes) |
paul@18 | 204 | self.emphasis = emphasis |
paul@18 | 205 | self.strong = strong |
paul@17 | 206 | |
paul@18 | 207 | def close_emphasis(self): |
paul@18 | 208 | if self.strong: |
paul@18 | 209 | span = FontStyle(self.nodes, emphasis=True) |
paul@18 | 210 | self.nodes = [span] |
paul@18 | 211 | self.emphasis = False |
paul@18 | 212 | return self.strong |
paul@18 | 213 | |
paul@18 | 214 | def close_strong(self): |
paul@18 | 215 | if self.emphasis: |
paul@18 | 216 | span = FontStyle(self.nodes, strong=True) |
paul@18 | 217 | self.nodes = [span] |
paul@18 | 218 | self.strong = False |
paul@18 | 219 | return self.emphasis |
paul@17 | 220 | |
paul@17 | 221 | def __repr__(self): |
paul@18 | 222 | return "FontStyle(%r, %r, %r)" % (self.nodes, self.emphasis, self.strong) |
paul@17 | 223 | |
paul@17 | 224 | def prettyprint(self, indent=""): |
paul@18 | 225 | l = ["%sFontStyle: emphasis=%r strong=%r" % (indent, self.emphasis, self.strong)] |
paul@17 | 226 | return self._prettyprint(l, indent) |
paul@17 | 227 | |
paul@17 | 228 | def to_string(self, out): |
paul@18 | 229 | if self.emphasis: |
paul@18 | 230 | out.start_emphasis() |
paul@18 | 231 | elif self.strong: |
paul@18 | 232 | out.start_strong() |
paul@17 | 233 | self._to_string(out) |
paul@18 | 234 | if self.emphasis: |
paul@18 | 235 | out.end_emphasis() |
paul@18 | 236 | elif self.strong: |
paul@18 | 237 | out.end_strong() |
paul@17 | 238 | |
paul@13 | 239 | class Heading(Container): |
paul@13 | 240 | |
paul@13 | 241 | "A heading." |
paul@13 | 242 | |
paul@13 | 243 | def __init__(self, nodes, level, start_extra="", start_pad="", end_pad="", end_extra=""): |
paul@13 | 244 | Container.__init__(self, nodes) |
paul@13 | 245 | self.level = level |
paul@13 | 246 | self.start_extra = start_extra |
paul@13 | 247 | self.start_pad = start_pad |
paul@13 | 248 | self.end_pad = end_pad |
paul@13 | 249 | self.end_extra = end_extra |
paul@13 | 250 | |
paul@13 | 251 | def __repr__(self): |
paul@13 | 252 | return "Heading(%r, %d, %r, %r, %r, %r)" % ( |
paul@13 | 253 | self.nodes, self.level, self.start_extra, self.start_pad, self.end_pad, self.end_extra) |
paul@13 | 254 | |
paul@13 | 255 | def prettyprint(self, indent=""): |
paul@13 | 256 | l = ["%sHeading: level=%d start_extra=%r start_pad=%r end_pad=%r end_extra=%r" % ( |
paul@13 | 257 | indent, self.level, self.start_extra, self.start_pad, self.end_pad, self.end_extra)] |
paul@16 | 258 | return self._prettyprint(l, indent) |
paul@13 | 259 | |
paul@13 | 260 | def to_string(self, out): |
paul@13 | 261 | out.start_heading(self.level, self.start_extra, self.start_pad) |
paul@16 | 262 | self._to_string(out) |
paul@13 | 263 | out.end_heading(self.level, self.end_pad, self.end_extra) |
paul@13 | 264 | |
paul@11 | 265 | class ListItem(Container): |
paul@11 | 266 | |
paul@11 | 267 | "A list item." |
paul@11 | 268 | |
paul@14 | 269 | def __init__(self, nodes, indent, marker, space): |
paul@14 | 270 | Container.__init__(self, nodes) |
paul@14 | 271 | self.indent = indent |
paul@14 | 272 | self.marker = marker |
paul@14 | 273 | self.space = space |
paul@14 | 274 | |
paul@11 | 275 | def __repr__(self): |
paul@14 | 276 | return "ListItem(%r, %r, %r, %r)" % (self.nodes, self.indent, self.marker, self.space) |
paul@11 | 277 | |
paul@11 | 278 | def prettyprint(self, indent=""): |
paul@14 | 279 | l = ["%sListItem: indent=%d marker=%r space=%r" % (indent, self.indent, self.marker, self.space)] |
paul@16 | 280 | return self._prettyprint(l, indent) |
paul@11 | 281 | |
paul@11 | 282 | def to_string(self, out): |
paul@14 | 283 | out.start_listitem(self.indent, self.marker, self.space) |
paul@16 | 284 | self._to_string(out) |
paul@14 | 285 | out.end_listitem(self.indent, self.marker) |
paul@11 | 286 | |
paul@25 | 287 | class TableAttrs(Container): |
paul@25 | 288 | |
paul@25 | 289 | "A collection of table attributes." |
paul@25 | 290 | |
paul@25 | 291 | def __repr__(self): |
paul@25 | 292 | return "TableAttrs(%r)" % self.nodes |
paul@25 | 293 | |
paul@25 | 294 | def prettyprint(self, indent=""): |
paul@25 | 295 | l = ["%sTableAttrs:" % indent] |
paul@25 | 296 | return self._prettyprint(l, indent) |
paul@25 | 297 | |
paul@25 | 298 | def to_string(self, out): |
paul@25 | 299 | out.start_table_attrs() |
paul@25 | 300 | self._to_string(out) |
paul@25 | 301 | out.end_table_attrs() |
paul@25 | 302 | |
paul@24 | 303 | class TableCell(Container): |
paul@24 | 304 | |
paul@24 | 305 | "A table cell." |
paul@24 | 306 | |
paul@25 | 307 | def __init__(self, nodes, attrs=None): |
paul@25 | 308 | Container.__init__(self, nodes) |
paul@25 | 309 | self.attrs = attrs |
paul@25 | 310 | |
paul@24 | 311 | def __repr__(self): |
paul@25 | 312 | return "TableCell(%r, %f)" % (self.nodes, self.attrs) |
paul@24 | 313 | |
paul@24 | 314 | def prettyprint(self, indent=""): |
paul@24 | 315 | l = ["%sTableCell:" % indent] |
paul@25 | 316 | if self.attrs: |
paul@25 | 317 | l.append(self.attrs.prettyprint(indent + " ")) |
paul@24 | 318 | return self._prettyprint(l, indent) |
paul@24 | 319 | |
paul@24 | 320 | def to_string(self, out): |
paul@25 | 321 | out.start_table_cell(self.attrs) |
paul@24 | 322 | self._to_string(out) |
paul@24 | 323 | out.end_table_cell() |
paul@24 | 324 | |
paul@24 | 325 | class TableRow(Container): |
paul@24 | 326 | |
paul@24 | 327 | "A table row." |
paul@24 | 328 | |
paul@24 | 329 | def __init__(self, nodes, trailing=""): |
paul@24 | 330 | Container.__init__(self, nodes) |
paul@24 | 331 | self.trailing = trailing |
paul@24 | 332 | |
paul@24 | 333 | def __repr__(self): |
paul@24 | 334 | return "TableRow(%r, %r)" % (self.nodes, self.trailing) |
paul@24 | 335 | |
paul@24 | 336 | def prettyprint(self, indent=""): |
paul@24 | 337 | l = ["%sTableRow: trailing=%r" % (indent, self.trailing)] |
paul@24 | 338 | return self._prettyprint(l, indent) |
paul@24 | 339 | |
paul@24 | 340 | def to_string(self, out): |
paul@24 | 341 | out.start_table_row() |
paul@24 | 342 | self._to_string(out) |
paul@24 | 343 | out.end_table_row(self.trailing) |
paul@24 | 344 | |
paul@23 | 345 | |
paul@17 | 346 | |
paul@23 | 347 | class Inline(Container): |
paul@23 | 348 | |
paul@23 | 349 | "Generic inline formatting." |
paul@17 | 350 | |
paul@17 | 351 | def __repr__(self): |
paul@23 | 352 | return "%s(%r)" % (self.__class__.__name__, self.nodes) |
paul@17 | 353 | |
paul@17 | 354 | def prettyprint(self, indent=""): |
paul@23 | 355 | l = ["%s%s" % (indent, self.__class__.__name__)] |
paul@17 | 356 | return self._prettyprint(l, indent) |
paul@17 | 357 | |
paul@23 | 358 | class Larger(Inline): |
paul@23 | 359 | |
paul@23 | 360 | "Larger text." |
paul@23 | 361 | |
paul@23 | 362 | def to_string(self, out): |
paul@23 | 363 | out.start_larger() |
paul@23 | 364 | self._to_string(out) |
paul@23 | 365 | out.end_larger() |
paul@23 | 366 | |
paul@23 | 367 | class Monospace(Inline): |
paul@23 | 368 | |
paul@23 | 369 | "Monospaced text." |
paul@23 | 370 | |
paul@17 | 371 | def to_string(self, out): |
paul@18 | 372 | out.start_monospace() |
paul@17 | 373 | self._to_string(out) |
paul@18 | 374 | out.end_monospace() |
paul@17 | 375 | |
paul@23 | 376 | class Smaller(Inline): |
paul@23 | 377 | |
paul@23 | 378 | "Smaller text." |
paul@23 | 379 | |
paul@23 | 380 | def to_string(self, out): |
paul@23 | 381 | out.start_smaller() |
paul@23 | 382 | self._to_string(out) |
paul@23 | 383 | out.end_smaller() |
paul@23 | 384 | |
paul@23 | 385 | class Subscript(Inline): |
paul@20 | 386 | |
paul@20 | 387 | "Subscripted text." |
paul@20 | 388 | |
paul@20 | 389 | def to_string(self, out): |
paul@20 | 390 | out.start_subscript() |
paul@20 | 391 | self._to_string(out) |
paul@20 | 392 | out.end_subscript() |
paul@20 | 393 | |
paul@23 | 394 | class Superscript(Inline): |
paul@20 | 395 | |
paul@20 | 396 | "Superscripted text." |
paul@20 | 397 | |
paul@20 | 398 | def to_string(self, out): |
paul@20 | 399 | out.start_superscript() |
paul@20 | 400 | self._to_string(out) |
paul@20 | 401 | out.end_superscript() |
paul@20 | 402 | |
paul@23 | 403 | class Underline(Inline): |
paul@19 | 404 | |
paul@19 | 405 | "Underlined text." |
paul@19 | 406 | |
paul@19 | 407 | def to_string(self, out): |
paul@19 | 408 | out.start_underline() |
paul@19 | 409 | self._to_string(out) |
paul@19 | 410 | out.end_underline() |
paul@19 | 411 | |
paul@12 | 412 | |
paul@12 | 413 | |
paul@12 | 414 | class Node: |
paul@12 | 415 | |
paul@12 | 416 | "A document node without children." |
paul@12 | 417 | |
paul@12 | 418 | def empty(self): |
paul@12 | 419 | return False |
paul@12 | 420 | |
paul@15 | 421 | class Break(Node): |
paul@15 | 422 | |
paul@15 | 423 | "A paragraph break." |
paul@15 | 424 | |
paul@15 | 425 | def __repr__(self): |
paul@15 | 426 | return "Break()" |
paul@15 | 427 | |
paul@15 | 428 | def prettyprint(self, indent=""): |
paul@15 | 429 | return "%sBreak" % indent |
paul@15 | 430 | |
paul@15 | 431 | def to_string(self, out): |
paul@15 | 432 | out.break_() |
paul@15 | 433 | |
paul@12 | 434 | class Rule(Node): |
paul@12 | 435 | |
paul@12 | 436 | "A horizontal rule." |
paul@12 | 437 | |
paul@12 | 438 | def __init__(self, length): |
paul@12 | 439 | self.length = length |
paul@12 | 440 | |
paul@12 | 441 | def __repr__(self): |
paul@12 | 442 | return "Rule(%d)" % self.length |
paul@12 | 443 | |
paul@12 | 444 | def prettyprint(self, indent=""): |
paul@25 | 445 | return "%sRule: length=%d" % (indent, self.length) |
paul@12 | 446 | |
paul@12 | 447 | def to_string(self, out): |
paul@12 | 448 | out.rule(self.length) |
paul@12 | 449 | |
paul@25 | 450 | class TableAttr(Node): |
paul@25 | 451 | |
paul@25 | 452 | "A table attribute." |
paul@25 | 453 | |
paul@27 | 454 | def __init__(self, name, value=None, concise=False, quote=None): |
paul@25 | 455 | self.name = name |
paul@25 | 456 | self.value = value |
paul@25 | 457 | self.concise = concise |
paul@27 | 458 | self.quote = quote |
paul@25 | 459 | |
paul@25 | 460 | def __repr__(self): |
paul@27 | 461 | return "TableAttr(%r, %r, %r, %r)" % (self.name, self.value, self.concise, self.quote) |
paul@25 | 462 | |
paul@25 | 463 | def prettyprint(self, indent=""): |
paul@27 | 464 | return "%sTableAttr: name=%r value=%r concise=%r quote=%r" % (indent, self.name, self.value, self.concise, self.quote) |
paul@25 | 465 | |
paul@25 | 466 | def to_string(self, out): |
paul@27 | 467 | out.table_attr(self.name, self.value, self.concise, self.quote) |
paul@25 | 468 | |
paul@12 | 469 | class Text(Node): |
paul@11 | 470 | |
paul@11 | 471 | "A text node." |
paul@11 | 472 | |
paul@11 | 473 | def __init__(self, s): |
paul@11 | 474 | self.s = s |
paul@11 | 475 | |
paul@11 | 476 | def empty(self): |
paul@11 | 477 | return not self.s |
paul@11 | 478 | |
paul@11 | 479 | def merge(self, text): |
paul@11 | 480 | self.s += text.s |
paul@11 | 481 | |
paul@11 | 482 | def __repr__(self): |
paul@11 | 483 | return "Text(%r)" % self.s |
paul@11 | 484 | |
paul@11 | 485 | def prettyprint(self, indent=""): |
paul@11 | 486 | return "%sText: %r" % (indent, self.s) |
paul@11 | 487 | |
paul@11 | 488 | def to_string(self, out): |
paul@11 | 489 | out.text(self.s) |
paul@11 | 490 | |
paul@11 | 491 | # vim: tabstop=4 expandtab shiftwidth=4 |