paul@0 | 1 | #!/usr/bin/env python |
paul@0 | 2 | |
paul@0 | 3 | """ |
paul@0 | 4 | Moin wiki format parser. |
paul@0 | 5 | |
paul@11 | 6 | Copyright (C) 2017 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@31 | 215 | item = ListItem([], indent, marker, space) |
paul@37 | 216 | self.parse_region_details(item, self.listitem_pattern_names) |
paul@43 | 217 | |
paul@43 | 218 | last = region.node(-1) |
paul@43 | 219 | |
paul@43 | 220 | # Start a new list if not preceded by a list item. |
paul@43 | 221 | |
paul@43 | 222 | if not isinstance(last, ListItem): |
paul@43 | 223 | item = self.parse_list(item) |
paul@43 | 224 | |
paul@43 | 225 | # End the current list if the indent or marker is different from the |
paul@43 | 226 | # last list item. |
paul@43 | 227 | |
paul@43 | 228 | elif last.indent != indent or last.marker != marker: |
paul@43 | 229 | |
paul@43 | 230 | # Queue the new list, end this list, causing the new list to be |
paul@43 | 231 | # added after this one. |
paul@43 | 232 | |
paul@43 | 233 | self.queue_region(self.parse_list(item), region) |
paul@43 | 234 | self.end_region(region) |
paul@43 | 235 | |
paul@43 | 236 | # Add a new item in a list or a completed nested list. |
paul@43 | 237 | |
paul@43 | 238 | self.add_node(region, item) |
paul@43 | 239 | self.new_block(region) |
paul@31 | 240 | |
paul@37 | 241 | def parse_rule(self, region): |
paul@31 | 242 | |
paul@31 | 243 | "Handle a horizontal rule within 'region'." |
paul@31 | 244 | |
paul@37 | 245 | length = len(self.read_match(1)) |
paul@31 | 246 | rule = Rule(length) |
paul@43 | 247 | self.add_node(region, rule) |
paul@43 | 248 | self.new_block(region) |
paul@31 | 249 | |
paul@37 | 250 | def parse_section(self, region): |
paul@31 | 251 | |
paul@31 | 252 | "Handle the start of a new section within 'region'." |
paul@31 | 253 | |
paul@31 | 254 | # Parse the section and start a new block after the section. |
paul@31 | 255 | |
paul@37 | 256 | indent = len(self.read_match(2)) |
paul@37 | 257 | level = len(self.read_match(3)) |
paul@43 | 258 | self.add_node(region, self.parse_region(level, indent)) |
paul@43 | 259 | self.new_block(region) |
paul@31 | 260 | |
paul@37 | 261 | def parse_section_end(self, region): |
paul@31 | 262 | |
paul@31 | 263 | "Handle the end of a new section within 'region'." |
paul@31 | 264 | |
paul@37 | 265 | feature = self.read_match() |
paul@31 | 266 | if region.have_end(feature): |
paul@31 | 267 | raise StopIteration |
paul@31 | 268 | else: |
paul@31 | 269 | region.append_inline(Text(feature)) |
paul@31 | 270 | |
paul@37 | 271 | def parse_table_attrs(self, cell): |
paul@31 | 272 | |
paul@31 | 273 | "Handle the start of table attributes within 'cell'." |
paul@31 | 274 | |
paul@31 | 275 | attrs = TableAttrs([]) |
paul@37 | 276 | self.parse_region_details(attrs, self.table_pattern_names) |
paul@31 | 277 | |
paul@31 | 278 | # Test the validity of the attributes. |
paul@31 | 279 | |
paul@31 | 280 | last = None |
paul@31 | 281 | |
paul@31 | 282 | for node in attrs.nodes: |
paul@31 | 283 | |
paul@31 | 284 | # Text separator nodes must be whitespace. |
paul@31 | 285 | |
paul@31 | 286 | if isinstance(node, Text): |
paul@31 | 287 | if node.s.strip(): |
paul@31 | 288 | break |
paul@31 | 289 | |
paul@31 | 290 | # Named attributes must be preceded by space if not the first. |
paul@31 | 291 | |
paul@31 | 292 | elif last and not node.concise and not isinstance(last, Text): |
paul@31 | 293 | break |
paul@31 | 294 | |
paul@31 | 295 | last = node |
paul@31 | 296 | |
paul@31 | 297 | # All nodes were valid: preserve the collection. |
paul@31 | 298 | |
paul@31 | 299 | else: |
paul@31 | 300 | cell.attrs = attrs |
paul@18 | 301 | return |
paul@18 | 302 | |
paul@31 | 303 | # Invalid nodes were found: serialise the attributes as text. |
paul@18 | 304 | |
paul@31 | 305 | cell.append_inline(Text(serialise(attrs))) |
paul@25 | 306 | |
paul@37 | 307 | def parse_table_row(self, region): |
paul@9 | 308 | |
paul@31 | 309 | "Handle the start of a table row within 'region'." |
paul@9 | 310 | |
paul@36 | 311 | # Identify any active table. |
paul@36 | 312 | |
paul@36 | 313 | table = region.node(-2) |
paul@36 | 314 | block = region.node(-1) |
paul@36 | 315 | |
paul@36 | 316 | if not (isinstance(table, Table) and block.empty()): |
paul@36 | 317 | new_table = table = Table([]) |
paul@36 | 318 | else: |
paul@36 | 319 | new_table = None |
paul@36 | 320 | |
paul@31 | 321 | row = TableRow([]) |
paul@9 | 322 | |
paul@31 | 323 | while True: |
paul@31 | 324 | cell = TableCell([]) |
paul@37 | 325 | self.parse_region_details(cell, self.table_region_pattern_names) |
paul@9 | 326 | |
paul@31 | 327 | # Handle the end of the row. |
paul@12 | 328 | |
paul@37 | 329 | if self.read_matching() == "tableend": |
paul@37 | 330 | trailing = self.read_match() |
paul@2 | 331 | |
paul@31 | 332 | # If the cell was started but not finished, convert the row into text. |
paul@2 | 333 | |
paul@31 | 334 | if not row.nodes or not cell.empty(): |
paul@31 | 335 | for node in row.nodes: |
paul@31 | 336 | region.append_inline(Text(serialise(node))) |
paul@31 | 337 | region.append_inline(Text(serialise(cell))) |
paul@31 | 338 | region.append_inline(Text(trailing)) |
paul@17 | 339 | |
paul@43 | 340 | self.new_block(region) |
paul@31 | 341 | return |
paul@29 | 342 | |
paul@31 | 343 | # Append the final cell, if not empty. |
paul@29 | 344 | |
paul@31 | 345 | else: |
paul@31 | 346 | row.trailing = trailing |
paul@29 | 347 | |
paul@31 | 348 | if not cell.empty(): |
paul@31 | 349 | row.append(cell) |
paul@31 | 350 | break |
paul@24 | 351 | |
paul@31 | 352 | # A cell separator has been found. |
paul@24 | 353 | |
paul@31 | 354 | row.append(cell) |
paul@24 | 355 | |
paul@36 | 356 | # Add the row to the table and any new table to the region. |
paul@36 | 357 | |
paul@36 | 358 | table.add(row) |
paul@36 | 359 | if new_table: |
paul@43 | 360 | self.add_node(region, new_table) |
paul@36 | 361 | |
paul@43 | 362 | self.new_block(region) |
paul@24 | 363 | |
paul@37 | 364 | def parse_valign(self, attrs): |
paul@24 | 365 | |
paul@31 | 366 | "Handle vertical alignment within 'attrs'." |
paul@24 | 367 | |
paul@37 | 368 | value = self.read_match() |
paul@31 | 369 | attr = TableAttr("valign", value == "^" and "top" or "bottom", True) |
paul@31 | 370 | attrs.append(attr) |
paul@25 | 371 | |
paul@30 | 372 | |
paul@30 | 373 | |
paul@31 | 374 | # Inline formatting handlers. |
paul@31 | 375 | |
paul@37 | 376 | def parse_inline(self, region, cls, pattern_name): |
paul@23 | 377 | |
paul@31 | 378 | "Handle an inline region." |
paul@31 | 379 | |
paul@31 | 380 | span = cls([]) |
paul@37 | 381 | self.parse_region_details(span, self.inline_patterns_for(pattern_name)) |
paul@31 | 382 | region.append_inline(span) |
paul@20 | 383 | |
paul@37 | 384 | def parse_larger(self, region): |
paul@37 | 385 | self.parse_inline(region, Larger, "larger") |
paul@31 | 386 | |
paul@37 | 387 | def parse_monospace(self, region): |
paul@37 | 388 | self.parse_inline(region, Monospace, "monospace") |
paul@20 | 389 | |
paul@37 | 390 | def parse_smaller(self, region): |
paul@37 | 391 | self.parse_inline(region, Smaller, "smaller") |
paul@31 | 392 | |
paul@37 | 393 | def parse_sub(self, region): |
paul@37 | 394 | self.parse_inline(region, Subscript, "sub") |
paul@31 | 395 | |
paul@37 | 396 | def parse_super(self, region): |
paul@37 | 397 | self.parse_inline(region, Superscript, "super") |
paul@20 | 398 | |
paul@37 | 399 | def parse_underline(self, region): |
paul@37 | 400 | self.parse_inline(region, Underline, "underline") |
paul@31 | 401 | |
paul@31 | 402 | |
paul@19 | 403 | |
paul@31 | 404 | # Table attribute handlers. |
paul@31 | 405 | |
paul@37 | 406 | def parse_table_attr(self, attrs, pattern_name): |
paul@31 | 407 | |
paul@31 | 408 | "Handle a table attribute." |
paul@25 | 409 | |
paul@37 | 410 | attrs.append(TableAttr(pattern_name, self.read_match(), True)) |
paul@37 | 411 | |
paul@37 | 412 | def parse_colour(self, cell): |
paul@37 | 413 | self.parse_table_attr(cell, "colour") |
paul@37 | 414 | |
paul@37 | 415 | def parse_colspan(self, cell): |
paul@37 | 416 | self.parse_table_attr(cell, "colspan") |
paul@37 | 417 | |
paul@37 | 418 | def parse_rowspan(self, cell): |
paul@37 | 419 | self.parse_table_attr(cell, "rowspan") |
paul@37 | 420 | |
paul@37 | 421 | def parse_width(self, cell): |
paul@37 | 422 | self.parse_table_attr(cell, "width") |
paul@37 | 423 | |
paul@37 | 424 | |
paul@37 | 425 | |
paul@37 | 426 | # Regular expressions. |
paul@37 | 427 | |
paul@37 | 428 | syntax = { |
paul@37 | 429 | # Page regions: |
paul@37 | 430 | "regionstart" : r"((^\N*)([{]{3,}))", # {{{... |
paul@37 | 431 | "regionend" : r"^\N*([}]{3,})", # }}}... |
paul@37 | 432 | "header" : r"#!(.*?)\n", # #! char-excl-nl |
paul@25 | 433 | |
paul@37 | 434 | # Region contents: |
paul@37 | 435 | # Line-oriented patterns: |
paul@37 | 436 | # blank line |
paul@37 | 437 | "break" : r"^(\s*?)\n", |
paul@37 | 438 | # ws... expecting text :: |
paul@37 | 439 | "defterm" : r"^(\N+)(?=.+?::)", |
paul@37 | 440 | # ws... expecting :: ws... |
paul@37 | 441 | "defterm_empty" : r"^(\N+)(?=::\s+)", |
paul@37 | 442 | # [ws...] =... ws... expecting headingend |
paul@37 | 443 | "heading" : r"^(\N*)(?P<x>=+)(\s+)(?=.*?\N+(?P=x)\N*$)", |
paul@37 | 444 | # ws... list-item [ws...] |
paul@37 | 445 | "listitem" : r"^(\N+)(\*)(\s*)", |
paul@37 | 446 | # ws... number-item ws... |
paul@37 | 447 | "listitem_num" : r"^(\N+)(\d+\.)(\s+)", |
paul@37 | 448 | # ws... alpha-item ws... |
paul@37 | 449 | "listitem_alpha": r"^(\N+)([aA]\.)(\s+)", |
paul@37 | 450 | # ws... roman-item ws... |
paul@37 | 451 | "listitem_roman": r"^(\N+)([iI]\.)(\s+)", |
paul@37 | 452 | # ws... dot-item [ws...] |
paul@37 | 453 | "listitem_dot" : r"^(\N+)(\.)(\s*)", |
paul@37 | 454 | # || |
paul@37 | 455 | "tablerow" : r"^\|\|", |
paul@37 | 456 | |
paul@37 | 457 | # Region contents: |
paul@37 | 458 | # Inline patterns: |
paul@37 | 459 | "fontstyle" : r"('{2,6})", |
paul@37 | 460 | "larger" : r"~\+", |
paul@37 | 461 | "monospace" : r"`", |
paul@37 | 462 | "rule" : r"(-----*)", # ----... |
paul@37 | 463 | "smaller" : r"~-", |
paul@37 | 464 | "sub" : r",,", |
paul@37 | 465 | "super" : r"\^", |
paul@37 | 466 | "underline" : r"__", |
paul@25 | 467 | |
paul@37 | 468 | # Inline contents: |
paul@37 | 469 | "largerend" : r"\+~", |
paul@37 | 470 | "monospaceend" : r"`", |
paul@37 | 471 | "smallerend" : r"-~", |
paul@37 | 472 | "subend" : r",,", |
paul@37 | 473 | "superend" : r"\^", |
paul@37 | 474 | "underlineend" : r"__", |
paul@37 | 475 | |
paul@37 | 476 | # Heading contents: |
paul@37 | 477 | "headingend" : r"(\N+)(=+)(\N*$)", # ws... =... [ws...] nl |
paul@37 | 478 | |
paul@37 | 479 | # List contents: |
paul@37 | 480 | "deftermend" : r"::(\s*?\n)", |
paul@37 | 481 | "deftermsep" : r"::(\s+)", |
paul@37 | 482 | "listitemend" : r"^", # next line |
paul@37 | 483 | |
paul@37 | 484 | # Table contents: |
paul@37 | 485 | "tableattrs" : r"<", |
paul@37 | 486 | "tablecell" : r"\|\|", |
paul@37 | 487 | "tableend" : r"(\s*?)^", # [ws...] next line |
paul@25 | 488 | |
paul@37 | 489 | # Table attributes: |
paul@37 | 490 | "tableattrsend" : r">", |
paul@37 | 491 | "halign" : r"([(:)])", |
paul@37 | 492 | "valign" : r"([v^])", |
paul@37 | 493 | "colour" : r"(\#[0-9A-F]{6})", |
paul@37 | 494 | "colspan" : r"-(\d+)", |
paul@37 | 495 | "rowspan" : r"\|(\d+)", |
paul@37 | 496 | "width" : r"(\d+%)", |
paul@37 | 497 | "attrname" : r"((?![-\d])[-\w]+)", # not-dash-or-digit dash-or-word-char... |
paul@37 | 498 | "attrvalue" : r"""=(?P<x>['"])(.*?)(?P=x)""", |
paul@37 | 499 | } |
paul@37 | 500 | |
paul@37 | 501 | patterns = get_patterns(syntax) |
paul@37 | 502 | |
paul@37 | 503 | |
paul@37 | 504 | |
paul@37 | 505 | # Pattern details. |
paul@31 | 506 | |
paul@37 | 507 | table_pattern_names = [ |
paul@37 | 508 | "attrname", "colour", "colspan", "halign", "rowspan", "tableattrsend", |
paul@37 | 509 | "valign", "width" |
paul@37 | 510 | ] |
paul@37 | 511 | |
paul@37 | 512 | inline_pattern_names = [ |
paul@37 | 513 | "fontstyle", "larger", "monospace", "smaller", "sub", "super", "underline", |
paul@37 | 514 | ] |
paul@37 | 515 | |
paul@43 | 516 | list_pattern_names = [ |
paul@43 | 517 | "listitem", "listitem_alpha", "listitem_dot", "listitem_num", |
paul@43 | 518 | "listitem_roman", |
paul@43 | 519 | ] |
paul@43 | 520 | |
paul@37 | 521 | listitem_pattern_names = inline_pattern_names + ["listitemend"] |
paul@37 | 522 | |
paul@43 | 523 | region_pattern_names = inline_pattern_names + list_pattern_names + [ |
paul@43 | 524 | "break", "heading", "defterm", "defterm_empty", |
paul@37 | 525 | "regionstart", "regionend", "rule", "tablerow", |
paul@37 | 526 | ] |
paul@37 | 527 | |
paul@37 | 528 | table_region_pattern_names = inline_pattern_names + [ |
paul@37 | 529 | "tableattrs", "tablecell", "tableend" |
paul@37 | 530 | ] |
paul@37 | 531 | |
paul@37 | 532 | def inline_patterns_for(self, name): |
paul@37 | 533 | names = self.inline_pattern_names[:] |
paul@37 | 534 | names[names.index(name)] = "%send" % name |
paul@37 | 535 | return names |
paul@25 | 536 | |
paul@30 | 537 | |
paul@30 | 538 | |
paul@32 | 539 | # Pattern handlers. |
paul@31 | 540 | |
paul@32 | 541 | end_region = ParserBase.end_region |
paul@31 | 542 | |
paul@31 | 543 | handlers = { |
paul@31 | 544 | None : end_region, |
paul@31 | 545 | "attrname" : parse_attrname, |
paul@31 | 546 | "break" : parse_break, |
paul@31 | 547 | "colour" : parse_colour, |
paul@31 | 548 | "colspan" : parse_colspan, |
paul@31 | 549 | "defterm" : parse_defterm, |
paul@31 | 550 | "defterm_empty" : parse_defterm_empty, |
paul@31 | 551 | "deftermend" : end_region, |
paul@31 | 552 | "deftermsep" : end_region, |
paul@31 | 553 | "fontstyle" : parse_fontstyle, |
paul@31 | 554 | "halign" : parse_halign, |
paul@31 | 555 | "heading" : parse_heading, |
paul@31 | 556 | "headingend" : parse_heading_end, |
paul@31 | 557 | "larger" : parse_larger, |
paul@31 | 558 | "largerend" : end_region, |
paul@31 | 559 | "listitemend" : end_region, |
paul@31 | 560 | "listitem" : parse_listitem, |
paul@31 | 561 | "listitem_alpha" : parse_listitem, |
paul@31 | 562 | "listitem_dot" : parse_listitem, |
paul@31 | 563 | "listitem_num" : parse_listitem, |
paul@31 | 564 | "listitem_roman" : parse_listitem, |
paul@31 | 565 | "monospace" : parse_monospace, |
paul@31 | 566 | "monospaceend" : end_region, |
paul@31 | 567 | "regionstart" : parse_section, |
paul@31 | 568 | "regionend" : parse_section_end, |
paul@31 | 569 | "rowspan" : parse_rowspan, |
paul@31 | 570 | "rule" : parse_rule, |
paul@31 | 571 | "smaller" : parse_smaller, |
paul@31 | 572 | "smallerend" : end_region, |
paul@31 | 573 | "sub" : parse_sub, |
paul@31 | 574 | "subend" : end_region, |
paul@31 | 575 | "super" : parse_super, |
paul@31 | 576 | "superend" : end_region, |
paul@31 | 577 | "tableattrs" : parse_table_attrs, |
paul@31 | 578 | "tableattrsend" : end_region, |
paul@31 | 579 | "tablerow" : parse_table_row, |
paul@31 | 580 | "tablecell" : end_region, |
paul@31 | 581 | "tableend" : end_region, |
paul@31 | 582 | "underline" : parse_underline, |
paul@31 | 583 | "underlineend" : end_region, |
paul@31 | 584 | "valign" : parse_valign, |
paul@31 | 585 | "width" : parse_width, |
paul@31 | 586 | } |
paul@2 | 587 | |
paul@42 | 588 | parser = MoinParser |
paul@0 | 589 | |
paul@0 | 590 | # vim: tabstop=4 expandtab shiftwidth=4 |