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@310 | 24 | from moinformat.serialisers import serialise |
paul@308 | 25 | from moinformat.tree.moin import Break, Table, TableAttrs, TableCell, \ |
paul@308 | 26 | TableRow, Text |
paul@83 | 27 | from moinformat.tree.table import Continuation |
paul@38 | 28 | |
paul@55 | 29 | join = "".join |
paul@38 | 30 | |
paul@38 | 31 | # Parser functionality. |
paul@38 | 32 | |
paul@42 | 33 | class TableParser(MoinParser): |
paul@38 | 34 | |
paul@38 | 35 | "A parser for improved table syntax." |
paul@38 | 36 | |
paul@301 | 37 | formats = ["table"] |
paul@109 | 38 | |
paul@38 | 39 | # Principal parser methods. |
paul@38 | 40 | |
paul@38 | 41 | def parse_region_content(self, items, region): |
paul@38 | 42 | |
paul@98 | 43 | """ |
paul@98 | 44 | Parse the data provided by 'items' to populate the given 'region'. For |
paul@98 | 45 | table regions, normal region handling is wrapped by management of the |
paul@98 | 46 | table structure. |
paul@98 | 47 | """ |
paul@38 | 48 | |
paul@38 | 49 | self.set_region(items, region) |
paul@38 | 50 | |
paul@38 | 51 | # Start to populate table rows. |
paul@38 | 52 | |
paul@38 | 53 | cell = TableCell([]) |
paul@38 | 54 | row = TableRow([cell]) |
paul@38 | 55 | table = Table([row]) |
paul@43 | 56 | self.append_node(self.region, table) |
paul@308 | 57 | self.new_block(cell) |
paul@38 | 58 | |
paul@38 | 59 | while True: |
paul@98 | 60 | self.parse_region_details(cell, self.region_pattern_names) |
paul@38 | 61 | |
paul@309 | 62 | # Suppress any initial paragraph break within a table cell. This is |
paul@309 | 63 | # caused by a blank line which might be employed for clarity. |
paul@309 | 64 | |
paul@309 | 65 | first = cell.node(0) |
paul@309 | 66 | |
paul@309 | 67 | if isinstance(first, Break): |
paul@309 | 68 | cell.remove(first) |
paul@310 | 69 | cell.padding += serialise(first, self.get_serialiser()) |
paul@309 | 70 | |
paul@38 | 71 | # Detect the end of the table. |
paul@38 | 72 | |
paul@54 | 73 | pattern = self.matching_pattern() |
paul@54 | 74 | |
paul@297 | 75 | if not pattern or pattern == "regionend": |
paul@38 | 76 | break |
paul@38 | 77 | |
paul@54 | 78 | elif pattern == "columnsep": |
paul@305 | 79 | cell = TableCell([], leading=self.match_group("leading"), |
paul@305 | 80 | padding=self.match_group("padding")) |
paul@38 | 81 | row.append(cell) |
paul@308 | 82 | self.new_block(cell) |
paul@38 | 83 | |
paul@54 | 84 | elif pattern == "rowsep": |
paul@305 | 85 | row = TableRow([], leading=self.match_group("leading"), |
paul@305 | 86 | padding=self.match_group("padding")) |
paul@38 | 87 | table.append(row) |
paul@38 | 88 | cell = TableCell([]) |
paul@38 | 89 | row.append(cell) |
paul@308 | 90 | self.new_block(cell) |
paul@38 | 91 | |
paul@38 | 92 | # Parser handler methods. |
paul@38 | 93 | |
paul@38 | 94 | def parse_continuation(self, cell): |
paul@72 | 95 | |
paul@72 | 96 | "Handle continuation padding." |
paul@72 | 97 | |
paul@72 | 98 | feature = self.match_group("feature") |
paul@308 | 99 | self.append_inline(cell, Continuation(feature)) |
paul@308 | 100 | |
paul@308 | 101 | def parse_table_attrs(self, cell): |
paul@38 | 102 | |
paul@308 | 103 | "Handle the start of table attributes within 'cell'." |
paul@38 | 104 | |
paul@308 | 105 | MoinParser.parse_table_attrs(self, cell) |
paul@308 | 106 | |
paul@308 | 107 | if isinstance(cell.node(-1), TableAttrs): |
paul@308 | 108 | self.new_block(cell) |
paul@38 | 109 | |
paul@38 | 110 | # Regular expressions. |
paul@38 | 111 | |
paul@38 | 112 | syntax = {} |
paul@42 | 113 | syntax.update(MoinParser.syntax) |
paul@38 | 114 | syntax.update({ |
paul@38 | 115 | # At start of line: |
paul@55 | 116 | |
paul@311 | 117 | "rowsep" : join((group("leading", r"\s*"), # ws... (optional) |
paul@311 | 118 | "^==", # == |
paul@311 | 119 | excl(r".*==\s*?$"), # not-heading |
paul@311 | 120 | group("padding", r"\N*\n?"))), # ws-excl-nl... nl (optional) |
paul@55 | 121 | |
paul@72 | 122 | "continuation" : group("feature", |
paul@311 | 123 | join((group("leading", r"\s*"), # ws... (optional) |
paul@305 | 124 | "^", |
paul@311 | 125 | group("indent", r"\s*"), # ws... (optional) |
paul@311 | 126 | r"\.\.", # .. |
paul@311 | 127 | excl(r"\."), # not-. |
paul@311 | 128 | group("padding", r"\N*")))), # ws-excl-nl... (optional) |
paul@38 | 129 | |
paul@38 | 130 | # Within text: |
paul@55 | 131 | |
paul@311 | 132 | "columnsep" : join((group("leading", r"\s*"), # ws... (optional) |
paul@311 | 133 | r"\|\|", # || |
paul@311 | 134 | excl(r"\|"), # not-| |
paul@311 | 135 | group("padding", r"\N*\n?"))), # ws-excl-nl... nl (optional) |
paul@38 | 136 | }) |
paul@38 | 137 | |
paul@38 | 138 | patterns = get_patterns(syntax) |
paul@38 | 139 | |
paul@38 | 140 | |
paul@38 | 141 | |
paul@38 | 142 | # Pattern details. |
paul@38 | 143 | |
paul@98 | 144 | region_pattern_names = [ |
paul@102 | 145 | "columnsep", "continuation", "rowsep", "tableattrs", |
paul@73 | 146 | ] + MoinParser.region_without_table_pattern_names |
paul@38 | 147 | |
paul@38 | 148 | |
paul@38 | 149 | |
paul@38 | 150 | # Pattern handlers. |
paul@38 | 151 | |
paul@98 | 152 | end_region = MoinParser.end_region |
paul@98 | 153 | parse_table_end = MoinParser.parse_region_end |
paul@98 | 154 | |
paul@38 | 155 | handlers = {} |
paul@42 | 156 | handlers.update(MoinParser.handlers) |
paul@38 | 157 | handlers.update({ |
paul@98 | 158 | "columnsep" : end_region, |
paul@38 | 159 | "continuation" : parse_continuation, |
paul@98 | 160 | "rowsep" : end_region, |
paul@38 | 161 | "regionend" : parse_table_end, |
paul@308 | 162 | "tableattrs" : parse_table_attrs, |
paul@38 | 163 | }) |
paul@38 | 164 | |
paul@40 | 165 | parser = TableParser |
paul@40 | 166 | |
paul@38 | 167 | # vim: tabstop=4 expandtab shiftwidth=4 |