paul@32 | 1 | #!/usr/bin/env python |
paul@32 | 2 | |
paul@32 | 3 | """ |
paul@32 | 4 | Moin wiki parsing functionality. |
paul@32 | 5 | |
paul@45 | 6 | Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@32 | 7 | |
paul@32 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@32 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@32 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@32 | 11 | version. |
paul@32 | 12 | |
paul@32 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@32 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@32 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@32 | 16 | details. |
paul@32 | 17 | |
paul@32 | 18 | You should have received a copy of the GNU General Public License along with |
paul@32 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@32 | 20 | """ |
paul@32 | 21 | |
paul@43 | 22 | from collections import defaultdict |
paul@83 | 23 | from moinformat.tree.moin import Block, Region, Text |
paul@33 | 24 | import re |
paul@33 | 25 | |
paul@33 | 26 | # Pattern management. |
paul@33 | 27 | |
paul@36 | 28 | ws_excl_nl = r"[ \f\r\t\v]" |
paul@55 | 29 | quotes = "['" '"]' # ['"] |
paul@55 | 30 | |
paul@121 | 31 | def choice(l): |
paul@121 | 32 | |
paul@121 | 33 | "Return a pattern matching a choice of patterns in 'l'." |
paul@121 | 34 | |
paul@121 | 35 | return "(%s)" % "|".join(l) |
paul@121 | 36 | |
paul@55 | 37 | def excl(s): |
paul@55 | 38 | |
paul@55 | 39 | "Return a non-matching pattern for 's'." |
paul@55 | 40 | |
paul@55 | 41 | return "(?!%s)" % s |
paul@55 | 42 | |
paul@55 | 43 | def expect(s): |
paul@55 | 44 | |
paul@55 | 45 | "Return a pattern expecting 's'." |
paul@55 | 46 | |
paul@55 | 47 | return "(?=%s)" % s |
paul@55 | 48 | |
paul@55 | 49 | def group(name, s): |
paul@55 | 50 | |
paul@58 | 51 | """ |
paul@58 | 52 | Return a pattern for the group having the given 'name' and employing the |
paul@58 | 53 | pattern string 's'. |
paul@58 | 54 | """ |
paul@55 | 55 | |
paul@55 | 56 | return "(?P<%s>%s)" % (name, s) |
paul@55 | 57 | |
paul@55 | 58 | def optional(s): |
paul@55 | 59 | |
paul@55 | 60 | "Return an optional pattern." |
paul@55 | 61 | |
paul@55 | 62 | return "(?:%s)?" % s |
paul@55 | 63 | |
paul@55 | 64 | def recur(name): |
paul@55 | 65 | |
paul@55 | 66 | "Return a test for a recurrence of group 'name'." |
paul@55 | 67 | |
paul@55 | 68 | return "(?P=%s)" % name |
paul@55 | 69 | |
paul@55 | 70 | def repeat(s, min=None, max=None): |
paul@55 | 71 | |
paul@55 | 72 | "Return a pattern matching 's' for the given 'min' and 'max' limits." |
paul@55 | 73 | |
paul@55 | 74 | return "%s{%s,%s}" % (s, min is not None and min or "", |
paul@55 | 75 | max is not None and max or "") |
paul@36 | 76 | |
paul@33 | 77 | def get_patterns(syntax): |
paul@33 | 78 | |
paul@36 | 79 | """ |
paul@36 | 80 | Define patterns for the regular expressions in the 'syntax' mapping. In each |
paul@58 | 81 | pattern, replace... |
paul@58 | 82 | |
paul@58 | 83 | \N with a pattern for matching whitespace excluding newlines |
paul@58 | 84 | \Q with a pattern for matching quotation marks |
paul@58 | 85 | |
paul@58 | 86 | Group names are also qualified with a pattern name prefix. |
paul@36 | 87 | """ |
paul@33 | 88 | |
paul@33 | 89 | patterns = {} |
paul@58 | 90 | |
paul@33 | 91 | for name, value in syntax.items(): |
paul@36 | 92 | value = value.replace(r"\N", ws_excl_nl) |
paul@55 | 93 | value = value.replace(r"\Q", quotes) |
paul@58 | 94 | |
paul@58 | 95 | # Add the name to group names as a prefix. |
paul@58 | 96 | |
paul@58 | 97 | value = value.replace("(?P<", "(?P<%s_" % name) |
paul@58 | 98 | value = value.replace("(?P=", "(?P=%s_" % name) |
paul@58 | 99 | |
paul@58 | 100 | # Record the updated expression and add an identifying null group. |
paul@58 | 101 | |
paul@58 | 102 | patterns[name] = "%s(?P<group_%s>)" % (value, name) |
paul@58 | 103 | |
paul@33 | 104 | return patterns |
paul@33 | 105 | |
paul@58 | 106 | def get_expression(d, keys): |
paul@33 | 107 | |
paul@58 | 108 | """ |
paul@58 | 109 | Return a compiled expression combining patterns in 'd' having the given |
paul@58 | 110 | 'keys'. |
paul@58 | 111 | """ |
paul@36 | 112 | |
paul@58 | 113 | subset = [] |
paul@58 | 114 | |
paul@37 | 115 | for key in keys: |
paul@58 | 116 | subset.append(d[key]) |
paul@58 | 117 | |
paul@58 | 118 | return re.compile("|".join(subset), re.UNICODE | re.MULTILINE) |
paul@36 | 119 | |
paul@36 | 120 | |
paul@32 | 121 | |
paul@32 | 122 | # Tokenising functions. |
paul@32 | 123 | |
paul@32 | 124 | class TokenStream: |
paul@32 | 125 | |
paul@32 | 126 | "A stream of tokens taken from a string." |
paul@32 | 127 | |
paul@37 | 128 | def __init__(self, s, pos=0): |
paul@32 | 129 | self.s = s |
paul@36 | 130 | self.pos = pos |
paul@45 | 131 | |
paul@45 | 132 | # Match details. |
paul@45 | 133 | |
paul@32 | 134 | self.match = None |
paul@45 | 135 | self.queued = None |
paul@58 | 136 | self.groups = {} |
paul@45 | 137 | |
paul@45 | 138 | # Pattern name details. |
paul@45 | 139 | |
paul@32 | 140 | self.matching = None |
paul@32 | 141 | |
paul@32 | 142 | def rewind(self, length): |
paul@32 | 143 | |
paul@32 | 144 | "Rewind in the string by 'length'." |
paul@32 | 145 | |
paul@32 | 146 | self.pos -= min(length, self.pos) |
paul@32 | 147 | |
paul@45 | 148 | def queue_match(self): |
paul@45 | 149 | |
paul@45 | 150 | "Rewind in the string to the start of the last match." |
paul@45 | 151 | |
paul@45 | 152 | self.queued = self.match |
paul@45 | 153 | |
paul@58 | 154 | def read_until(self, expression, remaining=True): |
paul@32 | 155 | |
paul@32 | 156 | """ |
paul@58 | 157 | Find the first match for the given 'expression'. Return the text |
paul@58 | 158 | preceding any match, the remaining text if no match was found, or None |
paul@58 | 159 | if no match was found and 'remaining' is given as a false value. |
paul@32 | 160 | """ |
paul@32 | 161 | |
paul@45 | 162 | if self.queued: |
paul@45 | 163 | self.match = self.queued |
paul@45 | 164 | self.queued = None |
paul@45 | 165 | else: |
paul@45 | 166 | self.matching = None |
paul@32 | 167 | |
paul@45 | 168 | # Find the first matching pattern. |
paul@32 | 169 | |
paul@58 | 170 | match = expression.search(self.s, self.pos) |
paul@58 | 171 | |
paul@58 | 172 | if match: |
paul@58 | 173 | for name, value in match.groupdict().items(): |
paul@58 | 174 | |
paul@58 | 175 | # Use a group with a non-null value to identify the |
paul@58 | 176 | # matching pattern. |
paul@58 | 177 | |
paul@58 | 178 | if name.startswith("group_") and value is not None: |
paul@58 | 179 | self.matching = name[len("group_"):] |
paul@58 | 180 | self.start, self.end = match.span() |
paul@45 | 181 | self.match = match |
paul@58 | 182 | break |
paul@58 | 183 | |
paul@58 | 184 | # Return the remaining text, if appropriate. |
paul@32 | 185 | |
paul@32 | 186 | if self.matching is None: |
paul@58 | 187 | self.groups = {} |
paul@32 | 188 | if remaining: |
paul@32 | 189 | return self.s[self.pos:] |
paul@32 | 190 | else: |
paul@32 | 191 | return None |
paul@32 | 192 | else: |
paul@58 | 193 | self.groups = self.filter_groups() |
paul@45 | 194 | return self.s[self.pos:self.start] |
paul@32 | 195 | |
paul@58 | 196 | def filter_groups(self): |
paul@58 | 197 | |
paul@58 | 198 | "Filter groups from the current match for the matching pattern." |
paul@58 | 199 | |
paul@59 | 200 | prefix = "%s_" % self.matching |
paul@59 | 201 | |
paul@58 | 202 | d = {} |
paul@58 | 203 | for key, value in self.match.groupdict().items(): |
paul@59 | 204 | if key.startswith(prefix): |
paul@59 | 205 | d[key[len(prefix):]] = value |
paul@58 | 206 | return d |
paul@58 | 207 | |
paul@58 | 208 | def match_group(self, group=None): |
paul@32 | 209 | |
paul@32 | 210 | """ |
paul@32 | 211 | Return the matched text, updating the position in the stream. If 'group' |
paul@32 | 212 | is specified, the indicated group in a match will be returned. |
paul@58 | 213 | Otherwise, the entire match is returned. |
paul@32 | 214 | """ |
paul@32 | 215 | |
paul@54 | 216 | self.update_pos() |
paul@54 | 217 | |
paul@32 | 218 | if self.match: |
paul@58 | 219 | if group is None: |
paul@58 | 220 | return self.s[self.start:self.end] |
paul@58 | 221 | else: |
paul@59 | 222 | return self.groups.get(group) |
paul@32 | 223 | else: |
paul@32 | 224 | return None |
paul@32 | 225 | |
paul@54 | 226 | def match_groups(self, groups=None): |
paul@51 | 227 | |
paul@54 | 228 | "Return the match 'groups', or all groups if unspecified." |
paul@54 | 229 | |
paul@54 | 230 | self.update_pos() |
paul@51 | 231 | |
paul@51 | 232 | if self.match: |
paul@54 | 233 | if groups is None: |
paul@58 | 234 | return self.groups |
paul@54 | 235 | else: |
paul@58 | 236 | l = [] |
paul@58 | 237 | for group in groups: |
paul@59 | 238 | l.append(self.groups.get(group)) |
paul@58 | 239 | return l |
paul@51 | 240 | else: |
paul@51 | 241 | return [] |
paul@51 | 242 | |
paul@54 | 243 | def update_pos(self): |
paul@54 | 244 | |
paul@54 | 245 | "Update the position in the stream." |
paul@54 | 246 | |
paul@54 | 247 | if self.match: |
paul@54 | 248 | _start, self.pos = self.match.span() |
paul@54 | 249 | else: |
paul@54 | 250 | self.pos = len(self.s) |
paul@54 | 251 | |
paul@32 | 252 | |
paul@32 | 253 | |
paul@32 | 254 | # Parser abstractions. |
paul@32 | 255 | |
paul@32 | 256 | class ParserBase: |
paul@32 | 257 | |
paul@32 | 258 | "Common parsing methods." |
paul@32 | 259 | |
paul@37 | 260 | region_pattern_names = None |
paul@37 | 261 | |
paul@87 | 262 | def __init__(self, formats=None, root=None): |
paul@32 | 263 | |
paul@32 | 264 | """ |
paul@32 | 265 | Initialise the parser with any given 'formats' mapping from region type |
paul@87 | 266 | names to parser objects. An optional 'root' indicates the document-level |
paul@87 | 267 | parser. |
paul@32 | 268 | """ |
paul@32 | 269 | |
paul@32 | 270 | self.formats = formats |
paul@87 | 271 | self.root = root |
paul@37 | 272 | |
paul@37 | 273 | def get_parser(self, format_type): |
paul@37 | 274 | |
paul@37 | 275 | """ |
paul@37 | 276 | Return a parser for 'format_type' or None if no suitable parser is found. |
paul@37 | 277 | """ |
paul@37 | 278 | |
paul@37 | 279 | if not self.formats: |
paul@37 | 280 | return None |
paul@37 | 281 | |
paul@37 | 282 | cls = self.formats.get(format_type) |
paul@37 | 283 | if cls: |
paul@87 | 284 | return cls(self.formats, self.root or self) |
paul@37 | 285 | else: |
paul@37 | 286 | return None |
paul@37 | 287 | |
paul@58 | 288 | def get_expression(self, pattern_names): |
paul@37 | 289 | |
paul@37 | 290 | "Return a mapping of the given 'pattern_names' to patterns." |
paul@37 | 291 | |
paul@58 | 292 | return get_expression(self.patterns, pattern_names) |
paul@32 | 293 | |
paul@36 | 294 | def get_items(self, s, pos=0): |
paul@32 | 295 | |
paul@36 | 296 | "Return a sequence of token items for 's' and 'pos'." |
paul@32 | 297 | |
paul@37 | 298 | return TokenStream(s, pos) |
paul@37 | 299 | |
paul@37 | 300 | def set_region(self, items, region): |
paul@37 | 301 | |
paul@37 | 302 | "Set the 'items' used to populate the given 'region'." |
paul@32 | 303 | |
paul@37 | 304 | self.items = items |
paul@37 | 305 | self.region = region |
paul@37 | 306 | |
paul@37 | 307 | def read_until(self, pattern_names, remaining=True): |
paul@36 | 308 | |
paul@37 | 309 | """ |
paul@37 | 310 | Read the next portion of input, matching using 'pattern_names'. Return |
paul@37 | 311 | the text preceding any match, the remaining text if no match was found, |
paul@37 | 312 | or None if no match was found and 'remaining' is given as a false value. |
paul@37 | 313 | """ |
paul@36 | 314 | |
paul@58 | 315 | return self.items.read_until(self.get_expression(pattern_names)) |
paul@37 | 316 | |
paul@58 | 317 | def match_group(self, group=None): |
paul@37 | 318 | |
paul@37 | 319 | """ |
paul@58 | 320 | Return the group of the matching pattern with the given 'group' |
paul@58 | 321 | identifier. If 'group' is omitted or None, return the entire match. |
paul@37 | 322 | """ |
paul@36 | 323 | |
paul@54 | 324 | return self.items.match_group(group) |
paul@37 | 325 | |
paul@54 | 326 | def matching_pattern(self): |
paul@36 | 327 | |
paul@37 | 328 | "Return the name of the matching pattern." |
paul@36 | 329 | |
paul@37 | 330 | return self.items.matching |
paul@37 | 331 | |
paul@51 | 332 | def match_groups(self): |
paul@51 | 333 | |
paul@51 | 334 | "Return the number of groups in the match." |
paul@51 | 335 | |
paul@51 | 336 | return self.items.match_groups() |
paul@51 | 337 | |
paul@37 | 338 | # Parser methods invoked from other objects. |
paul@36 | 339 | |
paul@32 | 340 | def parse(self, s): |
paul@32 | 341 | |
paul@32 | 342 | """ |
paul@32 | 343 | Parse page text 's'. Pages consist of regions delimited by markers. |
paul@32 | 344 | """ |
paul@32 | 345 | |
paul@37 | 346 | self.items = self.get_items(s) |
paul@37 | 347 | self.region = self.parse_region() |
paul@37 | 348 | return self.region |
paul@37 | 349 | |
paul@37 | 350 | def parse_region_content(self, items, region): |
paul@37 | 351 | |
paul@37 | 352 | "Parse the data provided by 'items' to populate a 'region'." |
paul@37 | 353 | |
paul@37 | 354 | self.set_region(items, region) |
paul@32 | 355 | |
paul@70 | 356 | # Parse inline and opaque regions. |
paul@70 | 357 | |
paul@70 | 358 | if not region.transparent: |
paul@70 | 359 | pattern_names = ["regionend"] |
paul@70 | 360 | |
paul@70 | 361 | # Define a block to hold text. |
paul@70 | 362 | |
paul@70 | 363 | else: |
paul@70 | 364 | self.new_block(region) |
paul@70 | 365 | pattern_names = self.region_pattern_names |
paul@37 | 366 | |
paul@70 | 367 | # Start parsing. |
paul@70 | 368 | |
paul@70 | 369 | if pattern_names: |
paul@70 | 370 | self.parse_region_details(region, pattern_names) |
paul@37 | 371 | |
paul@70 | 372 | # Reset the type if the region was not inline. |
paul@70 | 373 | |
paul@70 | 374 | if region.type == "inline": |
paul@70 | 375 | first = region.nodes and region.nodes[0] |
paul@70 | 376 | if first and isinstance(first, Text) and first.multiline(): |
paul@70 | 377 | region.type = None |
paul@37 | 378 | |
paul@37 | 379 | # Top-level parser handler methods. |
paul@37 | 380 | |
paul@52 | 381 | def parse_region(self, level=0, indent=0, type=None): |
paul@32 | 382 | |
paul@32 | 383 | """ |
paul@37 | 384 | Parse the data to populate a region with the given 'level' at the given |
paul@52 | 385 | 'indent' having the given initial 'type'. |
paul@32 | 386 | """ |
paul@32 | 387 | |
paul@52 | 388 | region = Region([], level, indent, type) |
paul@32 | 389 | |
paul@32 | 390 | # Parse section headers, then parse according to region type. |
paul@32 | 391 | |
paul@37 | 392 | self.parse_region_header(region) |
paul@37 | 393 | self.parse_region_type(region) |
paul@32 | 394 | |
paul@32 | 395 | return region |
paul@32 | 396 | |
paul@37 | 397 | def parse_region_type(self, region): |
paul@32 | 398 | |
paul@32 | 399 | """ |
paul@37 | 400 | Use configured parsers to parse 'region' based on its type. |
paul@32 | 401 | """ |
paul@32 | 402 | |
paul@32 | 403 | # Find an appropriate parser given the type. |
paul@32 | 404 | |
paul@37 | 405 | parser = self.get_parser(region.type) |
paul@70 | 406 | if not parser: |
paul@70 | 407 | region.transparent = False |
paul@70 | 408 | parser = parser or self.get_parser("moin") |
paul@70 | 409 | parser.parse_region_content(self.items, region) |
paul@32 | 410 | |
paul@37 | 411 | def parse_region_header(self, region): |
paul@32 | 412 | |
paul@32 | 413 | """ |
paul@37 | 414 | Parse the region header, setting it on the 'region' object. |
paul@32 | 415 | """ |
paul@32 | 416 | |
paul@37 | 417 | if self.read_until(["header"], False) == "": # None means no header |
paul@124 | 418 | region.args = self.match_group("args") |
paul@124 | 419 | region.type = region.args.split(" ", 1)[0] |
paul@32 | 420 | |
paul@32 | 421 | # Parsing utilities. |
paul@32 | 422 | |
paul@43 | 423 | def parse_region_details(self, region, pattern_names, strict=False): |
paul@32 | 424 | |
paul@43 | 425 | """ |
paul@43 | 426 | Search 'region' using the 'pattern_names'. If 'strict' is set to a true |
paul@43 | 427 | value, forbid the accumulation of additional textual padding. |
paul@43 | 428 | """ |
paul@32 | 429 | |
paul@32 | 430 | try: |
paul@32 | 431 | while True: |
paul@32 | 432 | |
paul@32 | 433 | # Obtain text before any marker or the end of the input. |
paul@32 | 434 | |
paul@37 | 435 | preceding = self.read_until(pattern_names) |
paul@32 | 436 | if preceding: |
paul@43 | 437 | if not strict: |
paul@43 | 438 | region.append_inline(Text(preceding)) |
paul@43 | 439 | else: |
paul@43 | 440 | break |
paul@32 | 441 | |
paul@32 | 442 | # End of input. |
paul@32 | 443 | |
paul@54 | 444 | if not self.matching_pattern(): |
paul@32 | 445 | break |
paul@32 | 446 | |
paul@32 | 447 | # Obtain any feature. |
paul@32 | 448 | |
paul@67 | 449 | feature = self.match_group("feature") or self.match_group() |
paul@54 | 450 | handler = self.handlers.get(self.matching_pattern()) |
paul@32 | 451 | |
paul@32 | 452 | # Handle each feature or add text to the region. |
paul@32 | 453 | |
paul@32 | 454 | if handler: |
paul@37 | 455 | handler(self, region) |
paul@43 | 456 | elif not strict: |
paul@43 | 457 | region.append_inline(Text(feature)) |
paul@32 | 458 | else: |
paul@43 | 459 | break |
paul@32 | 460 | |
paul@32 | 461 | except StopIteration: |
paul@32 | 462 | pass |
paul@32 | 463 | |
paul@32 | 464 | region.normalise() |
paul@32 | 465 | |
paul@43 | 466 | def add_node(self, region, node): |
paul@43 | 467 | |
paul@43 | 468 | "Add to 'region' the given 'node'." |
paul@43 | 469 | |
paul@43 | 470 | region.add(node) |
paul@43 | 471 | |
paul@43 | 472 | def append_node(self, region, node): |
paul@43 | 473 | |
paul@43 | 474 | "Append to 'region' the given 'node'." |
paul@43 | 475 | |
paul@43 | 476 | region.append(node) |
paul@43 | 477 | |
paul@37 | 478 | def end_region(self, region): |
paul@32 | 479 | |
paul@32 | 480 | "End the parsing of 'region', breaking out of the parsing loop." |
paul@32 | 481 | |
paul@32 | 482 | raise StopIteration |
paul@32 | 483 | |
paul@45 | 484 | def queue_match(self): |
paul@43 | 485 | |
paul@45 | 486 | "Queue the current match." |
paul@43 | 487 | |
paul@45 | 488 | self.items.queue_match() |
paul@43 | 489 | |
paul@43 | 490 | def new_block(self, region): |
paul@43 | 491 | |
paul@43 | 492 | "Start a new block in 'region'." |
paul@43 | 493 | |
paul@43 | 494 | self.add_node(region, Block([])) |
paul@43 | 495 | |
paul@98 | 496 | # Common handler methods. |
paul@98 | 497 | |
paul@98 | 498 | def parse_region_end(self, node): |
paul@98 | 499 | |
paul@98 | 500 | "Handle the end of a region occurring within 'node'." |
paul@98 | 501 | |
paul@98 | 502 | level = self.match_group("level") |
paul@98 | 503 | feature = self.match_group("feature") |
paul@98 | 504 | self.region.extra = self.match_group("extra") |
paul@98 | 505 | |
paul@98 | 506 | if self.region.have_end(level): |
paul@98 | 507 | raise StopIteration |
paul@98 | 508 | else: |
paul@98 | 509 | node.append_inline(Text(feature)) |
paul@98 | 510 | |
paul@32 | 511 | # vim: tabstop=4 expandtab shiftwidth=4 |