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@6 | 29 | "regionstart" : (r"^\s*([{]{3,})", re.MULTILINE | re.DOTALL), # {{{... |
paul@6 | 30 | "regionend" : (r"^\s*([}]{3,})", re.MULTILINE | re.DOTALL), # }}}... |
paul@6 | 31 | "header" : (r"#!(.*?)\n", 0), # #! char-excl-nl |
paul@0 | 32 | |
paul@0 | 33 | # Region contents: |
paul@6 | 34 | "break" : (r"^(\s*?)\n", re.MULTILINE), # blank line |
paul@0 | 35 | } |
paul@0 | 36 | |
paul@0 | 37 | # Define patterns for the regular expressions. |
paul@0 | 38 | |
paul@0 | 39 | patterns = {} |
paul@0 | 40 | for name, (value, flags) in syntax.items(): |
paul@0 | 41 | patterns[name] = re.compile(value, re.UNICODE | flags) |
paul@0 | 42 | |
paul@0 | 43 | |
paul@0 | 44 | |
paul@0 | 45 | # Document nodes. |
paul@0 | 46 | |
paul@0 | 47 | class Container: |
paul@0 | 48 | |
paul@0 | 49 | "A container of document nodes." |
paul@0 | 50 | |
paul@0 | 51 | def __init__(self, nodes): |
paul@0 | 52 | self.nodes = nodes |
paul@0 | 53 | |
paul@0 | 54 | def append(self, node): |
paul@0 | 55 | self.nodes.append(node) |
paul@0 | 56 | |
paul@8 | 57 | append_text = append |
paul@8 | 58 | |
paul@1 | 59 | def normalise(self): |
paul@1 | 60 | |
paul@1 | 61 | "Combine adjacent text nodes." |
paul@1 | 62 | |
paul@1 | 63 | nodes = self.nodes |
paul@1 | 64 | self.nodes = [] |
paul@1 | 65 | text = None |
paul@1 | 66 | |
paul@1 | 67 | for node in nodes: |
paul@1 | 68 | |
paul@1 | 69 | # Open a text node or merge text into an open node. |
paul@1 | 70 | |
paul@1 | 71 | if isinstance(node, Text): |
paul@1 | 72 | if not text: |
paul@1 | 73 | text = node |
paul@1 | 74 | else: |
paul@1 | 75 | text.merge(node) |
paul@1 | 76 | |
paul@1 | 77 | # Close any open text node and append the current node. |
paul@1 | 78 | |
paul@1 | 79 | else: |
paul@1 | 80 | if text: |
paul@1 | 81 | self.append(text) |
paul@1 | 82 | text = None |
paul@1 | 83 | self.append(node) |
paul@1 | 84 | |
paul@1 | 85 | # Add any open text node. |
paul@1 | 86 | |
paul@1 | 87 | if text: |
paul@1 | 88 | self.append(text) |
paul@1 | 89 | |
paul@3 | 90 | def __str__(self): |
paul@3 | 91 | return self.prettyprint() |
paul@3 | 92 | |
paul@3 | 93 | def prettyprint(self, indent=""): |
paul@3 | 94 | pass |
paul@3 | 95 | |
paul@0 | 96 | class Region(Container): |
paul@0 | 97 | |
paul@0 | 98 | "A region of the page." |
paul@0 | 99 | |
paul@1 | 100 | transparent_region_types = ["wiki"] |
paul@1 | 101 | |
paul@0 | 102 | def __init__(self, nodes, level=0, type=None): |
paul@0 | 103 | Container.__init__(self, nodes) |
paul@0 | 104 | self.level = level |
paul@0 | 105 | self.type = type |
paul@0 | 106 | |
paul@8 | 107 | def append_text(self, s): |
paul@8 | 108 | if self.is_transparent(): |
paul@8 | 109 | self.nodes[-1].append(s) |
paul@8 | 110 | else: |
paul@8 | 111 | self.append(s) |
paul@8 | 112 | |
paul@0 | 113 | def have_end(self, s): |
paul@0 | 114 | return self.level and s.startswith("}") and self.level == len(s) |
paul@0 | 115 | |
paul@1 | 116 | def is_transparent(self): |
paul@1 | 117 | return not self.level or self.type in self.transparent_region_types |
paul@1 | 118 | |
paul@0 | 119 | def __repr__(self): |
paul@0 | 120 | return "Region(%r, %r, %r)" % (self.nodes, self.level, self.type) |
paul@0 | 121 | |
paul@3 | 122 | def prettyprint(self, indent=""): |
paul@3 | 123 | l = ["%sRegion: level=%d type=%s" % (indent, self.level, self.type)] |
paul@3 | 124 | for node in self.nodes: |
paul@3 | 125 | l.append(node.prettyprint(indent + " ")) |
paul@3 | 126 | return "\n".join(l) |
paul@3 | 127 | |
paul@0 | 128 | def to_string(self, out): |
paul@0 | 129 | out.start_region(self.level, self.type) |
paul@0 | 130 | for node in self.nodes: |
paul@0 | 131 | node.to_string(out) |
paul@0 | 132 | out.end_region(self.level, self.type) |
paul@0 | 133 | |
paul@0 | 134 | class Block(Container): |
paul@0 | 135 | |
paul@0 | 136 | "A block in the page." |
paul@0 | 137 | |
paul@0 | 138 | def __init__(self, nodes, final=True): |
paul@0 | 139 | Container.__init__(self, nodes) |
paul@0 | 140 | self.final = final |
paul@0 | 141 | |
paul@0 | 142 | def __repr__(self): |
paul@0 | 143 | return "Block(%r)" % self.nodes |
paul@0 | 144 | |
paul@3 | 145 | def prettyprint(self, indent=""): |
paul@3 | 146 | l = ["%sBlock: final=%s" % (indent, self.final)] |
paul@3 | 147 | for node in self.nodes: |
paul@3 | 148 | l.append(node.prettyprint(indent + " ")) |
paul@3 | 149 | return "\n".join(l) |
paul@3 | 150 | |
paul@0 | 151 | def to_string(self, out): |
paul@0 | 152 | out.start_block(self.final) |
paul@0 | 153 | for node in self.nodes: |
paul@0 | 154 | node.to_string(out) |
paul@0 | 155 | out.end_block(self.final) |
paul@0 | 156 | |
paul@0 | 157 | class Text: |
paul@0 | 158 | |
paul@0 | 159 | "A text node." |
paul@0 | 160 | |
paul@0 | 161 | def __init__(self, s): |
paul@0 | 162 | self.s = s |
paul@0 | 163 | |
paul@1 | 164 | def merge(self, text): |
paul@1 | 165 | self.s += text.s |
paul@1 | 166 | |
paul@0 | 167 | def __repr__(self): |
paul@0 | 168 | return "Text(%r)" % self.s |
paul@0 | 169 | |
paul@3 | 170 | def prettyprint(self, indent=""): |
paul@3 | 171 | return "%sText: %r" % (indent, self.s) |
paul@3 | 172 | |
paul@0 | 173 | def to_string(self, out): |
paul@0 | 174 | out.text(self.s) |
paul@0 | 175 | |
paul@0 | 176 | |
paul@0 | 177 | |
paul@0 | 178 | # Serialisation. |
paul@0 | 179 | |
paul@0 | 180 | class Serialiser: |
paul@0 | 181 | |
paul@0 | 182 | "General serialisation support." |
paul@0 | 183 | |
paul@0 | 184 | def __init__(self, out): |
paul@0 | 185 | self.out = out |
paul@0 | 186 | |
paul@0 | 187 | class MoinSerialiser(Serialiser): |
paul@0 | 188 | |
paul@0 | 189 | "Serialisation of the page." |
paul@0 | 190 | |
paul@0 | 191 | def start_region(self, level, type): |
paul@0 | 192 | out = self.out |
paul@0 | 193 | if level: |
paul@0 | 194 | out("{" * level) # marker |
paul@1 | 195 | if type and level: |
paul@1 | 196 | out("#!%s\n" % type) # header |
paul@0 | 197 | |
paul@0 | 198 | def end_region(self, level, type): |
paul@0 | 199 | out = self.out |
paul@0 | 200 | if level: |
paul@0 | 201 | out("}" * level) # marker |
paul@0 | 202 | |
paul@0 | 203 | def start_block(self, final): |
paul@0 | 204 | pass |
paul@0 | 205 | |
paul@0 | 206 | def end_block(self, final): |
paul@0 | 207 | if not final: |
paul@0 | 208 | self.out("\n") |
paul@0 | 209 | |
paul@0 | 210 | def text(self, s): |
paul@0 | 211 | self.out(s) |
paul@0 | 212 | |
paul@0 | 213 | class HTMLSerialiser(Serialiser): |
paul@0 | 214 | |
paul@0 | 215 | "Serialisation of the page." |
paul@0 | 216 | |
paul@0 | 217 | def start_region(self, level, type): |
paul@0 | 218 | l = [] |
paul@0 | 219 | out = l.append |
paul@0 | 220 | if level: |
paul@0 | 221 | out("level-%d" % level) # marker |
paul@0 | 222 | |
paul@0 | 223 | # NOTE: Encode type details for CSS. |
paul@0 | 224 | |
paul@0 | 225 | if type: |
paul@0 | 226 | out("type-%s" % escape(type, True)) # header |
paul@0 | 227 | |
paul@0 | 228 | self.out("<span class='%s'>" % " ".join(l)) |
paul@0 | 229 | |
paul@0 | 230 | def end_region(self, level, type): |
paul@0 | 231 | self.out("</span>") |
paul@0 | 232 | |
paul@0 | 233 | def start_block(self, final): |
paul@0 | 234 | self.out("<p>") |
paul@0 | 235 | |
paul@0 | 236 | def end_block(self, final): |
paul@0 | 237 | self.out("</p>") |
paul@0 | 238 | |
paul@0 | 239 | def text(self, s): |
paul@0 | 240 | self.out(escape(s)) |
paul@0 | 241 | |
paul@0 | 242 | |
paul@0 | 243 | |
paul@2 | 244 | # Tokenising functions. |
paul@2 | 245 | |
paul@2 | 246 | class TokenStream: |
paul@2 | 247 | |
paul@2 | 248 | "A stream of tokens taken from a string." |
paul@2 | 249 | |
paul@2 | 250 | def __init__(self, s): |
paul@2 | 251 | self.s = s |
paul@2 | 252 | self.pos = 0 |
paul@2 | 253 | self.match = None |
paul@2 | 254 | self.matching = None |
paul@2 | 255 | |
paul@2 | 256 | def read_until(self, pattern_names, remaining=True): |
paul@2 | 257 | |
paul@2 | 258 | """ |
paul@2 | 259 | Find the first match for the given 'pattern_names'. Return the text |
paul@2 | 260 | preceding any match, the remaining text if no match was found, or None |
paul@2 | 261 | if no match was found and 'remaining' is given as a false value. |
paul@2 | 262 | """ |
paul@2 | 263 | |
paul@2 | 264 | first = None |
paul@2 | 265 | self.matching = None |
paul@2 | 266 | |
paul@2 | 267 | # Find the first matching pattern. |
paul@2 | 268 | |
paul@2 | 269 | for pattern_name in pattern_names: |
paul@2 | 270 | match = patterns[pattern_name].search(self.s, self.pos) |
paul@2 | 271 | if match: |
paul@2 | 272 | start, end = match.span() |
paul@2 | 273 | if self.matching is None or start < first: |
paul@2 | 274 | first = start |
paul@2 | 275 | self.matching = pattern_name |
paul@2 | 276 | self.match = match |
paul@2 | 277 | |
paul@2 | 278 | if self.matching is None: |
paul@2 | 279 | if remaining: |
paul@2 | 280 | return self.s[self.pos:] |
paul@2 | 281 | else: |
paul@2 | 282 | return None |
paul@2 | 283 | else: |
paul@2 | 284 | return self.s[self.pos:first] |
paul@2 | 285 | |
paul@2 | 286 | def read_match(self): |
paul@2 | 287 | |
paul@2 | 288 | "Return the matched text, updating the position in the stream." |
paul@2 | 289 | |
paul@2 | 290 | if self.match: |
paul@2 | 291 | _start, self.pos = self.match.span() |
paul@2 | 292 | s = self.match.group(1) |
paul@2 | 293 | return s |
paul@2 | 294 | else: |
paul@2 | 295 | self.pos = len(self.s) |
paul@2 | 296 | return None |
paul@2 | 297 | |
paul@2 | 298 | |
paul@2 | 299 | |
paul@0 | 300 | # Parser functions. |
paul@0 | 301 | |
paul@0 | 302 | def parse_page(s): |
paul@0 | 303 | |
paul@0 | 304 | """ |
paul@0 | 305 | Parse page text 's'. Pages consist of regions delimited by markers. |
paul@0 | 306 | """ |
paul@0 | 307 | |
paul@6 | 308 | return parse_region(TokenStream(s)) |
paul@1 | 309 | |
paul@6 | 310 | def parse_region(items, level=0): |
paul@1 | 311 | |
paul@6 | 312 | """ |
paul@6 | 313 | Parse the data provided by 'items' to populate a region at the given |
paul@6 | 314 | 'level'. |
paul@6 | 315 | """ |
paul@0 | 316 | |
paul@6 | 317 | region = Region([], level) |
paul@0 | 318 | |
paul@2 | 319 | # Parse section headers. |
paul@2 | 320 | |
paul@2 | 321 | parse_region_header(items, region) |
paul@2 | 322 | |
paul@8 | 323 | # Parse section body. |
paul@8 | 324 | |
paul@2 | 325 | if region.is_transparent(): |
paul@2 | 326 | parse_region_wiki(items, region) |
paul@2 | 327 | else: |
paul@2 | 328 | parse_region_opaque(items, region) |
paul@2 | 329 | |
paul@6 | 330 | return region |
paul@6 | 331 | |
paul@6 | 332 | def parse_region_header(items, region): |
paul@6 | 333 | |
paul@6 | 334 | """ |
paul@6 | 335 | Parse the region header from the 'items', setting it for the given 'region'. |
paul@6 | 336 | """ |
paul@6 | 337 | |
paul@6 | 338 | if items.read_until(["header"], False) == "": # None means no header |
paul@6 | 339 | region.type = items.read_match() |
paul@6 | 340 | |
paul@2 | 341 | def parse_region_wiki(items, region): |
paul@2 | 342 | |
paul@2 | 343 | "Parse the data provided by 'items' to populate a wiki 'region'." |
paul@0 | 344 | |
paul@8 | 345 | new_block(region) |
paul@8 | 346 | parse_region_details(items, region, ["break", "regionstart", "regionend"]) |
paul@0 | 347 | |
paul@8 | 348 | def parse_region_opaque(items, region): |
paul@1 | 349 | |
paul@8 | 350 | "Parse the data provided by 'items' to populate an opaque 'region'." |
paul@8 | 351 | |
paul@8 | 352 | parse_region_details(items, region, ["regionend"]) |
paul@1 | 353 | |
paul@8 | 354 | def parse_region_details(items, region, pattern_names): |
paul@0 | 355 | |
paul@8 | 356 | "Parse 'items' within 'region' searching using 'pattern_names'." |
paul@0 | 357 | |
paul@8 | 358 | try: |
paul@8 | 359 | while True: |
paul@0 | 360 | |
paul@8 | 361 | # Obtain text before any marker or the end of the input. |
paul@2 | 362 | |
paul@8 | 363 | preceding = items.read_until(pattern_names) |
paul@8 | 364 | if preceding: |
paul@8 | 365 | region.append_text(Text(preceding)) |
paul@2 | 366 | |
paul@8 | 367 | # End of input. |
paul@0 | 368 | |
paul@8 | 369 | if not items.matching: |
paul@8 | 370 | break |
paul@8 | 371 | |
paul@8 | 372 | # Obtain any feature. |
paul@2 | 373 | |
paul@8 | 374 | feature = items.read_match() |
paul@8 | 375 | handler = handlers.get(items.matching) |
paul@2 | 376 | |
paul@8 | 377 | # Handle each feature or add text to the region. |
paul@2 | 378 | |
paul@8 | 379 | if handler: |
paul@8 | 380 | handler(items, region) |
paul@8 | 381 | else: |
paul@8 | 382 | region.append_text(Text(feature)) |
paul@2 | 383 | |
paul@8 | 384 | except StopIteration: |
paul@8 | 385 | pass |
paul@2 | 386 | |
paul@2 | 387 | region.normalise() |
paul@0 | 388 | |
paul@8 | 389 | def end_region(items, region): |
paul@7 | 390 | |
paul@8 | 391 | "End the parsing of 'region'." |
paul@7 | 392 | |
paul@8 | 393 | raise StopIteration |
paul@7 | 394 | |
paul@8 | 395 | def parse_break(items, region): |
paul@8 | 396 | |
paul@8 | 397 | "Handle a paragraph break within 'region'." |
paul@7 | 398 | |
paul@7 | 399 | # Mark any previous block as not being the final one in a sequence. |
paul@7 | 400 | |
paul@7 | 401 | block = region.nodes[-1] |
paul@7 | 402 | block.final = False |
paul@8 | 403 | new_block(region) |
paul@2 | 404 | |
paul@8 | 405 | def parse_section(items, region): |
paul@2 | 406 | |
paul@8 | 407 | "Handle the start of a new section within 'region'." |
paul@2 | 408 | |
paul@8 | 409 | # Parse the section and start a new block after the section. |
paul@2 | 410 | |
paul@8 | 411 | level = len(items.read_match()) |
paul@8 | 412 | region.append(parse_region(items, level)) |
paul@8 | 413 | new_block(region) |
paul@2 | 414 | |
paul@8 | 415 | def parse_section_end(items, region): |
paul@2 | 416 | |
paul@8 | 417 | "Handle the end of a new section within 'region'." |
paul@1 | 418 | |
paul@8 | 419 | feature = items.read_match() |
paul@8 | 420 | if region.have_end(feature): |
paul@8 | 421 | raise StopIteration |
paul@8 | 422 | else: |
paul@8 | 423 | region.append_text(Text(feature)) |
paul@2 | 424 | |
paul@8 | 425 | # Pattern handlers. |
paul@2 | 426 | |
paul@8 | 427 | handlers = { |
paul@8 | 428 | None : end_region, |
paul@8 | 429 | "break" : parse_break, |
paul@8 | 430 | "regionstart" : parse_section, |
paul@8 | 431 | "regionend" : parse_section_end, |
paul@8 | 432 | } |
paul@2 | 433 | |
paul@6 | 434 | def new_block(region): |
paul@6 | 435 | |
paul@6 | 436 | "Start a new block in 'region'." |
paul@0 | 437 | |
paul@6 | 438 | block = Block([]) |
paul@6 | 439 | region.append(block) |
paul@0 | 440 | |
paul@1 | 441 | |
paul@1 | 442 | |
paul@1 | 443 | # Top-level functions. |
paul@0 | 444 | |
paul@0 | 445 | parse = parse_page |
paul@0 | 446 | |
paul@0 | 447 | def serialise(doc, serialiser=MoinSerialiser): |
paul@0 | 448 | l = [] |
paul@0 | 449 | doc.to_string(serialiser(l.append)) |
paul@0 | 450 | return "".join(l) |
paul@0 | 451 | |
paul@0 | 452 | # vim: tabstop=4 expandtab shiftwidth=4 |