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