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@18 | 22 | from moinformat.tree import Block, Break, DefItem, DefTerm, FontStyle, Heading, \ |
paul@23 | 23 | Larger, ListItem, Monospace, Region, Rule, Smaller, \ |
paul@23 | 24 | Subscript, Superscript, Text, Underline |
paul@0 | 25 | import re |
paul@0 | 26 | |
paul@0 | 27 | # Regular expressions. |
paul@0 | 28 | |
paul@0 | 29 | syntax = { |
paul@0 | 30 | # Page regions: |
paul@13 | 31 | "regionstart" : r"((^\s*)([{]{3,}))", # {{{... |
paul@13 | 32 | "regionend" : r"^\s*([}]{3,})", # }}}... |
paul@13 | 33 | "header" : r"#!(.*?)\n", # #! char-excl-nl |
paul@0 | 34 | |
paul@0 | 35 | # Region contents: |
paul@13 | 36 | # Line-oriented patterns: |
paul@14 | 37 | # blank line |
paul@14 | 38 | "break" : r"^(\s*?)\n", |
paul@16 | 39 | # ws... expecting text :: |
paul@16 | 40 | "defterm" : r"^(\s+)(?=.+?::)", |
paul@16 | 41 | # ws... expecting :: ws... |
paul@16 | 42 | "defterm_empty" : r"^(\s+)(?=::\s+)", |
paul@14 | 43 | # [ws...] =... ws... expecting headingend |
paul@14 | 44 | "heading" : r"^(\s*)(?P<x>=+)(\s+)(?=.*?\s+(?P=x)\s*\n)", |
paul@16 | 45 | # ws... list-item [ws...] |
paul@14 | 46 | "listitem" : r"^(\s+)(\*)(\s*)", |
paul@16 | 47 | # ws... number-item ws... |
paul@14 | 48 | "listitem_num" : r"^(\s+)(\d+\.)(\s+)", |
paul@16 | 49 | # ws... alpha-item ws... |
paul@14 | 50 | "listitem_alpha": r"^(\s+)([aA]\.)(\s+)", |
paul@16 | 51 | # ws... roman-item ws... |
paul@14 | 52 | "listitem_roman": r"^(\s+)([iI]\.)(\s+)", |
paul@16 | 53 | # ws... dot-item [ws...] |
paul@14 | 54 | "listitem_dot" : r"^(\s+)(\.)(\s*)", |
paul@13 | 55 | |
paul@13 | 56 | # Region contents: |
paul@13 | 57 | # Inline patterns: |
paul@18 | 58 | "fontstyle" : r"('{2,6})", |
paul@23 | 59 | "larger" : r"~\+", |
paul@18 | 60 | "monospace" : r"`", |
paul@13 | 61 | "rule" : r"(-----*)", # ----... |
paul@23 | 62 | "smaller" : r"~-", |
paul@20 | 63 | "sub" : r",,", |
paul@20 | 64 | "super" : r"\^", |
paul@19 | 65 | "underline" : r"__", |
paul@17 | 66 | |
paul@17 | 67 | # Inline contents: |
paul@23 | 68 | "largerend" : r"\+~", |
paul@18 | 69 | "monospaceend" : r"`", |
paul@23 | 70 | "smallerend" : r"-~", |
paul@20 | 71 | "subend" : r",,", |
paul@20 | 72 | "superend" : r"\^", |
paul@19 | 73 | "underlineend" : r"__", |
paul@13 | 74 | |
paul@13 | 75 | # Heading contents: |
paul@13 | 76 | "headingend" : r"(\s+)(=+)(\s*\n)", # ws... =... [ws...] nl |
paul@9 | 77 | |
paul@9 | 78 | # List contents: |
paul@16 | 79 | "deftermend" : r"::(\s*?\n)", |
paul@16 | 80 | "deftermsep" : r"::(\s+)", |
paul@13 | 81 | "listitemend" : r"^", # next line |
paul@0 | 82 | } |
paul@0 | 83 | |
paul@23 | 84 | # Define inline pattern details. |
paul@23 | 85 | |
paul@23 | 86 | inline_pattern_names = ["fontstyle", "larger", "monospace", "smaller", "sub", "super", "underline"] |
paul@23 | 87 | |
paul@23 | 88 | def inline_patterns_for(name): |
paul@23 | 89 | names = inline_pattern_names[:] |
paul@23 | 90 | names[names.index(name)] = "%send" % name |
paul@23 | 91 | return names |
paul@23 | 92 | |
paul@0 | 93 | # Define patterns for the regular expressions. |
paul@0 | 94 | |
paul@0 | 95 | patterns = {} |
paul@13 | 96 | for name, value in syntax.items(): |
paul@13 | 97 | patterns[name] = re.compile(value, re.UNICODE | re.MULTILINE) |
paul@0 | 98 | |
paul@0 | 99 | |
paul@0 | 100 | |
paul@2 | 101 | # Tokenising functions. |
paul@2 | 102 | |
paul@2 | 103 | class TokenStream: |
paul@2 | 104 | |
paul@2 | 105 | "A stream of tokens taken from a string." |
paul@2 | 106 | |
paul@2 | 107 | def __init__(self, s): |
paul@2 | 108 | self.s = s |
paul@2 | 109 | self.pos = 0 |
paul@2 | 110 | self.match = None |
paul@2 | 111 | self.matching = None |
paul@2 | 112 | |
paul@18 | 113 | def rewind(self, length): |
paul@18 | 114 | |
paul@18 | 115 | "Rewind in the string by 'length'." |
paul@18 | 116 | |
paul@18 | 117 | self.pos -= min(length, self.pos) |
paul@18 | 118 | |
paul@2 | 119 | def read_until(self, pattern_names, remaining=True): |
paul@2 | 120 | |
paul@2 | 121 | """ |
paul@2 | 122 | Find the first match for the given 'pattern_names'. Return the text |
paul@2 | 123 | preceding any match, the remaining text if no match was found, or None |
paul@2 | 124 | if no match was found and 'remaining' is given as a false value. |
paul@2 | 125 | """ |
paul@2 | 126 | |
paul@2 | 127 | first = None |
paul@2 | 128 | self.matching = None |
paul@2 | 129 | |
paul@2 | 130 | # Find the first matching pattern. |
paul@2 | 131 | |
paul@2 | 132 | for pattern_name in pattern_names: |
paul@2 | 133 | match = patterns[pattern_name].search(self.s, self.pos) |
paul@2 | 134 | if match: |
paul@2 | 135 | start, end = match.span() |
paul@2 | 136 | if self.matching is None or start < first: |
paul@2 | 137 | first = start |
paul@2 | 138 | self.matching = pattern_name |
paul@2 | 139 | self.match = match |
paul@2 | 140 | |
paul@2 | 141 | if self.matching is None: |
paul@2 | 142 | if remaining: |
paul@2 | 143 | return self.s[self.pos:] |
paul@2 | 144 | else: |
paul@2 | 145 | return None |
paul@2 | 146 | else: |
paul@2 | 147 | return self.s[self.pos:first] |
paul@2 | 148 | |
paul@10 | 149 | def read_match(self, group=1): |
paul@2 | 150 | |
paul@10 | 151 | """ |
paul@10 | 152 | Return the matched text, updating the position in the stream. If 'group' |
paul@10 | 153 | is specified, the indicated group in a match will be returned. |
paul@10 | 154 | Typically, group 1 should contain all pertinent data, but groups defined |
paul@10 | 155 | within group 1 can provide sections of the data. |
paul@10 | 156 | """ |
paul@2 | 157 | |
paul@2 | 158 | if self.match: |
paul@2 | 159 | _start, self.pos = self.match.span() |
paul@9 | 160 | try: |
paul@10 | 161 | return self.match.group(group) |
paul@9 | 162 | except IndexError: |
paul@9 | 163 | return "" |
paul@2 | 164 | else: |
paul@2 | 165 | self.pos = len(self.s) |
paul@2 | 166 | return None |
paul@2 | 167 | |
paul@2 | 168 | |
paul@2 | 169 | |
paul@0 | 170 | # Parser functions. |
paul@0 | 171 | |
paul@0 | 172 | def parse_page(s): |
paul@0 | 173 | |
paul@0 | 174 | """ |
paul@0 | 175 | Parse page text 's'. Pages consist of regions delimited by markers. |
paul@0 | 176 | """ |
paul@0 | 177 | |
paul@6 | 178 | return parse_region(TokenStream(s)) |
paul@1 | 179 | |
paul@10 | 180 | def parse_region(items, level=0, indent=0): |
paul@1 | 181 | |
paul@6 | 182 | """ |
paul@10 | 183 | Parse the data provided by 'items' to populate a region with the given |
paul@10 | 184 | 'level' at the given 'indent'. |
paul@6 | 185 | """ |
paul@0 | 186 | |
paul@10 | 187 | region = Region([], level, indent) |
paul@0 | 188 | |
paul@2 | 189 | # Parse section headers. |
paul@2 | 190 | |
paul@2 | 191 | parse_region_header(items, region) |
paul@2 | 192 | |
paul@8 | 193 | # Parse section body. |
paul@8 | 194 | |
paul@2 | 195 | if region.is_transparent(): |
paul@2 | 196 | parse_region_wiki(items, region) |
paul@2 | 197 | else: |
paul@2 | 198 | parse_region_opaque(items, region) |
paul@2 | 199 | |
paul@6 | 200 | return region |
paul@6 | 201 | |
paul@6 | 202 | def parse_region_header(items, region): |
paul@6 | 203 | |
paul@6 | 204 | """ |
paul@6 | 205 | Parse the region header from the 'items', setting it for the given 'region'. |
paul@6 | 206 | """ |
paul@6 | 207 | |
paul@6 | 208 | if items.read_until(["header"], False) == "": # None means no header |
paul@6 | 209 | region.type = items.read_match() |
paul@6 | 210 | |
paul@2 | 211 | def parse_region_wiki(items, region): |
paul@2 | 212 | |
paul@2 | 213 | "Parse the data provided by 'items' to populate a wiki 'region'." |
paul@0 | 214 | |
paul@8 | 215 | new_block(region) |
paul@20 | 216 | parse_region_details(items, region, inline_pattern_names + [ |
paul@16 | 217 | "break", "heading", |
paul@16 | 218 | "defterm", "defterm_empty", |
paul@16 | 219 | "listitem", "listitem_alpha", "listitem_dot", "listitem_num", |
paul@16 | 220 | "listitem_roman", |
paul@17 | 221 | "regionstart", "regionend", |
paul@17 | 222 | "rule", |
paul@18 | 223 | ]) |
paul@0 | 224 | |
paul@8 | 225 | def parse_region_opaque(items, region): |
paul@1 | 226 | |
paul@8 | 227 | "Parse the data provided by 'items' to populate an opaque 'region'." |
paul@8 | 228 | |
paul@8 | 229 | parse_region_details(items, region, ["regionend"]) |
paul@1 | 230 | |
paul@8 | 231 | def parse_region_details(items, region, pattern_names): |
paul@0 | 232 | |
paul@8 | 233 | "Parse 'items' within 'region' searching using 'pattern_names'." |
paul@0 | 234 | |
paul@8 | 235 | try: |
paul@8 | 236 | while True: |
paul@0 | 237 | |
paul@8 | 238 | # Obtain text before any marker or the end of the input. |
paul@2 | 239 | |
paul@8 | 240 | preceding = items.read_until(pattern_names) |
paul@8 | 241 | if preceding: |
paul@17 | 242 | region.append_inline(Text(preceding)) |
paul@2 | 243 | |
paul@8 | 244 | # End of input. |
paul@0 | 245 | |
paul@8 | 246 | if not items.matching: |
paul@8 | 247 | break |
paul@8 | 248 | |
paul@8 | 249 | # Obtain any feature. |
paul@2 | 250 | |
paul@8 | 251 | feature = items.read_match() |
paul@8 | 252 | handler = handlers.get(items.matching) |
paul@2 | 253 | |
paul@8 | 254 | # Handle each feature or add text to the region. |
paul@2 | 255 | |
paul@8 | 256 | if handler: |
paul@8 | 257 | handler(items, region) |
paul@8 | 258 | else: |
paul@17 | 259 | region.append_inline(Text(feature)) |
paul@2 | 260 | |
paul@8 | 261 | except StopIteration: |
paul@8 | 262 | pass |
paul@2 | 263 | |
paul@2 | 264 | region.normalise() |
paul@0 | 265 | |
paul@8 | 266 | def end_region(items, region): |
paul@7 | 267 | |
paul@8 | 268 | "End the parsing of 'region'." |
paul@7 | 269 | |
paul@8 | 270 | raise StopIteration |
paul@7 | 271 | |
paul@8 | 272 | def parse_break(items, region): |
paul@8 | 273 | |
paul@8 | 274 | "Handle a paragraph break within 'region'." |
paul@7 | 275 | |
paul@15 | 276 | region.add(Break()) |
paul@8 | 277 | new_block(region) |
paul@2 | 278 | |
paul@16 | 279 | def parse_defitem(items, region, extra=""): |
paul@16 | 280 | |
paul@16 | 281 | "Handle a definition item within 'region'." |
paul@16 | 282 | |
paul@16 | 283 | pad = items.read_match(1) |
paul@16 | 284 | item = DefItem([], pad, extra) |
paul@16 | 285 | parse_region_details(items, item, ["listitemend"]) |
paul@21 | 286 | region.add(item) |
paul@16 | 287 | new_block(region) |
paul@16 | 288 | |
paul@16 | 289 | def parse_defterm(items, region): |
paul@16 | 290 | |
paul@16 | 291 | "Handle a definition term within 'region'." |
paul@16 | 292 | |
paul@16 | 293 | pad = items.read_match(1) |
paul@16 | 294 | term = DefTerm([], pad) |
paul@16 | 295 | parse_region_details(items, term, ["deftermend", "deftermsep"]) |
paul@21 | 296 | region.add(term) |
paul@16 | 297 | if items.matching == "deftermsep": |
paul@16 | 298 | parse_defitem(items, region) |
paul@16 | 299 | |
paul@16 | 300 | def parse_defterm_empty(items, region): |
paul@16 | 301 | |
paul@16 | 302 | "Handle an empty definition term within 'region'." |
paul@16 | 303 | |
paul@16 | 304 | extra = items.read_match(1) |
paul@16 | 305 | parse_region_details(items, region, ["deftermsep"]) |
paul@16 | 306 | parse_defitem(items, region, extra) |
paul@16 | 307 | |
paul@18 | 308 | def parse_fontstyle(items, region): |
paul@18 | 309 | |
paul@18 | 310 | "Handle emphasis and strong styles." |
paul@18 | 311 | |
paul@18 | 312 | n = len(items.read_match(1)) |
paul@18 | 313 | |
paul@18 | 314 | # Handle endings. |
paul@18 | 315 | |
paul@18 | 316 | if isinstance(region, FontStyle): |
paul@18 | 317 | emphasis = n in (2, 4, 5) |
paul@18 | 318 | strong = n in (3, 5, 6) |
paul@18 | 319 | active = True |
paul@17 | 320 | |
paul@18 | 321 | if region.emphasis and emphasis: |
paul@18 | 322 | active = region.close_emphasis() |
paul@18 | 323 | n -= 2 |
paul@18 | 324 | if region.strong and strong: |
paul@18 | 325 | active = region.close_strong() |
paul@18 | 326 | n -= 3 |
paul@18 | 327 | |
paul@18 | 328 | if not active: |
paul@18 | 329 | if n: |
paul@18 | 330 | items.rewind(n) |
paul@18 | 331 | raise StopIteration |
paul@17 | 332 | |
paul@18 | 333 | elif not n: |
paul@18 | 334 | return |
paul@18 | 335 | |
paul@18 | 336 | # Handle new styles. |
paul@18 | 337 | |
paul@18 | 338 | emphasis = n in (2, 4, 5) |
paul@18 | 339 | strong = n in (3, 5, 6) |
paul@18 | 340 | double = n in (4, 6) |
paul@18 | 341 | |
paul@18 | 342 | span = FontStyle([], emphasis, strong) |
paul@18 | 343 | if not double: |
paul@20 | 344 | parse_region_details(items, span, inline_pattern_names) |
paul@17 | 345 | region.append_inline(span) |
paul@16 | 346 | |
paul@13 | 347 | def parse_heading(items, region): |
paul@13 | 348 | |
paul@13 | 349 | "Handle a heading." |
paul@9 | 350 | |
paul@13 | 351 | start_extra = items.read_match(1) |
paul@13 | 352 | level = len(items.read_match(2)) |
paul@13 | 353 | start_pad = items.read_match(3) |
paul@13 | 354 | heading = Heading([], level, start_extra, start_pad) |
paul@20 | 355 | parse_region_details(items, heading, ["headingend"] + inline_pattern_names) |
paul@21 | 356 | region.add(heading) |
paul@13 | 357 | new_block(region) |
paul@9 | 358 | |
paul@13 | 359 | def parse_heading_end(items, heading): |
paul@13 | 360 | |
paul@13 | 361 | "Handle the end of a heading." |
paul@13 | 362 | |
paul@13 | 363 | level = len(items.read_match(2)) |
paul@13 | 364 | if heading.level == level: |
paul@13 | 365 | heading.end_pad = items.read_match(1) |
paul@13 | 366 | heading.end_extra = items.read_match(3) |
paul@13 | 367 | raise StopIteration |
paul@9 | 368 | |
paul@9 | 369 | def parse_listitem(items, region): |
paul@9 | 370 | |
paul@9 | 371 | "Handle a list item marker within 'region'." |
paul@9 | 372 | |
paul@14 | 373 | indent = len(items.read_match(1)) |
paul@14 | 374 | marker = items.read_match(2) |
paul@14 | 375 | space = items.read_match(3) |
paul@14 | 376 | item = ListItem([], indent, marker, space) |
paul@9 | 377 | parse_region_details(items, item, ["listitemend"]) |
paul@21 | 378 | region.add(item) |
paul@9 | 379 | new_block(region) |
paul@9 | 380 | |
paul@12 | 381 | def parse_rule(items, region): |
paul@12 | 382 | |
paul@12 | 383 | "Handle a horizontal rule within 'region'." |
paul@12 | 384 | |
paul@12 | 385 | length = len(items.read_match(1)) |
paul@12 | 386 | rule = Rule(length) |
paul@21 | 387 | region.add(rule) |
paul@12 | 388 | new_block(region) |
paul@12 | 389 | |
paul@8 | 390 | def parse_section(items, region): |
paul@2 | 391 | |
paul@8 | 392 | "Handle the start of a new section within 'region'." |
paul@2 | 393 | |
paul@8 | 394 | # Parse the section and start a new block after the section. |
paul@2 | 395 | |
paul@10 | 396 | indent = len(items.read_match(2)) |
paul@10 | 397 | level = len(items.read_match(3)) |
paul@21 | 398 | region.add(parse_region(items, level, indent)) |
paul@8 | 399 | new_block(region) |
paul@2 | 400 | |
paul@8 | 401 | def parse_section_end(items, region): |
paul@2 | 402 | |
paul@8 | 403 | "Handle the end of a new section within 'region'." |
paul@1 | 404 | |
paul@8 | 405 | feature = items.read_match() |
paul@8 | 406 | if region.have_end(feature): |
paul@8 | 407 | raise StopIteration |
paul@8 | 408 | else: |
paul@17 | 409 | region.append_inline(Text(feature)) |
paul@17 | 410 | |
paul@23 | 411 | # Inline formatting handlers. |
paul@23 | 412 | |
paul@23 | 413 | def parse_inline(items, region, cls, pattern_name): |
paul@20 | 414 | |
paul@23 | 415 | "Handle an inline region." |
paul@20 | 416 | |
paul@23 | 417 | span = cls([]) |
paul@23 | 418 | parse_region_details(items, span, inline_patterns_for(pattern_name)) |
paul@20 | 419 | region.append_inline(span) |
paul@20 | 420 | |
paul@23 | 421 | parse_larger = lambda items, region: parse_inline(items, region, Larger, "larger") |
paul@23 | 422 | parse_monospace = lambda items, region: parse_inline(items, region, Monospace, "monospace") |
paul@23 | 423 | parse_smaller = lambda items, region: parse_inline(items, region, Smaller, "smaller") |
paul@23 | 424 | parse_sub = lambda items, region: parse_inline(items, region, Subscript, "sub") |
paul@23 | 425 | parse_super = lambda items, region: parse_inline(items, region, Superscript, "super") |
paul@23 | 426 | parse_underline = lambda items, region: parse_inline(items, region, Underline, "underline") |
paul@19 | 427 | |
paul@8 | 428 | # Pattern handlers. |
paul@2 | 429 | |
paul@8 | 430 | handlers = { |
paul@8 | 431 | None : end_region, |
paul@8 | 432 | "break" : parse_break, |
paul@16 | 433 | "defterm" : parse_defterm, |
paul@16 | 434 | "defterm_empty" : parse_defterm_empty, |
paul@17 | 435 | "deftermend" : end_region, |
paul@17 | 436 | "deftermsep" : end_region, |
paul@18 | 437 | "fontstyle" : parse_fontstyle, |
paul@13 | 438 | "heading" : parse_heading, |
paul@13 | 439 | "headingend" : parse_heading_end, |
paul@23 | 440 | "larger" : parse_larger, |
paul@23 | 441 | "largerend" : end_region, |
paul@17 | 442 | "listitemend" : end_region, |
paul@9 | 443 | "listitem" : parse_listitem, |
paul@14 | 444 | "listitem_alpha" : parse_listitem, |
paul@14 | 445 | "listitem_dot" : parse_listitem, |
paul@14 | 446 | "listitem_num" : parse_listitem, |
paul@14 | 447 | "listitem_roman" : parse_listitem, |
paul@18 | 448 | "monospace" : parse_monospace, |
paul@18 | 449 | "monospaceend" : end_region, |
paul@8 | 450 | "regionstart" : parse_section, |
paul@8 | 451 | "regionend" : parse_section_end, |
paul@12 | 452 | "rule" : parse_rule, |
paul@23 | 453 | "smaller" : parse_smaller, |
paul@23 | 454 | "smallerend" : end_region, |
paul@20 | 455 | "sub" : parse_sub, |
paul@20 | 456 | "subend" : end_region, |
paul@20 | 457 | "super" : parse_super, |
paul@20 | 458 | "superend" : end_region, |
paul@19 | 459 | "underline" : parse_underline, |
paul@19 | 460 | "underlineend" : end_region, |
paul@8 | 461 | } |
paul@2 | 462 | |
paul@6 | 463 | def new_block(region): |
paul@6 | 464 | |
paul@6 | 465 | "Start a new block in 'region'." |
paul@0 | 466 | |
paul@6 | 467 | block = Block([]) |
paul@15 | 468 | region.add(block) |
paul@0 | 469 | |
paul@1 | 470 | |
paul@1 | 471 | |
paul@1 | 472 | # Top-level functions. |
paul@0 | 473 | |
paul@0 | 474 | parse = parse_page |
paul@0 | 475 | |
paul@0 | 476 | # vim: tabstop=4 expandtab shiftwidth=4 |