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