MoinLight

Annotated moinformat/parsers/table.py

73:727abfd0f8ba
2018-07-17 Paul Boddie Reordered table patterns to allow the continuation pattern to match before the listitem_dot pattern.
paul@38 1
#!/usr/bin/env python
paul@38 2
paul@38 3
"""
paul@38 4
Moin wiki table parser.
paul@38 5
paul@54 6
Copyright (C) 2017, 2018 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@55 22
from moinformat.parsers.common import get_patterns, \
paul@55 23
                                      excl, expect, group
paul@42 24
from moinformat.parsers.moin import MoinParser
paul@38 25
from moinformat.tree import Table, TableAttrs, TableCell, TableRow, Text
paul@38 26
paul@55 27
join = "".join
paul@38 28
paul@38 29
# Parser functionality.
paul@38 30
paul@42 31
class TableParser(MoinParser):
paul@38 32
paul@38 33
    "A parser for improved table syntax."
paul@38 34
paul@38 35
    # Principal parser methods.
paul@38 36
paul@38 37
    def parse_region_content(self, items, region):
paul@38 38
paul@38 39
        "Parse the data provided by 'items' to populate the given 'region'."
paul@38 40
paul@38 41
        self.set_region(items, region)
paul@38 42
        self.parse_table_region()
paul@38 43
paul@38 44
    def parse_table_region(self):
paul@38 45
paul@38 46
        # Start to populate table rows.
paul@38 47
paul@38 48
        cell = TableCell([])
paul@38 49
        row = TableRow([cell])
paul@38 50
        table = Table([row])
paul@43 51
        self.append_node(self.region, table)
paul@38 52
paul@38 53
        while True:
paul@38 54
            self.parse_region_details(cell, self.table_region_pattern_names)
paul@38 55
paul@38 56
            # Detect the end of the table.
paul@38 57
paul@54 58
            pattern = self.matching_pattern()
paul@54 59
paul@54 60
            if pattern == "regionend":
paul@38 61
                break
paul@38 62
paul@54 63
            elif pattern == "columnsep":
paul@38 64
                cell = TableCell([])
paul@38 65
                row.append(cell)
paul@38 66
paul@54 67
            elif pattern == "rowsep":
paul@38 68
                row = TableRow([])
paul@38 69
                table.append(row)
paul@38 70
                cell = TableCell([])
paul@38 71
                row.append(cell)
paul@38 72
paul@38 73
    # Parser handler methods.
paul@38 74
paul@38 75
    def parse_continuation(self, cell):
paul@38 76
        pass
paul@38 77
paul@38 78
    def parse_table_end(self, cell):
paul@38 79
paul@38 80
        "Handle the end of a region within 'cell'."
paul@38 81
paul@67 82
        level = self.match_group("level")
paul@67 83
        feature = self.match_group("feature")
paul@67 84
        self.region.extra = self.match_group("extra")
paul@67 85
paul@67 86
        if self.region.have_end(level):
paul@38 87
            raise StopIteration
paul@38 88
        else:
paul@38 89
            cell.append_inline(Text(feature))
paul@38 90
paul@38 91
    # Regular expressions.
paul@38 92
paul@38 93
    syntax = {}
paul@42 94
    syntax.update(MoinParser.syntax)
paul@38 95
    syntax.update({
paul@38 96
        # At start of line:
paul@55 97
paul@55 98
        "rowsep"        : join(("^==",                      # ==
paul@55 99
                                excl(r".*==\s*?$"),         # not-heading
paul@55 100
                                expect(r"\N*?"))),          # ws-excl-nl
paul@55 101
paul@55 102
        "continuation"  : join(("^",
paul@55 103
                                group("indent", r"\N*"),    # ws... (optional)
paul@55 104
                                r"\.\.",                    # ..
paul@55 105
                                excl(r"\."),                # not-.
paul@55 106
                                expect(r"\N"))),            # ws
paul@38 107
paul@38 108
        # Within text:
paul@55 109
paul@55 110
        "columnsep"     : join((r"\|\|",                    # ||
paul@55 111
                                excl(r"\|"),                # not-|
paul@55 112
                                expect(r"\N"))),            # ws
paul@38 113
        })
paul@38 114
paul@38 115
    patterns = get_patterns(syntax)
paul@38 116
paul@38 117
paul@38 118
paul@38 119
    # Pattern details.
paul@38 120
paul@73 121
    table_region_pattern_names = [
paul@55 122
        "columnsep", "continuation", "rowsep",
paul@73 123
        ] + MoinParser.region_without_table_pattern_names
paul@38 124
paul@38 125
paul@38 126
paul@38 127
    # Pattern handlers.
paul@38 128
paul@38 129
    handlers = {}
paul@42 130
    handlers.update(MoinParser.handlers)
paul@38 131
    handlers.update({
paul@42 132
        "columnsep" : MoinParser.end_region,
paul@38 133
        "continuation" : parse_continuation,
paul@42 134
        "rowsep" : MoinParser.end_region,
paul@38 135
        "regionend" : parse_table_end,
paul@38 136
        })
paul@38 137
paul@40 138
parser = TableParser
paul@40 139
paul@38 140
# vim: tabstop=4 expandtab shiftwidth=4