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