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