paul@32 | 1 | #!/usr/bin/env python |
paul@32 | 2 | |
paul@32 | 3 | """ |
paul@32 | 4 | Moin wiki parsing functionality. |
paul@32 | 5 | |
paul@32 | 6 | Copyright (C) 2017 Paul Boddie <paul@boddie.org.uk> |
paul@32 | 7 | |
paul@32 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@32 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@32 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@32 | 11 | version. |
paul@32 | 12 | |
paul@32 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@32 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@32 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@32 | 16 | details. |
paul@32 | 17 | |
paul@32 | 18 | You should have received a copy of the GNU General Public License along with |
paul@32 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@32 | 20 | """ |
paul@32 | 21 | |
paul@32 | 22 | from moinformat.tree import Block, Region, Text |
paul@32 | 23 | |
paul@32 | 24 | # Tokenising functions. |
paul@32 | 25 | |
paul@32 | 26 | class TokenStream: |
paul@32 | 27 | |
paul@32 | 28 | "A stream of tokens taken from a string." |
paul@32 | 29 | |
paul@32 | 30 | def __init__(self, s, patterns): |
paul@32 | 31 | self.s = s |
paul@32 | 32 | self.patterns = patterns |
paul@32 | 33 | self.pos = 0 |
paul@32 | 34 | self.match = None |
paul@32 | 35 | self.matching = None |
paul@32 | 36 | |
paul@32 | 37 | def rewind(self, length): |
paul@32 | 38 | |
paul@32 | 39 | "Rewind in the string by 'length'." |
paul@32 | 40 | |
paul@32 | 41 | self.pos -= min(length, self.pos) |
paul@32 | 42 | |
paul@32 | 43 | def read_until(self, pattern_names, remaining=True): |
paul@32 | 44 | |
paul@32 | 45 | """ |
paul@32 | 46 | Find the first match for the given 'pattern_names'. Return the text |
paul@32 | 47 | preceding any match, the remaining text if no match was found, or None |
paul@32 | 48 | if no match was found and 'remaining' is given as a false value. |
paul@32 | 49 | """ |
paul@32 | 50 | |
paul@32 | 51 | first = None |
paul@32 | 52 | self.matching = None |
paul@32 | 53 | |
paul@32 | 54 | # Find the first matching pattern. |
paul@32 | 55 | |
paul@32 | 56 | for pattern_name in pattern_names: |
paul@32 | 57 | match = self.patterns[pattern_name].search(self.s, self.pos) |
paul@32 | 58 | if match: |
paul@32 | 59 | start, end = match.span() |
paul@32 | 60 | if self.matching is None or start < first: |
paul@32 | 61 | first = start |
paul@32 | 62 | self.matching = pattern_name |
paul@32 | 63 | self.match = match |
paul@32 | 64 | |
paul@32 | 65 | if self.matching is None: |
paul@32 | 66 | if remaining: |
paul@32 | 67 | return self.s[self.pos:] |
paul@32 | 68 | else: |
paul@32 | 69 | return None |
paul@32 | 70 | else: |
paul@32 | 71 | return self.s[self.pos:first] |
paul@32 | 72 | |
paul@32 | 73 | def read_match(self, group=1): |
paul@32 | 74 | |
paul@32 | 75 | """ |
paul@32 | 76 | Return the matched text, updating the position in the stream. If 'group' |
paul@32 | 77 | is specified, the indicated group in a match will be returned. |
paul@32 | 78 | Typically, group 1 should contain all pertinent data, but groups defined |
paul@32 | 79 | within group 1 can provide sections of the data. |
paul@32 | 80 | """ |
paul@32 | 81 | |
paul@32 | 82 | if self.match: |
paul@32 | 83 | _start, self.pos = self.match.span() |
paul@32 | 84 | try: |
paul@32 | 85 | return self.match.group(group) |
paul@32 | 86 | except IndexError: |
paul@32 | 87 | return "" |
paul@32 | 88 | else: |
paul@32 | 89 | self.pos = len(self.s) |
paul@32 | 90 | return None |
paul@32 | 91 | |
paul@32 | 92 | |
paul@32 | 93 | |
paul@32 | 94 | # Utility functions. |
paul@32 | 95 | |
paul@32 | 96 | def new_block(region): |
paul@32 | 97 | |
paul@32 | 98 | "Start a new block in 'region'." |
paul@32 | 99 | |
paul@32 | 100 | region.add(Block([])) |
paul@32 | 101 | |
paul@32 | 102 | |
paul@32 | 103 | |
paul@32 | 104 | # Parser abstractions. |
paul@32 | 105 | |
paul@32 | 106 | class ParserBase: |
paul@32 | 107 | |
paul@32 | 108 | "Common parsing methods." |
paul@32 | 109 | |
paul@32 | 110 | def __init__(self, formats=None): |
paul@32 | 111 | |
paul@32 | 112 | """ |
paul@32 | 113 | Initialise the parser with any given 'formats' mapping from region type |
paul@32 | 114 | names to parser objects. |
paul@32 | 115 | """ |
paul@32 | 116 | |
paul@32 | 117 | self.formats = formats |
paul@32 | 118 | |
paul@32 | 119 | def get_items(self, s): |
paul@32 | 120 | |
paul@32 | 121 | "Return a sequence of token items for 's'." |
paul@32 | 122 | |
paul@32 | 123 | raise NotImplementedError |
paul@32 | 124 | |
paul@32 | 125 | def parse(self, s): |
paul@32 | 126 | |
paul@32 | 127 | """ |
paul@32 | 128 | Parse page text 's'. Pages consist of regions delimited by markers. |
paul@32 | 129 | """ |
paul@32 | 130 | |
paul@32 | 131 | return self.parse_region(self.get_items(s)) |
paul@32 | 132 | |
paul@32 | 133 | def parse_region(self, items, level=0, indent=0): |
paul@32 | 134 | |
paul@32 | 135 | """ |
paul@32 | 136 | Parse the data provided by 'items' to populate a region with the given |
paul@32 | 137 | 'level' at the given 'indent'. |
paul@32 | 138 | """ |
paul@32 | 139 | |
paul@32 | 140 | region = Region([], level, indent) |
paul@32 | 141 | |
paul@32 | 142 | # Parse section headers, then parse according to region type. |
paul@32 | 143 | |
paul@32 | 144 | self.parse_region_header(items, region) |
paul@32 | 145 | self.parse_region_type(items, region) |
paul@32 | 146 | |
paul@32 | 147 | return region |
paul@32 | 148 | |
paul@32 | 149 | def parse_region_type(self, items, region): |
paul@32 | 150 | |
paul@32 | 151 | """ |
paul@32 | 152 | Given data provided by 'items', use configured parsers to parse the |
paul@32 | 153 | 'region' based on its type. |
paul@32 | 154 | """ |
paul@32 | 155 | |
paul@32 | 156 | # Find an appropriate parser given the type. |
paul@32 | 157 | |
paul@32 | 158 | if self.formats.has_key(region.type): |
paul@32 | 159 | self.formats[region.type].parse_region_content(items, region) |
paul@32 | 160 | |
paul@32 | 161 | # Otherwise, treat the section as opaque. |
paul@32 | 162 | |
paul@32 | 163 | else: |
paul@32 | 164 | self.parse_region_opaque(items, region) |
paul@32 | 165 | |
paul@32 | 166 | def parse_region_header(self, items, region): |
paul@32 | 167 | |
paul@32 | 168 | """ |
paul@32 | 169 | Parse the region header from the 'items', setting it for the given 'region'. |
paul@32 | 170 | """ |
paul@32 | 171 | |
paul@32 | 172 | if items.read_until(["header"], False) == "": # None means no header |
paul@32 | 173 | region.type = items.read_match() |
paul@32 | 174 | |
paul@32 | 175 | def parse_region_opaque(self, items, region): |
paul@32 | 176 | |
paul@32 | 177 | "Parse the data provided by 'items' to populate an opaque 'region'." |
paul@32 | 178 | |
paul@32 | 179 | region.transparent = False |
paul@32 | 180 | self.parse_region_details(items, region, ["regionend"]) |
paul@32 | 181 | |
paul@32 | 182 | def parse_region_content(self, items, region): |
paul@32 | 183 | |
paul@32 | 184 | "Parse the data provided by 'items' to populate the given 'region'." |
paul@32 | 185 | |
paul@32 | 186 | pass |
paul@32 | 187 | |
paul@32 | 188 | # Parsing utilities. |
paul@32 | 189 | |
paul@32 | 190 | def parse_region_details(self, items, region, pattern_names): |
paul@32 | 191 | |
paul@32 | 192 | "Parse 'items' within 'region' searching using 'pattern_names'." |
paul@32 | 193 | |
paul@32 | 194 | try: |
paul@32 | 195 | while True: |
paul@32 | 196 | |
paul@32 | 197 | # Obtain text before any marker or the end of the input. |
paul@32 | 198 | |
paul@32 | 199 | preceding = items.read_until(pattern_names) |
paul@32 | 200 | if preceding: |
paul@32 | 201 | region.append_inline(Text(preceding)) |
paul@32 | 202 | |
paul@32 | 203 | # End of input. |
paul@32 | 204 | |
paul@32 | 205 | if not items.matching: |
paul@32 | 206 | break |
paul@32 | 207 | |
paul@32 | 208 | # Obtain any feature. |
paul@32 | 209 | |
paul@32 | 210 | feature = items.read_match() |
paul@32 | 211 | handler = self.handlers.get(items.matching) |
paul@32 | 212 | |
paul@32 | 213 | # Handle each feature or add text to the region. |
paul@32 | 214 | |
paul@32 | 215 | if handler: |
paul@32 | 216 | handler(self, items, region) |
paul@32 | 217 | else: |
paul@32 | 218 | region.append_inline(Text(feature)) |
paul@32 | 219 | |
paul@32 | 220 | except StopIteration: |
paul@32 | 221 | pass |
paul@32 | 222 | |
paul@32 | 223 | region.normalise() |
paul@32 | 224 | |
paul@32 | 225 | def end_region(self, items, region): |
paul@32 | 226 | |
paul@32 | 227 | "End the parsing of 'region', breaking out of the parsing loop." |
paul@32 | 228 | |
paul@32 | 229 | raise StopIteration |
paul@32 | 230 | |
paul@32 | 231 | # vim: tabstop=4 expandtab shiftwidth=4 |