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