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@11 | 37 | def normalise(self): |
paul@11 | 38 | |
paul@11 | 39 | "Combine adjacent text nodes." |
paul@11 | 40 | |
paul@11 | 41 | nodes = self.nodes |
paul@11 | 42 | self.nodes = [] |
paul@11 | 43 | text = None |
paul@11 | 44 | |
paul@11 | 45 | for node in nodes: |
paul@11 | 46 | |
paul@11 | 47 | # Open a text node or merge text into an open node. |
paul@11 | 48 | |
paul@11 | 49 | if isinstance(node, Text): |
paul@11 | 50 | if not text: |
paul@11 | 51 | text = node |
paul@11 | 52 | else: |
paul@11 | 53 | text.merge(node) |
paul@11 | 54 | |
paul@11 | 55 | # Close any open text node and append the current node. |
paul@11 | 56 | |
paul@11 | 57 | else: |
paul@11 | 58 | if text: |
paul@11 | 59 | self.append(text) |
paul@11 | 60 | text = None |
paul@11 | 61 | self.append(node) |
paul@11 | 62 | |
paul@11 | 63 | # Add any open text node. |
paul@11 | 64 | |
paul@11 | 65 | if text: |
paul@11 | 66 | self.append(text) |
paul@11 | 67 | |
paul@11 | 68 | def __str__(self): |
paul@11 | 69 | return self.prettyprint() |
paul@11 | 70 | |
paul@11 | 71 | def prettyprint(self, indent=""): |
paul@11 | 72 | pass |
paul@11 | 73 | |
paul@11 | 74 | class Region(Container): |
paul@11 | 75 | |
paul@11 | 76 | "A region of the page." |
paul@11 | 77 | |
paul@11 | 78 | transparent_region_types = ["wiki"] |
paul@11 | 79 | |
paul@11 | 80 | def __init__(self, nodes, level=0, indent=0, type=None): |
paul@11 | 81 | Container.__init__(self, nodes) |
paul@11 | 82 | self.level = level |
paul@11 | 83 | self.indent = indent |
paul@11 | 84 | self.type = type |
paul@11 | 85 | |
paul@11 | 86 | def append(self, node): |
paul@11 | 87 | last = self.nodes and self.nodes[-1] |
paul@11 | 88 | if last and last.empty(): |
paul@11 | 89 | self.nodes[-1] = node |
paul@11 | 90 | else: |
paul@11 | 91 | self.nodes.append(node) |
paul@11 | 92 | |
paul@11 | 93 | def append_text(self, s): |
paul@11 | 94 | if self.is_transparent(): |
paul@11 | 95 | self.nodes[-1].append(s) |
paul@11 | 96 | else: |
paul@11 | 97 | self.append(s) |
paul@11 | 98 | |
paul@11 | 99 | def have_end(self, s): |
paul@11 | 100 | return self.level and s.startswith("}") and self.level == len(s) |
paul@11 | 101 | |
paul@11 | 102 | def is_transparent(self): |
paul@11 | 103 | return not self.level or self.type in self.transparent_region_types |
paul@11 | 104 | |
paul@11 | 105 | def __repr__(self): |
paul@11 | 106 | return "Region(%r, %r, %r, %r)" % (self.nodes, self.level, self.indent, self.type) |
paul@11 | 107 | |
paul@11 | 108 | def prettyprint(self, indent=""): |
paul@11 | 109 | l = ["%sRegion: level=%d indent=%d type=%s" % (indent, self.level, self.indent, self.type)] |
paul@11 | 110 | for node in self.nodes: |
paul@11 | 111 | l.append(node.prettyprint(indent + " ")) |
paul@11 | 112 | return "\n".join(l) |
paul@11 | 113 | |
paul@11 | 114 | def to_string(self, out): |
paul@11 | 115 | out.start_region(self.level, self.indent, self.type) |
paul@11 | 116 | for node in self.nodes: |
paul@11 | 117 | node.to_string(out) |
paul@11 | 118 | out.end_region(self.level, self.indent, self.type) |
paul@11 | 119 | |
paul@11 | 120 | class Block(Container): |
paul@11 | 121 | |
paul@11 | 122 | "A block in the page." |
paul@11 | 123 | |
paul@11 | 124 | def __init__(self, nodes, final=True): |
paul@11 | 125 | Container.__init__(self, nodes) |
paul@11 | 126 | self.final = final |
paul@11 | 127 | |
paul@11 | 128 | def __repr__(self): |
paul@11 | 129 | return "Block(%r)" % self.nodes |
paul@11 | 130 | |
paul@11 | 131 | def prettyprint(self, indent=""): |
paul@11 | 132 | l = ["%sBlock: final=%s" % (indent, self.final)] |
paul@11 | 133 | for node in self.nodes: |
paul@11 | 134 | l.append(node.prettyprint(indent + " ")) |
paul@11 | 135 | return "\n".join(l) |
paul@11 | 136 | |
paul@11 | 137 | def to_string(self, out): |
paul@11 | 138 | out.start_block(self.final) |
paul@11 | 139 | for node in self.nodes: |
paul@11 | 140 | node.to_string(out) |
paul@11 | 141 | out.end_block(self.final) |
paul@11 | 142 | |
paul@11 | 143 | class ListItem(Container): |
paul@11 | 144 | |
paul@11 | 145 | "A list item." |
paul@11 | 146 | |
paul@11 | 147 | def __repr__(self): |
paul@11 | 148 | return "ListItem(%r)" % self.nodes |
paul@11 | 149 | |
paul@11 | 150 | def prettyprint(self, indent=""): |
paul@11 | 151 | l = ["%sListItem:" % indent] |
paul@11 | 152 | for node in self.nodes: |
paul@11 | 153 | l.append(node.prettyprint(indent + " ")) |
paul@11 | 154 | return "\n".join(l) |
paul@11 | 155 | |
paul@11 | 156 | def to_string(self, out): |
paul@11 | 157 | out.start_listitem() |
paul@11 | 158 | for node in self.nodes: |
paul@11 | 159 | node.to_string(out) |
paul@11 | 160 | out.end_listitem() |
paul@11 | 161 | |
paul@11 | 162 | class Text: |
paul@11 | 163 | |
paul@11 | 164 | "A text node." |
paul@11 | 165 | |
paul@11 | 166 | def __init__(self, s): |
paul@11 | 167 | self.s = s |
paul@11 | 168 | |
paul@11 | 169 | def empty(self): |
paul@11 | 170 | return not self.s |
paul@11 | 171 | |
paul@11 | 172 | def merge(self, text): |
paul@11 | 173 | self.s += text.s |
paul@11 | 174 | |
paul@11 | 175 | def __repr__(self): |
paul@11 | 176 | return "Text(%r)" % self.s |
paul@11 | 177 | |
paul@11 | 178 | def prettyprint(self, indent=""): |
paul@11 | 179 | return "%sText: %r" % (indent, self.s) |
paul@11 | 180 | |
paul@11 | 181 | def to_string(self, out): |
paul@11 | 182 | out.text(self.s) |
paul@11 | 183 | |
paul@11 | 184 | # vim: tabstop=4 expandtab shiftwidth=4 |