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