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@28 | 22 | from moinformat.serialisers import serialise |
paul@18 | 23 | from moinformat.tree import Block, Break, DefItem, DefTerm, FontStyle, Heading, \ |
paul@23 | 24 | Larger, ListItem, Monospace, Region, Rule, Smaller, \ |
paul@25 | 25 | Subscript, Superscript, TableAttr, TableAttrs, \ |
paul@25 | 26 | TableCell, TableRow, Text, Underline |
paul@0 | 27 | import re |
paul@0 | 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@25 | 104 | # Define pattern details. |
paul@25 | 105 | |
paul@25 | 106 | table_pattern_names = ["attrname", "colour", "colspan", "halign", "rowspan", "tableattrsend", "valign", "width"] |
paul@23 | 107 | |
paul@23 | 108 | inline_pattern_names = ["fontstyle", "larger", "monospace", "smaller", "sub", "super", "underline"] |
paul@23 | 109 | |
paul@23 | 110 | def inline_patterns_for(name): |
paul@23 | 111 | names = inline_pattern_names[:] |
paul@23 | 112 | names[names.index(name)] = "%send" % name |
paul@23 | 113 | return names |
paul@23 | 114 | |
paul@0 | 115 | # Define patterns for the regular expressions. |
paul@0 | 116 | |
paul@0 | 117 | patterns = {} |
paul@13 | 118 | for name, value in syntax.items(): |
paul@13 | 119 | patterns[name] = re.compile(value, re.UNICODE | re.MULTILINE) |
paul@0 | 120 | |
paul@0 | 121 | |
paul@0 | 122 | |
paul@2 | 123 | # Tokenising functions. |
paul@2 | 124 | |
paul@2 | 125 | class TokenStream: |
paul@2 | 126 | |
paul@2 | 127 | "A stream of tokens taken from a string." |
paul@2 | 128 | |
paul@2 | 129 | def __init__(self, s): |
paul@2 | 130 | self.s = s |
paul@2 | 131 | self.pos = 0 |
paul@2 | 132 | self.match = None |
paul@2 | 133 | self.matching = None |
paul@2 | 134 | |
paul@18 | 135 | def rewind(self, length): |
paul@18 | 136 | |
paul@18 | 137 | "Rewind in the string by 'length'." |
paul@18 | 138 | |
paul@18 | 139 | self.pos -= min(length, self.pos) |
paul@18 | 140 | |
paul@2 | 141 | def read_until(self, pattern_names, remaining=True): |
paul@2 | 142 | |
paul@2 | 143 | """ |
paul@2 | 144 | Find the first match for the given 'pattern_names'. Return the text |
paul@2 | 145 | preceding any match, the remaining text if no match was found, or None |
paul@2 | 146 | if no match was found and 'remaining' is given as a false value. |
paul@2 | 147 | """ |
paul@2 | 148 | |
paul@2 | 149 | first = None |
paul@2 | 150 | self.matching = None |
paul@2 | 151 | |
paul@2 | 152 | # Find the first matching pattern. |
paul@2 | 153 | |
paul@2 | 154 | for pattern_name in pattern_names: |
paul@2 | 155 | match = patterns[pattern_name].search(self.s, self.pos) |
paul@2 | 156 | if match: |
paul@2 | 157 | start, end = match.span() |
paul@2 | 158 | if self.matching is None or start < first: |
paul@2 | 159 | first = start |
paul@2 | 160 | self.matching = pattern_name |
paul@2 | 161 | self.match = match |
paul@2 | 162 | |
paul@2 | 163 | if self.matching is None: |
paul@2 | 164 | if remaining: |
paul@2 | 165 | return self.s[self.pos:] |
paul@2 | 166 | else: |
paul@2 | 167 | return None |
paul@2 | 168 | else: |
paul@2 | 169 | return self.s[self.pos:first] |
paul@2 | 170 | |
paul@10 | 171 | def read_match(self, group=1): |
paul@2 | 172 | |
paul@10 | 173 | """ |
paul@10 | 174 | Return the matched text, updating the position in the stream. If 'group' |
paul@10 | 175 | is specified, the indicated group in a match will be returned. |
paul@10 | 176 | Typically, group 1 should contain all pertinent data, but groups defined |
paul@10 | 177 | within group 1 can provide sections of the data. |
paul@10 | 178 | """ |
paul@2 | 179 | |
paul@2 | 180 | if self.match: |
paul@2 | 181 | _start, self.pos = self.match.span() |
paul@9 | 182 | try: |
paul@10 | 183 | return self.match.group(group) |
paul@9 | 184 | except IndexError: |
paul@9 | 185 | return "" |
paul@2 | 186 | else: |
paul@2 | 187 | self.pos = len(self.s) |
paul@2 | 188 | return None |
paul@2 | 189 | |
paul@2 | 190 | |
paul@2 | 191 | |
paul@0 | 192 | # Parser functions. |
paul@0 | 193 | |
paul@0 | 194 | def parse_page(s): |
paul@0 | 195 | |
paul@0 | 196 | """ |
paul@0 | 197 | Parse page text 's'. Pages consist of regions delimited by markers. |
paul@0 | 198 | """ |
paul@0 | 199 | |
paul@6 | 200 | return parse_region(TokenStream(s)) |
paul@1 | 201 | |
paul@10 | 202 | def parse_region(items, level=0, indent=0): |
paul@1 | 203 | |
paul@6 | 204 | """ |
paul@10 | 205 | Parse the data provided by 'items' to populate a region with the given |
paul@10 | 206 | 'level' at the given 'indent'. |
paul@6 | 207 | """ |
paul@0 | 208 | |
paul@10 | 209 | region = Region([], level, indent) |
paul@0 | 210 | |
paul@2 | 211 | # Parse section headers. |
paul@2 | 212 | |
paul@2 | 213 | parse_region_header(items, region) |
paul@2 | 214 | |
paul@8 | 215 | # Parse section body. |
paul@8 | 216 | |
paul@2 | 217 | if region.is_transparent(): |
paul@2 | 218 | parse_region_wiki(items, region) |
paul@2 | 219 | else: |
paul@2 | 220 | parse_region_opaque(items, region) |
paul@2 | 221 | |
paul@6 | 222 | return region |
paul@6 | 223 | |
paul@6 | 224 | def parse_region_header(items, region): |
paul@6 | 225 | |
paul@6 | 226 | """ |
paul@6 | 227 | Parse the region header from the 'items', setting it for the given 'region'. |
paul@6 | 228 | """ |
paul@6 | 229 | |
paul@6 | 230 | if items.read_until(["header"], False) == "": # None means no header |
paul@6 | 231 | region.type = items.read_match() |
paul@6 | 232 | |
paul@2 | 233 | def parse_region_wiki(items, region): |
paul@2 | 234 | |
paul@2 | 235 | "Parse the data provided by 'items' to populate a wiki 'region'." |
paul@0 | 236 | |
paul@8 | 237 | new_block(region) |
paul@20 | 238 | parse_region_details(items, region, inline_pattern_names + [ |
paul@16 | 239 | "break", "heading", |
paul@16 | 240 | "defterm", "defterm_empty", |
paul@16 | 241 | "listitem", "listitem_alpha", "listitem_dot", "listitem_num", |
paul@16 | 242 | "listitem_roman", |
paul@17 | 243 | "regionstart", "regionend", |
paul@17 | 244 | "rule", |
paul@24 | 245 | "tablerow", |
paul@18 | 246 | ]) |
paul@0 | 247 | |
paul@8 | 248 | def parse_region_opaque(items, region): |
paul@1 | 249 | |
paul@8 | 250 | "Parse the data provided by 'items' to populate an opaque 'region'." |
paul@8 | 251 | |
paul@8 | 252 | parse_region_details(items, region, ["regionend"]) |
paul@1 | 253 | |
paul@8 | 254 | def parse_region_details(items, region, pattern_names): |
paul@0 | 255 | |
paul@8 | 256 | "Parse 'items' within 'region' searching using 'pattern_names'." |
paul@0 | 257 | |
paul@8 | 258 | try: |
paul@8 | 259 | while True: |
paul@0 | 260 | |
paul@8 | 261 | # Obtain text before any marker or the end of the input. |
paul@2 | 262 | |
paul@8 | 263 | preceding = items.read_until(pattern_names) |
paul@8 | 264 | if preceding: |
paul@17 | 265 | region.append_inline(Text(preceding)) |
paul@2 | 266 | |
paul@8 | 267 | # End of input. |
paul@0 | 268 | |
paul@8 | 269 | if not items.matching: |
paul@8 | 270 | break |
paul@8 | 271 | |
paul@8 | 272 | # Obtain any feature. |
paul@2 | 273 | |
paul@8 | 274 | feature = items.read_match() |
paul@8 | 275 | handler = handlers.get(items.matching) |
paul@2 | 276 | |
paul@8 | 277 | # Handle each feature or add text to the region. |
paul@2 | 278 | |
paul@8 | 279 | if handler: |
paul@8 | 280 | handler(items, region) |
paul@8 | 281 | else: |
paul@17 | 282 | region.append_inline(Text(feature)) |
paul@2 | 283 | |
paul@8 | 284 | except StopIteration: |
paul@8 | 285 | pass |
paul@2 | 286 | |
paul@2 | 287 | region.normalise() |
paul@0 | 288 | |
paul@8 | 289 | def end_region(items, region): |
paul@7 | 290 | |
paul@8 | 291 | "End the parsing of 'region'." |
paul@7 | 292 | |
paul@8 | 293 | raise StopIteration |
paul@7 | 294 | |
paul@25 | 295 | def parse_attrname(items, attrs): |
paul@25 | 296 | |
paul@25 | 297 | "Handle an attribute name within 'attrs'." |
paul@25 | 298 | |
paul@25 | 299 | name = items.read_match() |
paul@25 | 300 | attr = TableAttr(name) |
paul@25 | 301 | |
paul@25 | 302 | preceding = items.read_until(["attrvalue"], False) |
paul@25 | 303 | if preceding == "": |
paul@27 | 304 | attr.quote = items.read_match(1) |
paul@25 | 305 | attr.value = items.read_match(2) |
paul@25 | 306 | |
paul@25 | 307 | attrs.append(attr) |
paul@25 | 308 | |
paul@8 | 309 | def parse_break(items, region): |
paul@8 | 310 | |
paul@8 | 311 | "Handle a paragraph break within 'region'." |
paul@7 | 312 | |
paul@15 | 313 | region.add(Break()) |
paul@8 | 314 | new_block(region) |
paul@2 | 315 | |
paul@16 | 316 | def parse_defitem(items, region, extra=""): |
paul@16 | 317 | |
paul@16 | 318 | "Handle a definition item within 'region'." |
paul@16 | 319 | |
paul@16 | 320 | pad = items.read_match(1) |
paul@16 | 321 | item = DefItem([], pad, extra) |
paul@16 | 322 | parse_region_details(items, item, ["listitemend"]) |
paul@21 | 323 | region.add(item) |
paul@16 | 324 | new_block(region) |
paul@16 | 325 | |
paul@16 | 326 | def parse_defterm(items, region): |
paul@16 | 327 | |
paul@16 | 328 | "Handle a definition term within 'region'." |
paul@16 | 329 | |
paul@16 | 330 | pad = items.read_match(1) |
paul@16 | 331 | term = DefTerm([], pad) |
paul@16 | 332 | parse_region_details(items, term, ["deftermend", "deftermsep"]) |
paul@21 | 333 | region.add(term) |
paul@16 | 334 | if items.matching == "deftermsep": |
paul@16 | 335 | parse_defitem(items, region) |
paul@16 | 336 | |
paul@16 | 337 | def parse_defterm_empty(items, region): |
paul@16 | 338 | |
paul@16 | 339 | "Handle an empty definition term within 'region'." |
paul@16 | 340 | |
paul@16 | 341 | extra = items.read_match(1) |
paul@16 | 342 | parse_region_details(items, region, ["deftermsep"]) |
paul@16 | 343 | parse_defitem(items, region, extra) |
paul@16 | 344 | |
paul@18 | 345 | def parse_fontstyle(items, region): |
paul@18 | 346 | |
paul@18 | 347 | "Handle emphasis and strong styles." |
paul@18 | 348 | |
paul@18 | 349 | n = len(items.read_match(1)) |
paul@18 | 350 | |
paul@18 | 351 | # Handle endings. |
paul@18 | 352 | |
paul@18 | 353 | if isinstance(region, FontStyle): |
paul@18 | 354 | emphasis = n in (2, 4, 5) |
paul@18 | 355 | strong = n in (3, 5, 6) |
paul@18 | 356 | active = True |
paul@17 | 357 | |
paul@18 | 358 | if region.emphasis and emphasis: |
paul@18 | 359 | active = region.close_emphasis() |
paul@18 | 360 | n -= 2 |
paul@18 | 361 | if region.strong and strong: |
paul@18 | 362 | active = region.close_strong() |
paul@18 | 363 | n -= 3 |
paul@18 | 364 | |
paul@18 | 365 | if not active: |
paul@18 | 366 | if n: |
paul@18 | 367 | items.rewind(n) |
paul@18 | 368 | raise StopIteration |
paul@17 | 369 | |
paul@18 | 370 | elif not n: |
paul@18 | 371 | return |
paul@18 | 372 | |
paul@18 | 373 | # Handle new styles. |
paul@18 | 374 | |
paul@18 | 375 | emphasis = n in (2, 4, 5) |
paul@18 | 376 | strong = n in (3, 5, 6) |
paul@18 | 377 | double = n in (4, 6) |
paul@18 | 378 | |
paul@18 | 379 | span = FontStyle([], emphasis, strong) |
paul@18 | 380 | if not double: |
paul@20 | 381 | parse_region_details(items, span, inline_pattern_names) |
paul@17 | 382 | region.append_inline(span) |
paul@16 | 383 | |
paul@25 | 384 | def parse_halign(items, attrs): |
paul@25 | 385 | |
paul@25 | 386 | "Handle horizontal alignment within 'attrs'." |
paul@25 | 387 | |
paul@25 | 388 | value = items.read_match() |
paul@25 | 389 | attr = TableAttr("halign", value == "(" and "left" or value == ")" and "right" or "center", True) |
paul@25 | 390 | attrs.append(attr) |
paul@25 | 391 | |
paul@13 | 392 | def parse_heading(items, region): |
paul@13 | 393 | |
paul@13 | 394 | "Handle a heading." |
paul@9 | 395 | |
paul@13 | 396 | start_extra = items.read_match(1) |
paul@13 | 397 | level = len(items.read_match(2)) |
paul@13 | 398 | start_pad = items.read_match(3) |
paul@13 | 399 | heading = Heading([], level, start_extra, start_pad) |
paul@20 | 400 | parse_region_details(items, heading, ["headingend"] + inline_pattern_names) |
paul@21 | 401 | region.add(heading) |
paul@13 | 402 | new_block(region) |
paul@9 | 403 | |
paul@13 | 404 | def parse_heading_end(items, heading): |
paul@13 | 405 | |
paul@13 | 406 | "Handle the end of a heading." |
paul@13 | 407 | |
paul@13 | 408 | level = len(items.read_match(2)) |
paul@13 | 409 | if heading.level == level: |
paul@13 | 410 | heading.end_pad = items.read_match(1) |
paul@13 | 411 | heading.end_extra = items.read_match(3) |
paul@13 | 412 | raise StopIteration |
paul@9 | 413 | |
paul@9 | 414 | def parse_listitem(items, region): |
paul@9 | 415 | |
paul@9 | 416 | "Handle a list item marker within 'region'." |
paul@9 | 417 | |
paul@14 | 418 | indent = len(items.read_match(1)) |
paul@14 | 419 | marker = items.read_match(2) |
paul@14 | 420 | space = items.read_match(3) |
paul@14 | 421 | item = ListItem([], indent, marker, space) |
paul@9 | 422 | parse_region_details(items, item, ["listitemend"]) |
paul@21 | 423 | region.add(item) |
paul@9 | 424 | new_block(region) |
paul@9 | 425 | |
paul@12 | 426 | def parse_rule(items, region): |
paul@12 | 427 | |
paul@12 | 428 | "Handle a horizontal rule within 'region'." |
paul@12 | 429 | |
paul@12 | 430 | length = len(items.read_match(1)) |
paul@12 | 431 | rule = Rule(length) |
paul@21 | 432 | region.add(rule) |
paul@12 | 433 | new_block(region) |
paul@12 | 434 | |
paul@8 | 435 | def parse_section(items, region): |
paul@2 | 436 | |
paul@8 | 437 | "Handle the start of a new section within 'region'." |
paul@2 | 438 | |
paul@8 | 439 | # Parse the section and start a new block after the section. |
paul@2 | 440 | |
paul@10 | 441 | indent = len(items.read_match(2)) |
paul@10 | 442 | level = len(items.read_match(3)) |
paul@21 | 443 | region.add(parse_region(items, level, indent)) |
paul@8 | 444 | new_block(region) |
paul@2 | 445 | |
paul@8 | 446 | def parse_section_end(items, region): |
paul@2 | 447 | |
paul@8 | 448 | "Handle the end of a new section within 'region'." |
paul@1 | 449 | |
paul@8 | 450 | feature = items.read_match() |
paul@8 | 451 | if region.have_end(feature): |
paul@8 | 452 | raise StopIteration |
paul@8 | 453 | else: |
paul@17 | 454 | region.append_inline(Text(feature)) |
paul@17 | 455 | |
paul@25 | 456 | def parse_table_attrs(items, cell): |
paul@25 | 457 | |
paul@25 | 458 | "Handle the start of table attributes within 'cell'." |
paul@25 | 459 | |
paul@25 | 460 | attrs = TableAttrs([]) |
paul@25 | 461 | parse_region_details(items, attrs, table_pattern_names) |
paul@25 | 462 | cell.attrs = attrs |
paul@25 | 463 | |
paul@24 | 464 | def parse_table_row(items, region): |
paul@24 | 465 | |
paul@24 | 466 | "Handle the start of a table row within 'region'." |
paul@24 | 467 | |
paul@24 | 468 | row = TableRow([]) |
paul@24 | 469 | |
paul@24 | 470 | while True: |
paul@24 | 471 | cell = TableCell([]) |
paul@25 | 472 | parse_region_details(items, cell, ["tableattrs", "tablecell", "tableend"]) |
paul@24 | 473 | |
paul@24 | 474 | # Handle the end of the row. |
paul@24 | 475 | |
paul@24 | 476 | if items.matching == "tableend": |
paul@24 | 477 | trailing = items.read_match() |
paul@24 | 478 | |
paul@24 | 479 | # If the cell was started but not finished, convert the row into text. |
paul@24 | 480 | |
paul@24 | 481 | if not row.nodes or not cell.empty(): |
paul@24 | 482 | for node in row.nodes: |
paul@28 | 483 | region.append_inline(Text(serialise(node))) |
paul@28 | 484 | region.append_inline(Text(serialise(cell))) |
paul@24 | 485 | region.append_inline(Text(trailing)) |
paul@24 | 486 | |
paul@24 | 487 | new_block(region) |
paul@24 | 488 | return |
paul@24 | 489 | |
paul@24 | 490 | # Append the final cell, if not empty. |
paul@24 | 491 | |
paul@24 | 492 | else: |
paul@24 | 493 | row.trailing = trailing |
paul@24 | 494 | |
paul@24 | 495 | if not cell.empty(): |
paul@24 | 496 | row.append(cell) |
paul@24 | 497 | break |
paul@24 | 498 | |
paul@25 | 499 | # A cell separator has been found. |
paul@25 | 500 | |
paul@24 | 501 | row.append(cell) |
paul@24 | 502 | |
paul@24 | 503 | region.add(row) |
paul@24 | 504 | new_block(region) |
paul@24 | 505 | |
paul@25 | 506 | def parse_valign(items, attrs): |
paul@25 | 507 | |
paul@25 | 508 | "Handle vertical alignment within 'attrs'." |
paul@25 | 509 | |
paul@25 | 510 | value = items.read_match() |
paul@25 | 511 | attr = TableAttr("valign", value == "^" and "top" or "bottom", True) |
paul@25 | 512 | attrs.append(attr) |
paul@25 | 513 | |
paul@23 | 514 | # Inline formatting handlers. |
paul@23 | 515 | |
paul@23 | 516 | def parse_inline(items, region, cls, pattern_name): |
paul@20 | 517 | |
paul@23 | 518 | "Handle an inline region." |
paul@20 | 519 | |
paul@23 | 520 | span = cls([]) |
paul@23 | 521 | parse_region_details(items, span, inline_patterns_for(pattern_name)) |
paul@20 | 522 | region.append_inline(span) |
paul@20 | 523 | |
paul@23 | 524 | parse_larger = lambda items, region: parse_inline(items, region, Larger, "larger") |
paul@23 | 525 | parse_monospace = lambda items, region: parse_inline(items, region, Monospace, "monospace") |
paul@23 | 526 | parse_smaller = lambda items, region: parse_inline(items, region, Smaller, "smaller") |
paul@23 | 527 | parse_sub = lambda items, region: parse_inline(items, region, Subscript, "sub") |
paul@23 | 528 | parse_super = lambda items, region: parse_inline(items, region, Superscript, "super") |
paul@23 | 529 | parse_underline = lambda items, region: parse_inline(items, region, Underline, "underline") |
paul@19 | 530 | |
paul@25 | 531 | # Table attribute handlers. |
paul@25 | 532 | |
paul@25 | 533 | def parse_table_attr(items, attrs, pattern_name): |
paul@25 | 534 | |
paul@25 | 535 | "Handle a table attribute." |
paul@25 | 536 | |
paul@25 | 537 | value = items.read_match() |
paul@25 | 538 | attrs.append(TableAttr(pattern_name, value, True)) |
paul@25 | 539 | |
paul@25 | 540 | parse_colour = lambda items, cell: parse_table_attr(items, cell, "colour") |
paul@25 | 541 | parse_colspan = lambda items, cell: parse_table_attr(items, cell, "colspan") |
paul@25 | 542 | parse_rowspan = lambda items, cell: parse_table_attr(items, cell, "rowspan") |
paul@25 | 543 | parse_width = lambda items, cell: parse_table_attr(items, cell, "width") |
paul@25 | 544 | |
paul@8 | 545 | # Pattern handlers. |
paul@2 | 546 | |
paul@8 | 547 | handlers = { |
paul@8 | 548 | None : end_region, |
paul@25 | 549 | "attrname" : parse_attrname, |
paul@8 | 550 | "break" : parse_break, |
paul@25 | 551 | "colour" : parse_colour, |
paul@25 | 552 | "colspan" : parse_colspan, |
paul@16 | 553 | "defterm" : parse_defterm, |
paul@16 | 554 | "defterm_empty" : parse_defterm_empty, |
paul@17 | 555 | "deftermend" : end_region, |
paul@17 | 556 | "deftermsep" : end_region, |
paul@18 | 557 | "fontstyle" : parse_fontstyle, |
paul@25 | 558 | "halign" : parse_halign, |
paul@13 | 559 | "heading" : parse_heading, |
paul@13 | 560 | "headingend" : parse_heading_end, |
paul@23 | 561 | "larger" : parse_larger, |
paul@23 | 562 | "largerend" : end_region, |
paul@17 | 563 | "listitemend" : end_region, |
paul@9 | 564 | "listitem" : parse_listitem, |
paul@14 | 565 | "listitem_alpha" : parse_listitem, |
paul@14 | 566 | "listitem_dot" : parse_listitem, |
paul@14 | 567 | "listitem_num" : parse_listitem, |
paul@14 | 568 | "listitem_roman" : parse_listitem, |
paul@18 | 569 | "monospace" : parse_monospace, |
paul@18 | 570 | "monospaceend" : end_region, |
paul@8 | 571 | "regionstart" : parse_section, |
paul@8 | 572 | "regionend" : parse_section_end, |
paul@25 | 573 | "rowspan" : parse_rowspan, |
paul@12 | 574 | "rule" : parse_rule, |
paul@23 | 575 | "smaller" : parse_smaller, |
paul@23 | 576 | "smallerend" : end_region, |
paul@20 | 577 | "sub" : parse_sub, |
paul@20 | 578 | "subend" : end_region, |
paul@20 | 579 | "super" : parse_super, |
paul@20 | 580 | "superend" : end_region, |
paul@25 | 581 | "tableattrs" : parse_table_attrs, |
paul@25 | 582 | "tableattrsend" : end_region, |
paul@24 | 583 | "tablerow" : parse_table_row, |
paul@24 | 584 | "tablecell" : end_region, |
paul@24 | 585 | "tableend" : end_region, |
paul@19 | 586 | "underline" : parse_underline, |
paul@19 | 587 | "underlineend" : end_region, |
paul@25 | 588 | "valign" : parse_valign, |
paul@25 | 589 | "width" : parse_width, |
paul@8 | 590 | } |
paul@2 | 591 | |
paul@6 | 592 | def new_block(region): |
paul@6 | 593 | |
paul@6 | 594 | "Start a new block in 'region'." |
paul@0 | 595 | |
paul@6 | 596 | block = Block([]) |
paul@15 | 597 | region.add(block) |
paul@0 | 598 | |
paul@1 | 599 | |
paul@1 | 600 | |
paul@1 | 601 | # Top-level functions. |
paul@0 | 602 | |
paul@0 | 603 | parse = parse_page |
paul@0 | 604 | |
paul@0 | 605 | # vim: tabstop=4 expandtab shiftwidth=4 |