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