paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Moin wiki format parser. |
paul@0 | 5 | |
paul@0 | 6 | Copyright (C) 2012, 2013, 2015, 2017 Paul Boddie <paul@boddie.org.uk> |
paul@0 | 7 | |
paul@0 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@0 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@0 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@0 | 11 | version. |
paul@0 | 12 | |
paul@0 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@0 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@0 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@0 | 16 | details. |
paul@0 | 17 | |
paul@0 | 18 | You should have received a copy of the GNU General Public License along with |
paul@0 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@0 | 20 | """ |
paul@0 | 21 | |
paul@0 | 22 | from cgi import escape |
paul@0 | 23 | import re |
paul@0 | 24 | |
paul@0 | 25 | # Regular expressions. |
paul@0 | 26 | |
paul@0 | 27 | syntax = { |
paul@0 | 28 | # Page regions: |
paul@0 | 29 | "markers" : (r"^\s*([{]{3,}|[}]{3,})", re.MULTILINE | re.DOTALL), # {{{... or }}}... |
paul@0 | 30 | |
paul@0 | 31 | # Region contents: |
paul@0 | 32 | "header" : (r"\A#!(.*?)$", re.MULTILINE), # #! char-excl-nl |
paul@0 | 33 | "region text" : (r"(^\s*$)", re.MULTILINE), # blank line |
paul@0 | 34 | } |
paul@0 | 35 | |
paul@0 | 36 | # Define patterns for the regular expressions. |
paul@0 | 37 | |
paul@0 | 38 | patterns = {} |
paul@0 | 39 | for name, (value, flags) in syntax.items(): |
paul@0 | 40 | patterns[name] = re.compile(value, re.UNICODE | flags) |
paul@0 | 41 | |
paul@0 | 42 | |
paul@0 | 43 | |
paul@0 | 44 | # Document nodes. |
paul@0 | 45 | |
paul@0 | 46 | class Container: |
paul@0 | 47 | |
paul@0 | 48 | "A container of document nodes." |
paul@0 | 49 | |
paul@0 | 50 | def __init__(self, nodes): |
paul@0 | 51 | self.nodes = nodes |
paul@0 | 52 | |
paul@0 | 53 | def append(self, node): |
paul@0 | 54 | self.nodes.append(node) |
paul@0 | 55 | |
paul@0 | 56 | class Region(Container): |
paul@0 | 57 | |
paul@0 | 58 | "A region of the page." |
paul@0 | 59 | |
paul@0 | 60 | def __init__(self, nodes, level=0, type=None): |
paul@0 | 61 | Container.__init__(self, nodes) |
paul@0 | 62 | self.level = level |
paul@0 | 63 | self.type = type |
paul@0 | 64 | |
paul@0 | 65 | def have_end(self, s): |
paul@0 | 66 | return self.level and s.startswith("}") and self.level == len(s) |
paul@0 | 67 | |
paul@0 | 68 | def __repr__(self): |
paul@0 | 69 | return "Region(%r, %r, %r)" % (self.nodes, self.level, self.type) |
paul@0 | 70 | |
paul@0 | 71 | def to_string(self, out): |
paul@0 | 72 | out.start_region(self.level, self.type) |
paul@0 | 73 | for node in self.nodes: |
paul@0 | 74 | node.to_string(out) |
paul@0 | 75 | out.end_region(self.level, self.type) |
paul@0 | 76 | |
paul@0 | 77 | class Block(Container): |
paul@0 | 78 | |
paul@0 | 79 | "A block in the page." |
paul@0 | 80 | |
paul@0 | 81 | def __init__(self, nodes, final=True): |
paul@0 | 82 | Container.__init__(self, nodes) |
paul@0 | 83 | self.final = final |
paul@0 | 84 | |
paul@0 | 85 | def __repr__(self): |
paul@0 | 86 | return "Block(%r)" % self.nodes |
paul@0 | 87 | |
paul@0 | 88 | def to_string(self, out): |
paul@0 | 89 | out.start_block(self.final) |
paul@0 | 90 | for node in self.nodes: |
paul@0 | 91 | node.to_string(out) |
paul@0 | 92 | out.end_block(self.final) |
paul@0 | 93 | |
paul@0 | 94 | class Text: |
paul@0 | 95 | |
paul@0 | 96 | "A text node." |
paul@0 | 97 | |
paul@0 | 98 | def __init__(self, s): |
paul@0 | 99 | self.s = s |
paul@0 | 100 | |
paul@0 | 101 | def __repr__(self): |
paul@0 | 102 | return "Text(%r)" % self.s |
paul@0 | 103 | |
paul@0 | 104 | def to_string(self, out): |
paul@0 | 105 | out.text(self.s) |
paul@0 | 106 | |
paul@0 | 107 | |
paul@0 | 108 | |
paul@0 | 109 | # Serialisation. |
paul@0 | 110 | |
paul@0 | 111 | class Serialiser: |
paul@0 | 112 | |
paul@0 | 113 | "General serialisation support." |
paul@0 | 114 | |
paul@0 | 115 | def __init__(self, out): |
paul@0 | 116 | self.out = out |
paul@0 | 117 | |
paul@0 | 118 | class MoinSerialiser(Serialiser): |
paul@0 | 119 | |
paul@0 | 120 | "Serialisation of the page." |
paul@0 | 121 | |
paul@0 | 122 | def start_region(self, level, type): |
paul@0 | 123 | out = self.out |
paul@0 | 124 | if level: |
paul@0 | 125 | out("{" * level) # marker |
paul@0 | 126 | if type: |
paul@0 | 127 | out("#!%s" % type) # header |
paul@0 | 128 | |
paul@0 | 129 | def end_region(self, level, type): |
paul@0 | 130 | out = self.out |
paul@0 | 131 | if level: |
paul@0 | 132 | out("}" * level) # marker |
paul@0 | 133 | |
paul@0 | 134 | def start_block(self, final): |
paul@0 | 135 | pass |
paul@0 | 136 | |
paul@0 | 137 | def end_block(self, final): |
paul@0 | 138 | if not final: |
paul@0 | 139 | self.out("\n") |
paul@0 | 140 | |
paul@0 | 141 | def text(self, s): |
paul@0 | 142 | self.out(s) |
paul@0 | 143 | |
paul@0 | 144 | class HTMLSerialiser(Serialiser): |
paul@0 | 145 | |
paul@0 | 146 | "Serialisation of the page." |
paul@0 | 147 | |
paul@0 | 148 | def start_region(self, level, type): |
paul@0 | 149 | l = [] |
paul@0 | 150 | out = l.append |
paul@0 | 151 | if level: |
paul@0 | 152 | out("level-%d" % level) # marker |
paul@0 | 153 | |
paul@0 | 154 | # NOTE: Encode type details for CSS. |
paul@0 | 155 | |
paul@0 | 156 | if type: |
paul@0 | 157 | out("type-%s" % escape(type, True)) # header |
paul@0 | 158 | |
paul@0 | 159 | self.out("<span class='%s'>" % " ".join(l)) |
paul@0 | 160 | |
paul@0 | 161 | def end_region(self, level, type): |
paul@0 | 162 | self.out("</span>") |
paul@0 | 163 | |
paul@0 | 164 | def start_block(self, final): |
paul@0 | 165 | self.out("<p>") |
paul@0 | 166 | |
paul@0 | 167 | def end_block(self, final): |
paul@0 | 168 | self.out("</p>") |
paul@0 | 169 | |
paul@0 | 170 | def text(self, s): |
paul@0 | 171 | self.out(escape(s)) |
paul@0 | 172 | |
paul@0 | 173 | |
paul@0 | 174 | |
paul@0 | 175 | # Parser functions. |
paul@0 | 176 | |
paul@0 | 177 | def parse_page(s): |
paul@0 | 178 | |
paul@0 | 179 | """ |
paul@0 | 180 | Parse page text 's'. Pages consist of regions delimited by markers. |
paul@0 | 181 | """ |
paul@0 | 182 | |
paul@0 | 183 | region = Region([]) |
paul@0 | 184 | items = iter(patterns["markers"].split(s)) |
paul@0 | 185 | parse_region(items, region) |
paul@0 | 186 | return region |
paul@0 | 187 | |
paul@0 | 188 | def parse_region(items, region): |
paul@0 | 189 | |
paul@0 | 190 | "Parse the data provided by 'items' to populate 'region'." |
paul@0 | 191 | |
paul@0 | 192 | nodes = region.nodes |
paul@0 | 193 | first = True |
paul@0 | 194 | |
paul@0 | 195 | # Process exposed text and sections. |
paul@0 | 196 | |
paul@0 | 197 | try: |
paul@0 | 198 | while True: |
paul@0 | 199 | |
paul@0 | 200 | # Parse section headers. |
paul@0 | 201 | |
paul@0 | 202 | if first: |
paul@0 | 203 | match_text = parse_region_header(items.next(), region) |
paul@0 | 204 | first = False |
paul@0 | 205 | else: |
paul@0 | 206 | match_text = items.next() |
paul@0 | 207 | |
paul@0 | 208 | # Start a section if an appropriate marker is given. |
paul@0 | 209 | |
paul@0 | 210 | if match_text.startswith("{"): |
paul@0 | 211 | _region = Region([], len(match_text)) |
paul@0 | 212 | region.append(_region) |
paul@0 | 213 | parse_region(items, _region) |
paul@0 | 214 | |
paul@0 | 215 | # Interpret the given marker, closing the current section if the |
paul@0 | 216 | # given marker is the corresponding end marker for the current |
paul@0 | 217 | # section. |
paul@0 | 218 | |
paul@0 | 219 | elif region.have_end(match_text): |
paul@0 | 220 | return |
paul@0 | 221 | |
paul@0 | 222 | # Otherwise, parse text in the region. |
paul@0 | 223 | |
paul@0 | 224 | else: |
paul@0 | 225 | parse_region_text(match_text, region) |
paul@0 | 226 | |
paul@0 | 227 | except StopIteration: |
paul@0 | 228 | pass |
paul@0 | 229 | |
paul@0 | 230 | def parse_region_header(s, region): |
paul@0 | 231 | |
paul@0 | 232 | """ |
paul@0 | 233 | Parse the text 's', extracting any region header and setting it for the |
paul@0 | 234 | given 'region'. Return the remaining text. |
paul@0 | 235 | """ |
paul@0 | 236 | |
paul@0 | 237 | items = iter(patterns["header"].split(s)) |
paul@0 | 238 | pre_header = items.next() |
paul@0 | 239 | |
paul@0 | 240 | if not pre_header: |
paul@0 | 241 | region.type = items.next() |
paul@0 | 242 | return items.next() |
paul@0 | 243 | else: |
paul@0 | 244 | return pre_header |
paul@0 | 245 | |
paul@0 | 246 | def parse_region_text(s, region): |
paul@0 | 247 | |
paul@0 | 248 | "Parse the text 's' as part of 'region'." |
paul@0 | 249 | |
paul@0 | 250 | items = iter(patterns["region text"].split(s)) |
paul@0 | 251 | block = Block([]) |
paul@0 | 252 | region.append(block) |
paul@0 | 253 | |
paul@0 | 254 | try: |
paul@0 | 255 | while True: |
paul@0 | 256 | match_text = items.next() |
paul@0 | 257 | |
paul@0 | 258 | if not match_text.strip(): |
paul@0 | 259 | region.append(block) |
paul@0 | 260 | block.final = False |
paul@0 | 261 | block = Block([]) |
paul@0 | 262 | else: |
paul@0 | 263 | block.append(Text(match_text)) |
paul@0 | 264 | |
paul@0 | 265 | except StopIteration: |
paul@0 | 266 | pass |
paul@0 | 267 | |
paul@0 | 268 | # Top-level parsing function. |
paul@0 | 269 | |
paul@0 | 270 | parse = parse_page |
paul@0 | 271 | |
paul@0 | 272 | # Top-level serialising functions. |
paul@0 | 273 | |
paul@0 | 274 | def serialise(doc, serialiser=MoinSerialiser): |
paul@0 | 275 | l = [] |
paul@0 | 276 | doc.to_string(serialiser(l.append)) |
paul@0 | 277 | return "".join(l) |
paul@0 | 278 | |
paul@0 | 279 | # vim: tabstop=4 expandtab shiftwidth=4 |