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@11 | 22 | from moinformat.tree import Region, Block, ListItem, Text |
paul@0 | 23 | import re |
paul@0 | 24 | |
paul@0 | 25 | # Regular expressions. |
paul@0 | 26 | |
paul@0 | 27 | syntax = { |
paul@0 | 28 | # Page regions: |
paul@10 | 29 | "regionstart" : (r"((^\s*)([{]{3,}))", re.MULTILINE | re.DOTALL), # {{{... |
paul@6 | 30 | "regionend" : (r"^\s*([}]{3,})", re.MULTILINE | re.DOTALL), # }}}... |
paul@6 | 31 | "header" : (r"#!(.*?)\n", 0), # #! char-excl-nl |
paul@0 | 32 | |
paul@0 | 33 | # Region contents: |
paul@6 | 34 | "break" : (r"^(\s*?)\n", re.MULTILINE), # blank line |
paul@9 | 35 | "listitem" : (r"^((\s+)([*]|\d+[.]))", re.MULTILINE), # indent (list-item or number-item) |
paul@9 | 36 | |
paul@9 | 37 | # List contents: |
paul@9 | 38 | "listitemend" : (r"^", re.MULTILINE), # next line |
paul@0 | 39 | } |
paul@0 | 40 | |
paul@0 | 41 | # Define patterns for the regular expressions. |
paul@0 | 42 | |
paul@0 | 43 | patterns = {} |
paul@0 | 44 | for name, (value, flags) in syntax.items(): |
paul@0 | 45 | patterns[name] = re.compile(value, re.UNICODE | flags) |
paul@0 | 46 | |
paul@0 | 47 | |
paul@0 | 48 | |
paul@2 | 49 | # Tokenising functions. |
paul@2 | 50 | |
paul@2 | 51 | class TokenStream: |
paul@2 | 52 | |
paul@2 | 53 | "A stream of tokens taken from a string." |
paul@2 | 54 | |
paul@2 | 55 | def __init__(self, s): |
paul@2 | 56 | self.s = s |
paul@2 | 57 | self.pos = 0 |
paul@2 | 58 | self.match = None |
paul@2 | 59 | self.matching = None |
paul@2 | 60 | |
paul@2 | 61 | def read_until(self, pattern_names, remaining=True): |
paul@2 | 62 | |
paul@2 | 63 | """ |
paul@2 | 64 | Find the first match for the given 'pattern_names'. Return the text |
paul@2 | 65 | preceding any match, the remaining text if no match was found, or None |
paul@2 | 66 | if no match was found and 'remaining' is given as a false value. |
paul@2 | 67 | """ |
paul@2 | 68 | |
paul@2 | 69 | first = None |
paul@2 | 70 | self.matching = None |
paul@2 | 71 | |
paul@2 | 72 | # Find the first matching pattern. |
paul@2 | 73 | |
paul@2 | 74 | for pattern_name in pattern_names: |
paul@2 | 75 | match = patterns[pattern_name].search(self.s, self.pos) |
paul@2 | 76 | if match: |
paul@2 | 77 | start, end = match.span() |
paul@2 | 78 | if self.matching is None or start < first: |
paul@2 | 79 | first = start |
paul@2 | 80 | self.matching = pattern_name |
paul@2 | 81 | self.match = match |
paul@2 | 82 | |
paul@2 | 83 | if self.matching is None: |
paul@2 | 84 | if remaining: |
paul@2 | 85 | return self.s[self.pos:] |
paul@2 | 86 | else: |
paul@2 | 87 | return None |
paul@2 | 88 | else: |
paul@2 | 89 | return self.s[self.pos:first] |
paul@2 | 90 | |
paul@10 | 91 | def read_match(self, group=1): |
paul@2 | 92 | |
paul@10 | 93 | """ |
paul@10 | 94 | Return the matched text, updating the position in the stream. If 'group' |
paul@10 | 95 | is specified, the indicated group in a match will be returned. |
paul@10 | 96 | Typically, group 1 should contain all pertinent data, but groups defined |
paul@10 | 97 | within group 1 can provide sections of the data. |
paul@10 | 98 | """ |
paul@2 | 99 | |
paul@2 | 100 | if self.match: |
paul@2 | 101 | _start, self.pos = self.match.span() |
paul@9 | 102 | try: |
paul@10 | 103 | return self.match.group(group) |
paul@9 | 104 | except IndexError: |
paul@9 | 105 | return "" |
paul@2 | 106 | else: |
paul@2 | 107 | self.pos = len(self.s) |
paul@2 | 108 | return None |
paul@2 | 109 | |
paul@2 | 110 | |
paul@2 | 111 | |
paul@0 | 112 | # Parser functions. |
paul@0 | 113 | |
paul@0 | 114 | def parse_page(s): |
paul@0 | 115 | |
paul@0 | 116 | """ |
paul@0 | 117 | Parse page text 's'. Pages consist of regions delimited by markers. |
paul@0 | 118 | """ |
paul@0 | 119 | |
paul@6 | 120 | return parse_region(TokenStream(s)) |
paul@1 | 121 | |
paul@10 | 122 | def parse_region(items, level=0, indent=0): |
paul@1 | 123 | |
paul@6 | 124 | """ |
paul@10 | 125 | Parse the data provided by 'items' to populate a region with the given |
paul@10 | 126 | 'level' at the given 'indent'. |
paul@6 | 127 | """ |
paul@0 | 128 | |
paul@10 | 129 | region = Region([], level, indent) |
paul@0 | 130 | |
paul@2 | 131 | # Parse section headers. |
paul@2 | 132 | |
paul@2 | 133 | parse_region_header(items, region) |
paul@2 | 134 | |
paul@8 | 135 | # Parse section body. |
paul@8 | 136 | |
paul@2 | 137 | if region.is_transparent(): |
paul@2 | 138 | parse_region_wiki(items, region) |
paul@2 | 139 | else: |
paul@2 | 140 | parse_region_opaque(items, region) |
paul@2 | 141 | |
paul@6 | 142 | return region |
paul@6 | 143 | |
paul@6 | 144 | def parse_region_header(items, region): |
paul@6 | 145 | |
paul@6 | 146 | """ |
paul@6 | 147 | Parse the region header from the 'items', setting it for the given 'region'. |
paul@6 | 148 | """ |
paul@6 | 149 | |
paul@6 | 150 | if items.read_until(["header"], False) == "": # None means no header |
paul@6 | 151 | region.type = items.read_match() |
paul@6 | 152 | |
paul@2 | 153 | def parse_region_wiki(items, region): |
paul@2 | 154 | |
paul@2 | 155 | "Parse the data provided by 'items' to populate a wiki 'region'." |
paul@0 | 156 | |
paul@8 | 157 | new_block(region) |
paul@9 | 158 | parse_region_details(items, region, ["break", "listitem", "regionstart", "regionend"]) |
paul@0 | 159 | |
paul@8 | 160 | def parse_region_opaque(items, region): |
paul@1 | 161 | |
paul@8 | 162 | "Parse the data provided by 'items' to populate an opaque 'region'." |
paul@8 | 163 | |
paul@8 | 164 | parse_region_details(items, region, ["regionend"]) |
paul@1 | 165 | |
paul@8 | 166 | def parse_region_details(items, region, pattern_names): |
paul@0 | 167 | |
paul@8 | 168 | "Parse 'items' within 'region' searching using 'pattern_names'." |
paul@0 | 169 | |
paul@8 | 170 | try: |
paul@8 | 171 | while True: |
paul@0 | 172 | |
paul@8 | 173 | # Obtain text before any marker or the end of the input. |
paul@2 | 174 | |
paul@8 | 175 | preceding = items.read_until(pattern_names) |
paul@8 | 176 | if preceding: |
paul@8 | 177 | region.append_text(Text(preceding)) |
paul@2 | 178 | |
paul@8 | 179 | # End of input. |
paul@0 | 180 | |
paul@8 | 181 | if not items.matching: |
paul@8 | 182 | break |
paul@8 | 183 | |
paul@8 | 184 | # Obtain any feature. |
paul@2 | 185 | |
paul@8 | 186 | feature = items.read_match() |
paul@8 | 187 | handler = handlers.get(items.matching) |
paul@2 | 188 | |
paul@8 | 189 | # Handle each feature or add text to the region. |
paul@2 | 190 | |
paul@8 | 191 | if handler: |
paul@8 | 192 | handler(items, region) |
paul@8 | 193 | else: |
paul@8 | 194 | region.append_text(Text(feature)) |
paul@2 | 195 | |
paul@8 | 196 | except StopIteration: |
paul@8 | 197 | pass |
paul@2 | 198 | |
paul@2 | 199 | region.normalise() |
paul@0 | 200 | |
paul@8 | 201 | def end_region(items, region): |
paul@7 | 202 | |
paul@8 | 203 | "End the parsing of 'region'." |
paul@7 | 204 | |
paul@8 | 205 | raise StopIteration |
paul@7 | 206 | |
paul@8 | 207 | def parse_break(items, region): |
paul@8 | 208 | |
paul@8 | 209 | "Handle a paragraph break within 'region'." |
paul@7 | 210 | |
paul@7 | 211 | # Mark any previous block as not being the final one in a sequence. |
paul@7 | 212 | |
paul@7 | 213 | block = region.nodes[-1] |
paul@7 | 214 | block.final = False |
paul@8 | 215 | new_block(region) |
paul@2 | 216 | |
paul@9 | 217 | def parse_listitem_end(items, region): |
paul@9 | 218 | |
paul@9 | 219 | "Handle the end of a list." |
paul@9 | 220 | |
paul@9 | 221 | raise StopIteration |
paul@9 | 222 | |
paul@9 | 223 | def parse_listitem(items, region): |
paul@9 | 224 | |
paul@9 | 225 | "Handle a list item marker within 'region'." |
paul@9 | 226 | |
paul@9 | 227 | item = ListItem([]) |
paul@9 | 228 | parse_region_details(items, item, ["listitemend"]) |
paul@9 | 229 | region.append(item) |
paul@9 | 230 | new_block(region) |
paul@9 | 231 | |
paul@8 | 232 | def parse_section(items, region): |
paul@2 | 233 | |
paul@8 | 234 | "Handle the start of a new section within 'region'." |
paul@2 | 235 | |
paul@8 | 236 | # Parse the section and start a new block after the section. |
paul@2 | 237 | |
paul@10 | 238 | indent = len(items.read_match(2)) |
paul@10 | 239 | level = len(items.read_match(3)) |
paul@10 | 240 | region.append(parse_region(items, level, indent)) |
paul@8 | 241 | new_block(region) |
paul@2 | 242 | |
paul@8 | 243 | def parse_section_end(items, region): |
paul@2 | 244 | |
paul@8 | 245 | "Handle the end of a new section within 'region'." |
paul@1 | 246 | |
paul@8 | 247 | feature = items.read_match() |
paul@8 | 248 | if region.have_end(feature): |
paul@8 | 249 | raise StopIteration |
paul@8 | 250 | else: |
paul@8 | 251 | region.append_text(Text(feature)) |
paul@2 | 252 | |
paul@8 | 253 | # Pattern handlers. |
paul@2 | 254 | |
paul@8 | 255 | handlers = { |
paul@8 | 256 | None : end_region, |
paul@8 | 257 | "break" : parse_break, |
paul@9 | 258 | "listitemend" : parse_listitem_end, |
paul@9 | 259 | "listitem" : parse_listitem, |
paul@8 | 260 | "regionstart" : parse_section, |
paul@8 | 261 | "regionend" : parse_section_end, |
paul@8 | 262 | } |
paul@2 | 263 | |
paul@6 | 264 | def new_block(region): |
paul@6 | 265 | |
paul@6 | 266 | "Start a new block in 'region'." |
paul@0 | 267 | |
paul@6 | 268 | block = Block([]) |
paul@6 | 269 | region.append(block) |
paul@0 | 270 | |
paul@1 | 271 | |
paul@1 | 272 | |
paul@1 | 273 | # Top-level functions. |
paul@0 | 274 | |
paul@0 | 275 | parse = parse_page |
paul@0 | 276 | |
paul@0 | 277 | # vim: tabstop=4 expandtab shiftwidth=4 |