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@11 | 32 | append_text = append |
paul@11 | 33 | |
paul@11 | 34 | def empty(self): |
paul@11 | 35 | return not self.nodes |
paul@11 | 36 | |
paul@14 | 37 | def last(self): |
paul@14 | 38 | return self.nodes and self.nodes[-1] or None |
paul@14 | 39 | |
paul@11 | 40 | def normalise(self): |
paul@11 | 41 | |
paul@11 | 42 | "Combine adjacent text nodes." |
paul@11 | 43 | |
paul@11 | 44 | nodes = self.nodes |
paul@11 | 45 | self.nodes = [] |
paul@11 | 46 | text = None |
paul@11 | 47 | |
paul@11 | 48 | for node in nodes: |
paul@11 | 49 | |
paul@11 | 50 | # Open a text node or merge text into an open node. |
paul@11 | 51 | |
paul@11 | 52 | if isinstance(node, Text): |
paul@11 | 53 | if not text: |
paul@11 | 54 | text = node |
paul@11 | 55 | else: |
paul@11 | 56 | text.merge(node) |
paul@11 | 57 | |
paul@11 | 58 | # Close any open text node and append the current node. |
paul@11 | 59 | |
paul@11 | 60 | else: |
paul@11 | 61 | if text: |
paul@11 | 62 | self.append(text) |
paul@11 | 63 | text = None |
paul@11 | 64 | self.append(node) |
paul@11 | 65 | |
paul@11 | 66 | # Add any open text node. |
paul@11 | 67 | |
paul@11 | 68 | if text: |
paul@11 | 69 | self.append(text) |
paul@11 | 70 | |
paul@11 | 71 | def __str__(self): |
paul@11 | 72 | return self.prettyprint() |
paul@11 | 73 | |
paul@11 | 74 | def prettyprint(self, indent=""): |
paul@11 | 75 | pass |
paul@11 | 76 | |
paul@11 | 77 | class Region(Container): |
paul@11 | 78 | |
paul@11 | 79 | "A region of the page." |
paul@11 | 80 | |
paul@11 | 81 | transparent_region_types = ["wiki"] |
paul@11 | 82 | |
paul@11 | 83 | def __init__(self, nodes, level=0, indent=0, type=None): |
paul@11 | 84 | Container.__init__(self, nodes) |
paul@11 | 85 | self.level = level |
paul@11 | 86 | self.indent = indent |
paul@11 | 87 | self.type = type |
paul@11 | 88 | |
paul@11 | 89 | def append(self, node): |
paul@14 | 90 | last = self.last() |
paul@11 | 91 | if last and last.empty(): |
paul@11 | 92 | self.nodes[-1] = node |
paul@11 | 93 | else: |
paul@11 | 94 | self.nodes.append(node) |
paul@11 | 95 | |
paul@11 | 96 | def append_text(self, s): |
paul@11 | 97 | if self.is_transparent(): |
paul@11 | 98 | self.nodes[-1].append(s) |
paul@11 | 99 | else: |
paul@11 | 100 | self.append(s) |
paul@11 | 101 | |
paul@11 | 102 | def have_end(self, s): |
paul@11 | 103 | return self.level and s.startswith("}") and self.level == len(s) |
paul@11 | 104 | |
paul@11 | 105 | def is_transparent(self): |
paul@11 | 106 | return not self.level or self.type in self.transparent_region_types |
paul@11 | 107 | |
paul@11 | 108 | def __repr__(self): |
paul@11 | 109 | return "Region(%r, %r, %r, %r)" % (self.nodes, self.level, self.indent, self.type) |
paul@11 | 110 | |
paul@11 | 111 | def prettyprint(self, indent=""): |
paul@11 | 112 | l = ["%sRegion: level=%d indent=%d type=%s" % (indent, self.level, self.indent, self.type)] |
paul@11 | 113 | for node in self.nodes: |
paul@11 | 114 | l.append(node.prettyprint(indent + " ")) |
paul@11 | 115 | return "\n".join(l) |
paul@11 | 116 | |
paul@11 | 117 | def to_string(self, out): |
paul@11 | 118 | out.start_region(self.level, self.indent, self.type) |
paul@11 | 119 | for node in self.nodes: |
paul@11 | 120 | node.to_string(out) |
paul@11 | 121 | out.end_region(self.level, self.indent, self.type) |
paul@11 | 122 | |
paul@11 | 123 | class Block(Container): |
paul@11 | 124 | |
paul@11 | 125 | "A block in the page." |
paul@11 | 126 | |
paul@11 | 127 | def __init__(self, nodes, final=True): |
paul@11 | 128 | Container.__init__(self, nodes) |
paul@11 | 129 | self.final = final |
paul@11 | 130 | |
paul@11 | 131 | def __repr__(self): |
paul@11 | 132 | return "Block(%r)" % self.nodes |
paul@11 | 133 | |
paul@11 | 134 | def prettyprint(self, indent=""): |
paul@11 | 135 | l = ["%sBlock: final=%s" % (indent, self.final)] |
paul@11 | 136 | for node in self.nodes: |
paul@11 | 137 | l.append(node.prettyprint(indent + " ")) |
paul@11 | 138 | return "\n".join(l) |
paul@11 | 139 | |
paul@11 | 140 | def to_string(self, out): |
paul@11 | 141 | out.start_block(self.final) |
paul@11 | 142 | for node in self.nodes: |
paul@11 | 143 | node.to_string(out) |
paul@11 | 144 | out.end_block(self.final) |
paul@11 | 145 | |
paul@13 | 146 | class Heading(Container): |
paul@13 | 147 | |
paul@13 | 148 | "A heading." |
paul@13 | 149 | |
paul@13 | 150 | def __init__(self, nodes, level, start_extra="", start_pad="", end_pad="", end_extra=""): |
paul@13 | 151 | Container.__init__(self, nodes) |
paul@13 | 152 | self.level = level |
paul@13 | 153 | self.start_extra = start_extra |
paul@13 | 154 | self.start_pad = start_pad |
paul@13 | 155 | self.end_pad = end_pad |
paul@13 | 156 | self.end_extra = end_extra |
paul@13 | 157 | |
paul@13 | 158 | def __repr__(self): |
paul@13 | 159 | return "Heading(%r, %d, %r, %r, %r, %r)" % ( |
paul@13 | 160 | self.nodes, self.level, self.start_extra, self.start_pad, self.end_pad, self.end_extra) |
paul@13 | 161 | |
paul@13 | 162 | def prettyprint(self, indent=""): |
paul@13 | 163 | l = ["%sHeading: level=%d start_extra=%r start_pad=%r end_pad=%r end_extra=%r" % ( |
paul@13 | 164 | indent, self.level, self.start_extra, self.start_pad, self.end_pad, self.end_extra)] |
paul@13 | 165 | for node in self.nodes: |
paul@13 | 166 | l.append(node.prettyprint(indent + " ")) |
paul@13 | 167 | return "\n".join(l) |
paul@13 | 168 | |
paul@13 | 169 | def to_string(self, out): |
paul@13 | 170 | out.start_heading(self.level, self.start_extra, self.start_pad) |
paul@13 | 171 | for node in self.nodes: |
paul@13 | 172 | node.to_string(out) |
paul@13 | 173 | out.end_heading(self.level, self.end_pad, self.end_extra) |
paul@13 | 174 | |
paul@11 | 175 | class ListItem(Container): |
paul@11 | 176 | |
paul@11 | 177 | "A list item." |
paul@11 | 178 | |
paul@14 | 179 | def __init__(self, nodes, indent, marker, space): |
paul@14 | 180 | Container.__init__(self, nodes) |
paul@14 | 181 | self.indent = indent |
paul@14 | 182 | self.marker = marker |
paul@14 | 183 | self.space = space |
paul@14 | 184 | |
paul@11 | 185 | def __repr__(self): |
paul@14 | 186 | return "ListItem(%r, %r, %r, %r)" % (self.nodes, self.indent, self.marker, self.space) |
paul@11 | 187 | |
paul@11 | 188 | def prettyprint(self, indent=""): |
paul@14 | 189 | l = ["%sListItem: indent=%d marker=%r space=%r" % (indent, self.indent, self.marker, self.space)] |
paul@11 | 190 | for node in self.nodes: |
paul@11 | 191 | l.append(node.prettyprint(indent + " ")) |
paul@11 | 192 | return "\n".join(l) |
paul@11 | 193 | |
paul@11 | 194 | def to_string(self, out): |
paul@14 | 195 | out.start_listitem(self.indent, self.marker, self.space) |
paul@11 | 196 | for node in self.nodes: |
paul@11 | 197 | node.to_string(out) |
paul@14 | 198 | out.end_listitem(self.indent, self.marker) |
paul@11 | 199 | |
paul@12 | 200 | |
paul@12 | 201 | |
paul@12 | 202 | class Node: |
paul@12 | 203 | |
paul@12 | 204 | "A document node without children." |
paul@12 | 205 | |
paul@12 | 206 | def empty(self): |
paul@12 | 207 | return False |
paul@12 | 208 | |
paul@12 | 209 | class Rule(Node): |
paul@12 | 210 | |
paul@12 | 211 | "A horizontal rule." |
paul@12 | 212 | |
paul@12 | 213 | def __init__(self, length): |
paul@12 | 214 | self.length = length |
paul@12 | 215 | |
paul@12 | 216 | def __repr__(self): |
paul@12 | 217 | return "Rule(%d)" % self.length |
paul@12 | 218 | |
paul@12 | 219 | def prettyprint(self, indent=""): |
paul@12 | 220 | return "%sRule: %d" % (indent, self.length) |
paul@12 | 221 | |
paul@12 | 222 | def to_string(self, out): |
paul@12 | 223 | out.rule(self.length) |
paul@12 | 224 | |
paul@12 | 225 | class Text(Node): |
paul@11 | 226 | |
paul@11 | 227 | "A text node." |
paul@11 | 228 | |
paul@11 | 229 | def __init__(self, s): |
paul@11 | 230 | self.s = s |
paul@11 | 231 | |
paul@11 | 232 | def empty(self): |
paul@11 | 233 | return not self.s |
paul@11 | 234 | |
paul@11 | 235 | def merge(self, text): |
paul@11 | 236 | self.s += text.s |
paul@11 | 237 | |
paul@11 | 238 | def __repr__(self): |
paul@11 | 239 | return "Text(%r)" % self.s |
paul@11 | 240 | |
paul@11 | 241 | def prettyprint(self, indent=""): |
paul@11 | 242 | return "%sText: %r" % (indent, self.s) |
paul@11 | 243 | |
paul@11 | 244 | def to_string(self, out): |
paul@11 | 245 | out.text(self.s) |
paul@11 | 246 | |
paul@11 | 247 | # vim: tabstop=4 expandtab shiftwidth=4 |