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