1 #!/usr/bin/env python 2 3 """ 4 Moin wiki format parser. 5 6 Copyright (C) 2017, 2018, 2019 Paul Boddie <paul@boddie.org.uk> 7 8 This program is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free Software 10 Foundation; either version 3 of the License, or (at your option) any later 11 version. 12 13 This program is distributed in the hope that it will be useful, but WITHOUT 14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 details. 17 18 You should have received a copy of the GNU General Public License along with 19 this program. If not, see <http://www.gnu.org/licenses/>. 20 """ 21 22 # Document transformations. 23 24 from moinformat.macros import get_macro 25 26 # Parser functionality and pattern definition. 27 28 from moinformat.parsers.common import ParserBase, get_patterns, choice, \ 29 excl, expect, group, optional, recur, \ 30 repeat 31 32 # Serialisation. 33 34 from moinformat.serialisers import serialise 35 36 # Document tree nodes. 37 38 from moinformat.tree.moin import Anchor, Break, Comment, DefItem, DefTerm, \ 39 Directive, FontStyle, Heading, Larger, \ 40 LineBreak, Link, LinkLabel, LinkParameter, \ 41 List, ListItem, Macro, Monospace, Region, \ 42 Rule, Smaller, Strikethrough, Subscript, \ 43 Superscript, Table, TableAttr, TableAttrs, \ 44 TableCell, TableRow, Text, Transclusion, \ 45 Underline, Verbatim 46 47 # Link parsing. 48 49 from moinformat.utils.links import parse_link_target 50 51 join = "".join 52 53 class MoinParser(ParserBase): 54 55 "A wiki region parser." 56 57 format = "moin" 58 59 def __init__(self, metadata, parsers=None, root=None): 60 61 """ 62 Initialise the parser with the given 'metadata' and optional 'parsers'. 63 An optional 'root' indicates the document-level parser. 64 """ 65 66 ParserBase.__init__(self, metadata, parsers, root) 67 68 # Record certain node occurrences for later evaluation. 69 70 self.macros = [] 71 72 # Record headings for identifier disambiguation. 73 74 self.headings = [] 75 76 # Record link targets for resource identification. 77 78 self.link_targets = [] 79 80 # Principal parser methods. 81 82 def parse(self, s): 83 84 """ 85 Parse page text 's'. Pages consist of regions delimited by markers. 86 """ 87 88 self.items = self.get_items(s) 89 self.region = Region([], type="moin") 90 91 # Parse page header and directives. 92 93 self.parse_region_header(self.region) 94 self.parse_region_directives(self.region) 95 96 # Handle pages directly with this parser. Pages do not need to use an 97 # explicit format indicator. 98 99 if not self.region.type: 100 self.parse_region_content(self.items, self.region) 101 102 # Otherwise, test the type and find an appropriate parser. 103 104 else: 105 self.parse_region_type(self.region) 106 107 # Assign heading identifiers. 108 109 self.identify_headings() 110 111 return self.region 112 113 114 115 # Macro evaluation. 116 117 def evaluate_macros(self): 118 119 "Evaluate the macro nodes in the document." 120 121 for node in self.macros: 122 123 # Obtain a class for the named macro. 124 125 macro_cls = get_macro(node.name) 126 if not macro_cls: 127 continue 128 129 # Instantiate the class and evaluate the macro. 130 131 macro = macro_cls(node, self.region, self.metadata) 132 macro.evaluate() 133 134 # Metadata extraction. 135 136 def update_metadata(self, metadata): 137 138 "Update 'metadata' for the document." 139 140 if self.headings: 141 metadata.set("title", self.headings[0].text_content()) 142 else: 143 metadata.set("title", self.metadata.get("pagename")) 144 145 # Heading disambiguation. 146 147 def identify_headings(self): 148 149 "Assign identifiers to headings based on their textual content." 150 151 d = {} 152 153 for heading in self.headings: 154 text = heading.text_content() 155 156 if not d.has_key(text): 157 d[text] = 0 158 heading.identifier = text 159 else: 160 d[text] += 1 161 heading.identifier = "%s-%d" % (text, d[text]) 162 163 164 165 # Conversion back to text. 166 167 def get_serialiser(self): 168 169 "Return metadata employing Moin as the output format." 170 171 metadata = self.metadata.copy() 172 metadata.set("link_format", None) 173 metadata.set("output_context", "standalone") 174 metadata.set("output_format", "moin") 175 return metadata.get_serialiser() 176 177 178 179 # Parser methods supporting different page features. 180 181 def parse_attrname(self, attrs): 182 183 "Handle an attribute name within 'attrs'." 184 185 name = self.match_group("name") 186 attr = TableAttr(name) 187 188 preceding = self.read_until(["attrvalue"], False) 189 if preceding == "": 190 attr.quote = self.match_group("quote") 191 attr.value = self.match_group("value") 192 193 attrs.append(attr) 194 195 def parse_break(self, region): 196 197 "Handle a paragraph break within 'region'." 198 199 self.add_node(region, Break()) 200 self.new_block(region) 201 202 def parse_comment(self, region): 203 204 "Handle a comment within 'region'." 205 206 comment = self.match_group("comment") 207 extra = self.match_group("extra") 208 self.add_node(region, Comment(comment, extra)) 209 self.new_block(region) 210 211 def parse_defitem(self, region, extra=""): 212 213 "Handle a definition item within 'region'." 214 215 pad = self.match_group("pad") 216 item = DefItem([], pad, extra) 217 self.parse_region_details(item, self.listitem_pattern_names) 218 self.add_node(region, item) 219 self.new_block(region) 220 221 def parse_defterm(self, region): 222 223 "Handle a definition term within 'region'." 224 225 pad = self.match_group("pad") 226 term = DefTerm([], pad) 227 self.parse_region_details(term, ["deftermend", "deftermsep"] + self.inline_pattern_names) 228 self.add_node(region, term) 229 230 if self.matching_pattern() == "deftermsep": 231 self.parse_defitem(region) 232 233 # Add padding from the separator to the term, there being no item. 234 235 else: 236 term.extra = self.match_group("pad") 237 238 def parse_defterm_empty(self, region): 239 240 "Handle an empty definition term within 'region'." 241 242 extra = self.match_group("pad") 243 self.parse_region_details(region, ["deftermsep"]) 244 self.parse_defitem(region, extra) 245 246 def parse_directive(self, region): 247 248 "Handle a processing directive within 'region'." 249 250 directive = self.match_group("directive") 251 extra = self.match_group("extra") 252 self.add_node(region, Directive(directive, extra)) 253 self.new_block(region) 254 255 def parse_fontstyle(self, region): 256 257 "Handle emphasis and strong styles." 258 259 n = len(self.match_group("style")) 260 261 # Handle endings. 262 263 if isinstance(region, FontStyle): 264 emphasis = n in (2, 4, 5) 265 strong = n in (3, 5, 6) 266 active = True 267 268 if region.emphasis and emphasis: 269 active = region.close_emphasis() 270 n -= 2 271 if region.strong and strong: 272 active = region.close_strong() 273 n -= 3 274 275 if not active: 276 if n: 277 self.items.rewind(n) 278 raise StopIteration 279 280 elif not n: 281 return 282 283 # Handle new styles. 284 285 emphasis = n in (2, 4, 5) 286 strong = n in (3, 5, 6) 287 double = n in (4, 6) 288 289 span = FontStyle([], emphasis, strong) 290 if not double: 291 self.parse_region_details(span, self.inline_pattern_names) 292 region.append_inline(span) 293 294 def parse_halign(self, attrs): 295 296 "Handle horizontal alignment within 'attrs'." 297 298 value = self.match_group("value") 299 attr = TableAttr("align", value == "(" and "left" or value == ")" and "right" or "center", True) 300 attrs.append(attr) 301 302 def parse_heading(self, region): 303 304 "Handle a heading." 305 306 start_extra = self.match_group("extra") 307 level = len(self.match_group("level")) 308 start_pad = self.match_group("pad") 309 heading = Heading([], level, start_extra, start_pad) 310 self.parse_region_details(heading, ["headingend"] + self.inline_pattern_names) 311 self.add_node(region, heading) 312 self.new_block(region) 313 314 # Record the heading for later processing. 315 316 self.root.headings.append(heading) 317 318 def parse_heading_end(self, heading): 319 320 "Handle the end of a heading." 321 322 level = len(self.match_group("level")) 323 if heading.level == level: 324 heading.end_pad = self.match_group("pad") 325 heading.end_extra = self.match_group("extra") 326 raise StopIteration 327 328 def parse_list(self, item): 329 330 "Create a list, starting with 'item'." 331 332 list = List([item]) 333 self.parse_region_details(list, self.list_pattern_names, True) 334 return list 335 336 def parse_listitem(self, region): 337 338 "Handle a list item marker within 'region'." 339 340 indent = len(self.match_group("indent")) 341 marker = self.match_group("marker") 342 num = self.match_group("num") 343 space = self.match_group("pad") 344 345 last = region.node(-1) 346 347 new_list = not isinstance(last, (List, ListItem)) 348 same_indent = not new_list and indent == last.indent 349 new_marker = not new_list and last.marker != marker and same_indent 350 new_num = not new_list and num is not None and last.num != num and same_indent 351 352 # If the marker or number changes at the same indent, or if the indent 353 # is smaller, queue the item and end the list. 354 355 # Note that Moin format does not seek to support item renumbering, 356 # instead starting new lists on number changes. 357 358 if not new_list and (new_marker or new_num or indent < last.indent): 359 self.queue_match() 360 self.end_region(region) 361 362 # Obtain a list item and populate it. 363 364 item = ListItem([], indent, marker, space, num) 365 self.parse_region_details(item, self.listitem_pattern_names) 366 367 # Start a new list if not preceded by a list item, adding a trailing 368 # block for new elements. 369 370 if new_list: 371 item = self.parse_list(item) 372 self.add_node(region, item) 373 self.new_block(region) 374 375 # Add a nested list to the last item. 376 377 elif indent > last.indent: 378 item = self.parse_list(item) 379 self.add_node(last, item) 380 381 # Add the item to the current list. 382 383 else: 384 self.add_node(region, item) 385 386 def parse_rule(self, region): 387 388 "Handle a horizontal rule within 'region'." 389 390 length = len(self.match_group("rule")) 391 rule = Rule(length) 392 self.add_node(region, rule) 393 self.new_block(region) 394 395 def parse_section(self, region): 396 397 "Handle the start of a new section within 'region'." 398 399 # Parse the section and start a new block after the section. 400 401 indent = len(self.match_group("indent")) 402 level = len(self.match_group("level")) 403 404 section = self.parse_region(level, indent, "inline") 405 406 # If the section is inline, treat it like any other inline element. 407 408 if section.type == "inline": 409 region.append_inline(section) 410 411 # Otherwise, add it as a new block element. 412 413 else: 414 self.add_node(region, section) 415 if region.allow_blocks: 416 self.new_block(region) 417 418 def parse_table_attrs(self, cell): 419 420 "Handle the start of table attributes within 'cell'." 421 422 attrs = TableAttrs([]) 423 self.parse_region_details(attrs, self.table_attr_pattern_names) 424 425 # Test the validity of the attributes. 426 427 last = None 428 429 for node in attrs.nodes: 430 431 # Text separator nodes must be whitespace. 432 433 if isinstance(node, Text): 434 if node.s.strip(): 435 break 436 437 # Named attributes must be preceded by space if not the first. 438 439 elif last and not node.concise and not isinstance(last, Text): 440 break 441 442 last = node 443 444 # All nodes were valid: preserve the collection. 445 446 else: 447 # Add the attributes as a node, also recording their presence. 448 449 cell.append(attrs) 450 cell.attrs = attrs 451 return 452 453 # Invalid nodes were found: serialise the attributes as text. 454 455 cell.append_inline(Text(serialise(attrs, self.get_serialiser()))) 456 457 def parse_table_row(self, region): 458 459 "Handle the start of a table row within 'region'." 460 461 # Identify any active table. 462 463 table = region.node(-2) 464 block = region.node(-1) 465 466 if not (isinstance(table, Table) and block.empty()): 467 new_table = table = Table([]) 468 else: 469 new_table = None 470 471 row = TableRow([]) 472 473 while True: 474 cell = TableCell([]) 475 self.parse_region_details(cell, self.table_row_pattern_names) 476 477 # Handle the end of the row. 478 479 if self.matching_pattern() == "tableend": 480 trailing = self.match_group("extra") 481 482 # If the cell was started but not finished, convert the row into text. 483 484 if not row.nodes or not cell.empty(): 485 486 # Convert the nodes back to text. 487 488 serialiser = self.get_serialiser() 489 490 for node in row.nodes: 491 region.append_inline(Text(serialise(node, serialiser))) 492 493 region.append_inline(Text(serialise(cell, serialiser) + trailing)) 494 495 self.new_block(region) 496 return 497 498 # Append the final cell, if not empty. 499 500 else: 501 row.trailing = trailing 502 503 if not cell.empty(): 504 row.append(cell) 505 break 506 507 # A cell separator has been found. 508 509 row.append(cell) 510 511 # Add the row to the table and any new table to the region. 512 513 table.add(row) 514 if new_table: 515 self.add_node(region, new_table) 516 517 self.new_block(region) 518 519 def parse_valign(self, attrs): 520 521 "Handle vertical alignment within 'attrs'." 522 523 value = self.match_group("value") 524 attr = TableAttr("valign", value == "^" and "top" or "bottom", True) 525 attrs.append(attr) 526 527 528 529 def inline_patterns_for(self, name): 530 531 "Return active patterns for the inline element having the given 'name'." 532 533 names = self.inline_pattern_names[:] 534 names[names.index(name)] = "%send" % name 535 return names 536 537 538 539 # Inline formatting handlers. 540 541 def parse_inline(self, region, cls, pattern_name): 542 543 "Handle an inline region." 544 545 span = cls([]) 546 self.parse_region_details(span, self.inline_patterns_for(pattern_name)) 547 region.append_inline(span) 548 549 def parse_larger(self, region): 550 self.parse_inline(region, Larger, "larger") 551 552 def parse_monospace(self, region): 553 span = Monospace([]) 554 self.parse_region_details(span, ["monospaceend"]) 555 region.append_inline(span) 556 557 def parse_smaller(self, region): 558 self.parse_inline(region, Smaller, "smaller") 559 560 def parse_strike(self, region): 561 self.parse_inline(region, Strikethrough, "strike") 562 563 def parse_sub(self, region): 564 self.parse_inline(region, Subscript, "sub") 565 566 def parse_super(self, region): 567 self.parse_inline(region, Superscript, "super") 568 569 def parse_underline(self, region): 570 self.parse_inline(region, Underline, "underline") 571 572 # Link formatting handlers. 573 574 def _parse_link(self, region, cls, pattern_names): 575 target = self.match_group("target") 576 end = self.match_group("end") 577 578 # Obtain an object for the link target. 579 580 link_target = parse_link_target(target, self.metadata) 581 582 # Obtain an object for the node. 583 584 span = cls([], link_target) 585 586 # Obtain the extra details. 587 588 if not end: 589 cls = LinkLabel 590 591 # Introduce a label or parameter for each separated region. 592 593 while True: 594 param = cls([]) 595 self.parse_region_details(param, pattern_names) 596 span.append(param) 597 598 if self.matching_pattern() != "linksep": 599 break 600 601 cls = LinkParameter 602 603 region.append_inline(span) 604 605 # Record the link target for later processing. 606 607 self.root.link_targets.append(link_target) 608 609 def parse_link(self, region): 610 self._parse_link(region, Link, self.link_pattern_names) 611 612 def parse_transclusion(self, region): 613 self._parse_link(region, Transclusion, self.transclusion_pattern_names) 614 615 616 617 # Complete inline pattern handlers. 618 619 def parse_anchor(self, region): 620 target = self.match_group("target") 621 anchor = Anchor(target) 622 region.append_inline(anchor) 623 624 def parse_linebreak(self, region): 625 region.append_inline(LineBreak()) 626 627 def parse_macro(self, region): 628 name = self.match_group("name") 629 args = self.match_group("args") 630 631 # Obtain the raw arguments. Moin usually leaves it to the macro to 632 # interpret the individual arguments. 633 634 arglist = args and args.split(",") or [] 635 macro = Macro(name, arglist, region.append_point(), region) 636 region.append_inline(macro) 637 638 # Record the macro for later processing. 639 640 self.root.macros.append(macro) 641 642 def parse_verbatim(self, region): 643 text = self.match_group("verbatim") 644 region.append_inline(Verbatim(text)) 645 646 647 648 # Table attribute handlers. 649 650 def parse_table_attr(self, attrs, pattern_name): 651 652 "Handle a table attribute." 653 654 attrs.append(TableAttr(pattern_name, self.match_group("value"), True)) 655 656 def parse_colour(self, cell): 657 self.parse_table_attr(cell, "bgcolor") 658 659 def parse_colspan(self, cell): 660 self.parse_table_attr(cell, "colspan") 661 662 def parse_rowspan(self, cell): 663 self.parse_table_attr(cell, "rowspan") 664 665 def parse_width(self, cell): 666 self.parse_table_attr(cell, "width") 667 668 669 670 # Regular expressions. 671 672 syntax = { 673 # Page regions: 674 675 "regionstart" : join((group("indent", r"\N*"), # ws... (optional) 676 group("level", repeat("[{]", 3)))), # {{{... 677 678 "regionend" : join((r"\N*", # ws... (optional) 679 group("feature", join(( 680 group("level", repeat("[}]", 3)), # }}}... 681 optional(group("extra", r"\n"))))))), # nl (optional) 682 683 # Region header and directives: 684 685 "header" : join(("#!", # #! 686 group("args", ".*?"), "\n")), # text-excl-nl 687 688 "directive" : join((r"^#", # # 689 group("directive", r".*?$"), # rest of line 690 optional(group("extra", r"\n")))), # nl (optional) 691 692 # Region contents: 693 694 # Line-oriented patterns support features which require their own 695 # separate lines. 696 697 "break" : r"^(\s*?)\n", # blank line 698 699 "comment" : join((r"^##", # ## 700 group("comment", r".*?$"), # rest of line 701 optional(group("extra", r"\n")))), # nl (optional) 702 703 "defterm" : join(("^", 704 group("pad", r"\N+"), # ws... 705 expect(".+?::"))), # text :: 706 707 "defterm_empty" : join(("^", 708 group("pad", r"\N+"), # ws... 709 expect("::\s+"))), # :: ws... 710 711 "heading" : join(("^", 712 group("extra", r"\N*"), # ws... (optional) 713 group("level", "=+"), # =... 714 group("pad", r"\s+"), # ws... 715 expect(join((r".*?\N+", # text 716 recur("level"), # =... 717 r"\N*$"))))), # ws... (optional) 718 719 "listitem" : join(("^", 720 group("indent", r"\N+"), # ws... 721 group("marker", r"\*"), # list-marker 722 group("pad", r"\s*"))), # ws... (optional) 723 724 "listitem_num" : join(("^", 725 group("indent", r"\N+"), # ws... 726 group("marker", r"\d+\."), # decimal-marker 727 optional(join(("#", group("num", r"\d+")))), # # num (optional) 728 group("pad", r"\s+"))), # ws... 729 730 "listitem_alpha": join(("^", 731 group("indent", r"\N+"), # ws... 732 group("marker", r"[aA]\."), # alpha-marker 733 optional(join(("#", group("num", r"\d+")))), # # num (optional) 734 group("pad", r"\s+"))), # ws... 735 736 "listitem_roman": join(("^", 737 group("indent", r"\N+"), # ws... 738 group("marker", r"[iI]\."), # roman-marker 739 optional(join(("#", group("num", r"\d+")))), # # num (optional) 740 group("pad", r"\s+"))), # ws... 741 742 "listitem_dot" : join(("^", 743 group("indent", r"\N+"), # ws... 744 group("marker", r"\."), # dot-marker 745 group("pad", r"\s*"))), # ws... (optional) 746 747 "tablerow" : r"^\|\|", # || 748 749 # Region contents: 750 751 # Inline patterns are for markup features that appear within blocks. 752 # The patterns below start inline spans that can contain other markup 753 # features. 754 755 "fontstyle" : group("style", repeat("'", 2, 6)), # ''... 756 757 # Trivial markup balancing is done below using the end features. 758 759 "larger" : join((r"~\+", # ~+ 760 expect(r"\P*?\+~"))), # ... +~ 761 762 "monospace" : join((r"`", # ` 763 expect(r"\P*?`"))), # ... ` 764 765 "smaller" : join((r"~-", # ~- 766 expect(r"\P*?-~"))), # ... -~ 767 768 "strike" : join((r"--\(", # --( 769 expect(r"\P*?\)--"))), # ... )-- 770 771 "sub" : join((r",,", # ,, 772 expect(r"\P*?,,"))), # ... ,, 773 774 "super" : join((r"\^", # ^ 775 expect(r"\P*?\^"))), # ... ^ 776 777 "underline" : join((r"__", # __ 778 expect(r"\P*?__"))), # ... __ 779 780 # Rules are treated as inline but, unlike the above, appear without 781 # contents. 782 783 "rule" : group("rule", "-----*"), # ----... 784 785 # Links and transclusions may start inline spans. 786 787 "link" : join((r"\[\[", # [[ 788 group("target", r"\P*?"), # ... 789 choice((r"\|", # | 790 group("end", r"]]"))))), # ]] 791 792 "transclusion" : join((r"\{\{", # {{ 793 excl(r"\{"), # not-{ 794 group("target", r"\P*?"), # ... 795 choice((r"\|", # | 796 group("end", r"}}"))))), # }} 797 798 # Complete inline patterns are for markup features that do not support 799 # arbitrary content within them: 800 801 "anchor" : join((r"\(\(", # (( 802 group("target", ".*?"), # target 803 r"\)\)")), # )) 804 805 "linebreak" : r"\\\\", # \\ 806 807 "macro" : join(("<<", # << 808 group("name", "\w+?"), # digit-letter... 809 optional(join((r"\(", # ( (optional) 810 group("args", ".*?"), # not-)... 811 r"\)"))), # ) (optional) 812 ">>")), # >> 813 814 "verbatim" : join(("<<<", # <<< 815 group("verbatim", r"\P*?"), # ... 816 ">>>")), 817 818 # Ending patterns for inline features: 819 820 "largerend" : r"\+~", # +~ 821 "linkend" : r"]]", # ]] 822 "monospaceend" : r"`", # ` 823 "smallerend" : r"-~", # -~ 824 "strikeend" : r"\)--", # )-- 825 "subend" : r",,", # ,, 826 "superend" : r"\^", # ^ 827 "transclusionend": r"}}", # }} 828 "underlineend" : r"__", # __ 829 830 # Heading contents: 831 832 "headingend" : join((group("pad", r"\N+"), # ws... 833 group("level", "=+"), # =... 834 group("extra", r"\N*\n"))), # ws (optional) nl 835 836 # Link/transclusion contents: 837 838 "linksep" : r"\|", # | 839 840 # List contents: 841 842 "deftermend" : join(("::", group("pad", r"\s*?\n"))), # :: 843 # ws... (optional) 844 # nl 845 846 "deftermsep" : join(("::", group("pad", r"\s+"))), # :: 847 # ws... 848 849 "listitemend" : join((r"^", # next line 850 choice((expect(r"[^\s]"), # without indent 851 expect(r"\Z"), # end of string 852 expect(r"\N+\*"), # or with ws... list-marker 853 expect(r"\N+\d\."), # or with ws... decimal-marker 854 expect(r"\N+[aA]\."), # or with ws... alpha-marker 855 expect(r"\N+[iI]\."), # or with ws... roman-marker 856 expect(r"\N+\."), # or with ws... dot-marker 857 expect(r"\N+.+?::\s"), # or with ws... text :: ws (next defterm) 858 expect(r"\N+::\s"))))), # or with ws... :: ws (next defitem) 859 860 # Table contents: 861 862 "tableattrs" : join(("<", # lt 863 excl("<"))), # not-lt 864 865 "tablecell" : r"\|\|", # || 866 867 "tableend" : join((group("extra", r"\s*?"), # ws... (optional) 868 "^")), # next line 869 870 # Table attributes: 871 872 "tableattrsend" : r">", # > 873 "halign" : group("value", "[(:)]"), # halign-marker 874 "valign" : group("value", "[v^]"), # valign-marker 875 "colour" : group("value", join(("\#", # # 876 repeat("[0-9A-Fa-f]", 6, 6)))), # nnnnnn 877 878 "colspan" : join(("-", # - 879 group("value", "\d+"))), # n... 880 881 "rowspan" : join((r"\|", # | 882 group("value", "\d+"))), # n... 883 884 "width" : group("value", "\d+%"), # n... % 885 886 "attrname" : join((excl(r"[-\d]"), # not-dash-or-digit 887 group("name", r"[-\w]+"))), # dash-digit-letter... 888 889 "attrvalue" : join(("=", group("quote", r"\Q"), # quote 890 group("value", ".*?"), # non-quote... (optional) 891 recur("quote"))), # quote 892 } 893 894 patterns = get_patterns(syntax) 895 896 897 898 # Patterns available within certain markup features. 899 900 table_attr_pattern_names = [ 901 "attrname", "colour", "colspan", "halign", "rowspan", "tableattrsend", 902 "valign", "width" 903 ] 904 905 inline_without_links_pattern_names = [ 906 "anchor", "fontstyle", "larger", "linebreak", "macro", 907 "monospace", "regionstart", "smaller", "strike", "sub", "super", 908 "underline", "verbatim" 909 ] 910 911 inline_pattern_names = inline_without_links_pattern_names + [ 912 "link", "transclusion"] 913 914 link_pattern_names = inline_without_links_pattern_names + [ 915 "linkend", "linksep", "transclusion"] 916 917 list_pattern_names = [ 918 "listitem", "listitem_alpha", "listitem_dot", "listitem_num", 919 "listitem_roman", 920 ] 921 922 listitem_pattern_names = inline_pattern_names + ["listitemend"] 923 924 region_without_table_pattern_names = inline_pattern_names + list_pattern_names + [ 925 "break", "comment", "heading", "defterm", "defterm_empty", 926 "regionend", "rule", 927 ] 928 929 table_row_pattern_names = inline_pattern_names + [ 930 "tableattrs", "tablecell", "tableend" 931 ] 932 933 transclusion_pattern_names = inline_without_links_pattern_names + [ 934 "linksep", "transclusionend"] 935 936 # The region pattern names are specifically used by the common parser 937 # functionality. 938 939 region_pattern_names = region_without_table_pattern_names + ["tablerow"] 940 941 942 943 # Pattern handlers. 944 945 end_region = ParserBase.end_region 946 parse_section_end = ParserBase.parse_region_end 947 948 handlers = { 949 None : end_region, 950 "anchor" : parse_anchor, 951 "attrname" : parse_attrname, 952 "break" : parse_break, 953 "colour" : parse_colour, 954 "colspan" : parse_colspan, 955 "comment" : parse_comment, 956 "defterm" : parse_defterm, 957 "defterm_empty" : parse_defterm_empty, 958 "deftermend" : end_region, 959 "deftermsep" : end_region, 960 "directive" : parse_directive, 961 "fontstyle" : parse_fontstyle, 962 "halign" : parse_halign, 963 "heading" : parse_heading, 964 "headingend" : parse_heading_end, 965 "larger" : parse_larger, 966 "largerend" : end_region, 967 "linebreak" : parse_linebreak, 968 "link" : parse_link, 969 "linkend" : end_region, 970 "linksep" : end_region, 971 "macro" : parse_macro, 972 "listitemend" : end_region, 973 "listitem" : parse_listitem, 974 "listitem_alpha" : parse_listitem, 975 "listitem_dot" : parse_listitem, 976 "listitem_num" : parse_listitem, 977 "listitem_roman" : parse_listitem, 978 "monospace" : parse_monospace, 979 "monospaceend" : end_region, 980 "regionstart" : parse_section, 981 "regionend" : parse_section_end, 982 "rowspan" : parse_rowspan, 983 "rule" : parse_rule, 984 "smaller" : parse_smaller, 985 "smallerend" : end_region, 986 "strike" : parse_strike, 987 "strikeend" : end_region, 988 "sub" : parse_sub, 989 "subend" : end_region, 990 "super" : parse_super, 991 "superend" : end_region, 992 "tableattrs" : parse_table_attrs, 993 "tableattrsend" : end_region, 994 "tablerow" : parse_table_row, 995 "tablecell" : end_region, 996 "tableend" : end_region, 997 "transclusion" : parse_transclusion, 998 "transclusionend" : end_region, 999 "underline" : parse_underline, 1000 "underlineend" : end_region, 1001 "valign" : parse_valign, 1002 "verbatim" : parse_verbatim, 1003 "width" : parse_width, 1004 } 1005 1006 parser = MoinParser 1007 1008 # vim: tabstop=4 expandtab shiftwidth=4