paul@38 | 1 | #!/usr/bin/env python |
paul@38 | 2 | |
paul@38 | 3 | """ |
paul@38 | 4 | Moin wiki table parser. |
paul@38 | 5 | |
paul@301 | 6 | Copyright (C) 2017, 2018, 2021 Paul Boddie <paul@boddie.org.uk> |
paul@38 | 7 | |
paul@38 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@38 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@38 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@38 | 11 | version. |
paul@38 | 12 | |
paul@38 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@38 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@38 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@38 | 16 | details. |
paul@38 | 17 | |
paul@38 | 18 | You should have received a copy of the GNU General Public License along with |
paul@38 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@38 | 20 | """ |
paul@38 | 21 | |
paul@308 | 22 | from moinformat.parsers.common import get_patterns, excl, group |
paul@42 | 23 | from moinformat.parsers.moin import MoinParser |
paul@308 | 24 | from moinformat.tree.moin import Break, Table, TableAttrs, TableCell, \ |
paul@308 | 25 | TableRow, Text |
paul@83 | 26 | from moinformat.tree.table import Continuation |
paul@38 | 27 | |
paul@55 | 28 | join = "".join |
paul@38 | 29 | |
paul@38 | 30 | # Parser functionality. |
paul@38 | 31 | |
paul@42 | 32 | class TableParser(MoinParser): |
paul@38 | 33 | |
paul@38 | 34 | "A parser for improved table syntax." |
paul@38 | 35 | |
paul@301 | 36 | formats = ["table"] |
paul@109 | 37 | |
paul@38 | 38 | # Principal parser methods. |
paul@38 | 39 | |
paul@38 | 40 | def parse_region_content(self, items, region): |
paul@38 | 41 | |
paul@98 | 42 | """ |
paul@98 | 43 | Parse the data provided by 'items' to populate the given 'region'. For |
paul@98 | 44 | table regions, normal region handling is wrapped by management of the |
paul@98 | 45 | table structure. |
paul@98 | 46 | """ |
paul@38 | 47 | |
paul@38 | 48 | self.set_region(items, region) |
paul@38 | 49 | |
paul@38 | 50 | # Start to populate table rows. |
paul@38 | 51 | |
paul@38 | 52 | cell = TableCell([]) |
paul@38 | 53 | row = TableRow([cell]) |
paul@38 | 54 | table = Table([row]) |
paul@43 | 55 | self.append_node(self.region, table) |
paul@308 | 56 | self.new_block(cell) |
paul@38 | 57 | |
paul@38 | 58 | while True: |
paul@98 | 59 | self.parse_region_details(cell, self.region_pattern_names) |
paul@38 | 60 | |
paul@38 | 61 | # Detect the end of the table. |
paul@38 | 62 | |
paul@54 | 63 | pattern = self.matching_pattern() |
paul@54 | 64 | |
paul@297 | 65 | if not pattern or pattern == "regionend": |
paul@38 | 66 | break |
paul@38 | 67 | |
paul@54 | 68 | elif pattern == "columnsep": |
paul@305 | 69 | cell = TableCell([], leading=self.match_group("leading"), |
paul@305 | 70 | padding=self.match_group("padding")) |
paul@38 | 71 | row.append(cell) |
paul@308 | 72 | self.new_block(cell) |
paul@38 | 73 | |
paul@54 | 74 | elif pattern == "rowsep": |
paul@305 | 75 | row = TableRow([], leading=self.match_group("leading"), |
paul@305 | 76 | padding=self.match_group("padding")) |
paul@38 | 77 | table.append(row) |
paul@38 | 78 | cell = TableCell([]) |
paul@38 | 79 | row.append(cell) |
paul@308 | 80 | self.new_block(cell) |
paul@38 | 81 | |
paul@38 | 82 | # Parser handler methods. |
paul@38 | 83 | |
paul@38 | 84 | def parse_continuation(self, cell): |
paul@72 | 85 | |
paul@72 | 86 | "Handle continuation padding." |
paul@72 | 87 | |
paul@72 | 88 | feature = self.match_group("feature") |
paul@308 | 89 | self.append_inline(cell, Continuation(feature)) |
paul@308 | 90 | |
paul@308 | 91 | def parse_table_attrs(self, cell): |
paul@38 | 92 | |
paul@308 | 93 | "Handle the start of table attributes within 'cell'." |
paul@38 | 94 | |
paul@308 | 95 | MoinParser.parse_table_attrs(self, cell) |
paul@308 | 96 | |
paul@308 | 97 | if isinstance(cell.node(-1), TableAttrs): |
paul@308 | 98 | self.new_block(cell) |
paul@38 | 99 | |
paul@38 | 100 | # Regular expressions. |
paul@38 | 101 | |
paul@38 | 102 | syntax = {} |
paul@42 | 103 | syntax.update(MoinParser.syntax) |
paul@38 | 104 | syntax.update({ |
paul@38 | 105 | # At start of line: |
paul@55 | 106 | |
paul@305 | 107 | "rowsep" : join((group("leading", r"\s*"), # ws... (optional) |
paul@305 | 108 | "^==", # == |
paul@55 | 109 | excl(r".*==\s*?$"), # not-heading |
paul@305 | 110 | group("padding", r"\s*"))), # ws... (optional) |
paul@55 | 111 | |
paul@72 | 112 | "continuation" : group("feature", |
paul@305 | 113 | join((group("leading", r"\s*"), # ws... (optional) |
paul@305 | 114 | "^", |
paul@305 | 115 | group("indent", r"\s*"), # ws... (optional) |
paul@55 | 116 | r"\.\.", # .. |
paul@305 | 117 | excl(r"\."), # not-. |
paul@305 | 118 | group("padding", r"\s*")))),# ws... (optional) |
paul@38 | 119 | |
paul@38 | 120 | # Within text: |
paul@55 | 121 | |
paul@305 | 122 | "columnsep" : join((group("leading", r"\s*"), # ws... (optional) |
paul@305 | 123 | r"\|\|", # || |
paul@305 | 124 | excl(r"\|"), # not-| |
paul@305 | 125 | group("padding", r"\s*"))), # ws... (optional) |
paul@38 | 126 | }) |
paul@38 | 127 | |
paul@38 | 128 | patterns = get_patterns(syntax) |
paul@38 | 129 | |
paul@38 | 130 | |
paul@38 | 131 | |
paul@38 | 132 | # Pattern details. |
paul@38 | 133 | |
paul@98 | 134 | region_pattern_names = [ |
paul@102 | 135 | "columnsep", "continuation", "rowsep", "tableattrs", |
paul@73 | 136 | ] + MoinParser.region_without_table_pattern_names |
paul@38 | 137 | |
paul@38 | 138 | |
paul@38 | 139 | |
paul@38 | 140 | # Pattern handlers. |
paul@38 | 141 | |
paul@98 | 142 | end_region = MoinParser.end_region |
paul@98 | 143 | parse_table_end = MoinParser.parse_region_end |
paul@98 | 144 | |
paul@38 | 145 | handlers = {} |
paul@42 | 146 | handlers.update(MoinParser.handlers) |
paul@38 | 147 | handlers.update({ |
paul@98 | 148 | "columnsep" : end_region, |
paul@38 | 149 | "continuation" : parse_continuation, |
paul@98 | 150 | "rowsep" : end_region, |
paul@38 | 151 | "regionend" : parse_table_end, |
paul@308 | 152 | "tableattrs" : parse_table_attrs, |
paul@38 | 153 | }) |
paul@38 | 154 | |
paul@40 | 155 | parser = TableParser |
paul@40 | 156 | |
paul@38 | 157 | # vim: tabstop=4 expandtab shiftwidth=4 |