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