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