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