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