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