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