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