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