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@1 | 32 | "header" : (r"\A#!(.*?)\n", 0), # #! 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@1 | 56 | def normalise(self): |
paul@1 | 57 | |
paul@1 | 58 | "Combine adjacent text nodes." |
paul@1 | 59 | |
paul@1 | 60 | nodes = self.nodes |
paul@1 | 61 | self.nodes = [] |
paul@1 | 62 | text = None |
paul@1 | 63 | |
paul@1 | 64 | for node in nodes: |
paul@1 | 65 | |
paul@1 | 66 | # Open a text node or merge text into an open node. |
paul@1 | 67 | |
paul@1 | 68 | if isinstance(node, Text): |
paul@1 | 69 | if not text: |
paul@1 | 70 | text = node |
paul@1 | 71 | else: |
paul@1 | 72 | text.merge(node) |
paul@1 | 73 | |
paul@1 | 74 | # Close any open text node and append the current node. |
paul@1 | 75 | |
paul@1 | 76 | else: |
paul@1 | 77 | if text: |
paul@1 | 78 | self.append(text) |
paul@1 | 79 | text = None |
paul@1 | 80 | self.append(node) |
paul@1 | 81 | |
paul@1 | 82 | # Add any open text node. |
paul@1 | 83 | |
paul@1 | 84 | if text: |
paul@1 | 85 | self.append(text) |
paul@1 | 86 | |
paul@0 | 87 | class Region(Container): |
paul@0 | 88 | |
paul@0 | 89 | "A region of the page." |
paul@0 | 90 | |
paul@1 | 91 | transparent_region_types = ["wiki"] |
paul@1 | 92 | |
paul@0 | 93 | def __init__(self, nodes, level=0, type=None): |
paul@0 | 94 | Container.__init__(self, nodes) |
paul@0 | 95 | self.level = level |
paul@0 | 96 | self.type = type |
paul@0 | 97 | |
paul@1 | 98 | def expand(self): |
paul@1 | 99 | |
paul@1 | 100 | """ |
paul@1 | 101 | Expand text nodes by parsing them as region text, if the region is |
paul@1 | 102 | understandable to the standard parser. |
paul@1 | 103 | """ |
paul@1 | 104 | |
paul@1 | 105 | if self.is_transparent(): |
paul@1 | 106 | nodes = self.nodes |
paul@1 | 107 | self.nodes = [] |
paul@1 | 108 | |
paul@1 | 109 | for node in nodes: |
paul@1 | 110 | if isinstance(node, Text): |
paul@1 | 111 | parse_region_text(node.s, self) |
paul@1 | 112 | else: |
paul@1 | 113 | self.append(node) |
paul@1 | 114 | |
paul@1 | 115 | def have_start(self, s): |
paul@1 | 116 | return self.is_transparent() and s.startswith("{") |
paul@1 | 117 | |
paul@0 | 118 | def have_end(self, s): |
paul@0 | 119 | return self.level and s.startswith("}") and self.level == len(s) |
paul@0 | 120 | |
paul@1 | 121 | def is_transparent(self): |
paul@1 | 122 | return not self.level or self.type in self.transparent_region_types |
paul@1 | 123 | |
paul@0 | 124 | def __repr__(self): |
paul@0 | 125 | return "Region(%r, %r, %r)" % (self.nodes, self.level, self.type) |
paul@0 | 126 | |
paul@0 | 127 | def to_string(self, out): |
paul@0 | 128 | out.start_region(self.level, self.type) |
paul@0 | 129 | for node in self.nodes: |
paul@0 | 130 | node.to_string(out) |
paul@0 | 131 | out.end_region(self.level, self.type) |
paul@0 | 132 | |
paul@0 | 133 | class Block(Container): |
paul@0 | 134 | |
paul@0 | 135 | "A block in the page." |
paul@0 | 136 | |
paul@0 | 137 | def __init__(self, nodes, final=True): |
paul@0 | 138 | Container.__init__(self, nodes) |
paul@0 | 139 | self.final = final |
paul@0 | 140 | |
paul@0 | 141 | def __repr__(self): |
paul@0 | 142 | return "Block(%r)" % self.nodes |
paul@0 | 143 | |
paul@0 | 144 | def to_string(self, out): |
paul@0 | 145 | out.start_block(self.final) |
paul@0 | 146 | for node in self.nodes: |
paul@0 | 147 | node.to_string(out) |
paul@0 | 148 | out.end_block(self.final) |
paul@0 | 149 | |
paul@0 | 150 | class Text: |
paul@0 | 151 | |
paul@0 | 152 | "A text node." |
paul@0 | 153 | |
paul@0 | 154 | def __init__(self, s): |
paul@0 | 155 | self.s = s |
paul@0 | 156 | |
paul@1 | 157 | def merge(self, text): |
paul@1 | 158 | self.s += text.s |
paul@1 | 159 | |
paul@0 | 160 | def __repr__(self): |
paul@0 | 161 | return "Text(%r)" % self.s |
paul@0 | 162 | |
paul@0 | 163 | def to_string(self, out): |
paul@0 | 164 | out.text(self.s) |
paul@0 | 165 | |
paul@0 | 166 | |
paul@0 | 167 | |
paul@0 | 168 | # Serialisation. |
paul@0 | 169 | |
paul@0 | 170 | class Serialiser: |
paul@0 | 171 | |
paul@0 | 172 | "General serialisation support." |
paul@0 | 173 | |
paul@0 | 174 | def __init__(self, out): |
paul@0 | 175 | self.out = out |
paul@0 | 176 | |
paul@0 | 177 | class MoinSerialiser(Serialiser): |
paul@0 | 178 | |
paul@0 | 179 | "Serialisation of the page." |
paul@0 | 180 | |
paul@0 | 181 | def start_region(self, level, type): |
paul@0 | 182 | out = self.out |
paul@0 | 183 | if level: |
paul@0 | 184 | out("{" * level) # marker |
paul@1 | 185 | if type and level: |
paul@1 | 186 | out("#!%s\n" % type) # header |
paul@0 | 187 | |
paul@0 | 188 | def end_region(self, level, type): |
paul@0 | 189 | out = self.out |
paul@0 | 190 | if level: |
paul@0 | 191 | out("}" * level) # marker |
paul@0 | 192 | |
paul@0 | 193 | def start_block(self, final): |
paul@0 | 194 | pass |
paul@0 | 195 | |
paul@0 | 196 | def end_block(self, final): |
paul@0 | 197 | if not final: |
paul@0 | 198 | self.out("\n") |
paul@0 | 199 | |
paul@0 | 200 | def text(self, s): |
paul@0 | 201 | self.out(s) |
paul@0 | 202 | |
paul@0 | 203 | class HTMLSerialiser(Serialiser): |
paul@0 | 204 | |
paul@0 | 205 | "Serialisation of the page." |
paul@0 | 206 | |
paul@0 | 207 | def start_region(self, level, type): |
paul@0 | 208 | l = [] |
paul@0 | 209 | out = l.append |
paul@0 | 210 | if level: |
paul@0 | 211 | out("level-%d" % level) # marker |
paul@0 | 212 | |
paul@0 | 213 | # NOTE: Encode type details for CSS. |
paul@0 | 214 | |
paul@0 | 215 | if type: |
paul@0 | 216 | out("type-%s" % escape(type, True)) # header |
paul@0 | 217 | |
paul@0 | 218 | self.out("<span class='%s'>" % " ".join(l)) |
paul@0 | 219 | |
paul@0 | 220 | def end_region(self, level, type): |
paul@0 | 221 | self.out("</span>") |
paul@0 | 222 | |
paul@0 | 223 | def start_block(self, final): |
paul@0 | 224 | self.out("<p>") |
paul@0 | 225 | |
paul@0 | 226 | def end_block(self, final): |
paul@0 | 227 | self.out("</p>") |
paul@0 | 228 | |
paul@0 | 229 | def text(self, s): |
paul@0 | 230 | self.out(escape(s)) |
paul@0 | 231 | |
paul@0 | 232 | |
paul@0 | 233 | |
paul@0 | 234 | # Parser functions. |
paul@0 | 235 | |
paul@0 | 236 | def parse_page(s): |
paul@0 | 237 | |
paul@0 | 238 | """ |
paul@0 | 239 | Parse page text 's'. Pages consist of regions delimited by markers. |
paul@0 | 240 | """ |
paul@0 | 241 | |
paul@1 | 242 | # Define tokens for interpretation by the parser. |
paul@1 | 243 | |
paul@1 | 244 | items = iter(patterns["markers"].split(s)) |
paul@1 | 245 | |
paul@1 | 246 | # Define a region for the page and parse it. |
paul@1 | 247 | |
paul@0 | 248 | region = Region([]) |
paul@0 | 249 | parse_region(items, region) |
paul@0 | 250 | return region |
paul@0 | 251 | |
paul@0 | 252 | def parse_region(items, region): |
paul@0 | 253 | |
paul@0 | 254 | "Parse the data provided by 'items' to populate 'region'." |
paul@0 | 255 | |
paul@0 | 256 | nodes = region.nodes |
paul@0 | 257 | first = True |
paul@0 | 258 | |
paul@0 | 259 | # Process exposed text and sections. |
paul@0 | 260 | |
paul@0 | 261 | try: |
paul@1 | 262 | try: |
paul@1 | 263 | while True: |
paul@0 | 264 | |
paul@1 | 265 | # Parse section headers. |
paul@0 | 266 | |
paul@1 | 267 | if first: |
paul@1 | 268 | match_text = parse_region_header(items.next(), region) |
paul@1 | 269 | first = False |
paul@1 | 270 | else: |
paul@1 | 271 | match_text = items.next() |
paul@1 | 272 | |
paul@1 | 273 | # Start a section if an appropriate marker is given. |
paul@0 | 274 | |
paul@1 | 275 | if region.have_start(match_text): |
paul@1 | 276 | |
paul@1 | 277 | # Define the section and parse it. |
paul@1 | 278 | |
paul@1 | 279 | _region = Region([], len(match_text)) |
paul@1 | 280 | region.append(_region) |
paul@1 | 281 | parse_region(items, _region) |
paul@0 | 282 | |
paul@1 | 283 | # Interpret the given marker, closing the current section if the |
paul@1 | 284 | # given marker is the corresponding end marker for the current |
paul@1 | 285 | # section. |
paul@0 | 286 | |
paul@1 | 287 | elif region.have_end(match_text): |
paul@1 | 288 | return |
paul@1 | 289 | |
paul@1 | 290 | # Otherwise, parse text in the region. |
paul@1 | 291 | |
paul@1 | 292 | else: |
paul@1 | 293 | region.append(Text(match_text)) |
paul@0 | 294 | |
paul@1 | 295 | # End of input. |
paul@0 | 296 | |
paul@1 | 297 | except StopIteration: |
paul@1 | 298 | pass |
paul@0 | 299 | |
paul@1 | 300 | finally: |
paul@1 | 301 | region.normalise() |
paul@0 | 302 | |
paul@1 | 303 | # Parse region contents, if possible. |
paul@1 | 304 | |
paul@1 | 305 | region.expand() |
paul@0 | 306 | |
paul@0 | 307 | def parse_region_header(s, region): |
paul@0 | 308 | |
paul@0 | 309 | """ |
paul@0 | 310 | Parse the text 's', extracting any region header and setting it for the |
paul@0 | 311 | given 'region'. Return the remaining text. |
paul@0 | 312 | """ |
paul@0 | 313 | |
paul@0 | 314 | items = iter(patterns["header"].split(s)) |
paul@0 | 315 | pre_header = items.next() |
paul@0 | 316 | |
paul@0 | 317 | if not pre_header: |
paul@0 | 318 | region.type = items.next() |
paul@0 | 319 | return items.next() |
paul@0 | 320 | else: |
paul@0 | 321 | return pre_header |
paul@0 | 322 | |
paul@0 | 323 | def parse_region_text(s, region): |
paul@0 | 324 | |
paul@0 | 325 | "Parse the text 's' as part of 'region'." |
paul@0 | 326 | |
paul@0 | 327 | items = iter(patterns["region text"].split(s)) |
paul@0 | 328 | block = Block([]) |
paul@0 | 329 | region.append(block) |
paul@0 | 330 | |
paul@0 | 331 | try: |
paul@0 | 332 | while True: |
paul@0 | 333 | match_text = items.next() |
paul@0 | 334 | |
paul@0 | 335 | if not match_text.strip(): |
paul@0 | 336 | region.append(block) |
paul@0 | 337 | block.final = False |
paul@0 | 338 | block = Block([]) |
paul@0 | 339 | else: |
paul@0 | 340 | block.append(Text(match_text)) |
paul@0 | 341 | |
paul@0 | 342 | except StopIteration: |
paul@0 | 343 | pass |
paul@0 | 344 | |
paul@1 | 345 | |
paul@1 | 346 | |
paul@1 | 347 | # Top-level functions. |
paul@0 | 348 | |
paul@0 | 349 | parse = parse_page |
paul@0 | 350 | |
paul@0 | 351 | def serialise(doc, serialiser=MoinSerialiser): |
paul@0 | 352 | l = [] |
paul@0 | 353 | doc.to_string(serialiser(l.append)) |
paul@0 | 354 | return "".join(l) |
paul@0 | 355 | |
paul@0 | 356 | # vim: tabstop=4 expandtab shiftwidth=4 |