MoinLight

Annotated moinformat/serialisers/moin/table.py

305:8a1de3e6b7ef
2021-10-09 Paul Boddie Incorporate whitespace into table parser features to avoid spurious line breaks being introduced, although any initial blank lines in a region will still cause breaks to appear at that point.
paul@41 1
#!/usr/bin/env python
paul@41 2
paul@41 3
"""
paul@41 4
Moin wiki table serialiser.
paul@41 5
paul@301 6
Copyright (C) 2017, 2018, 2021 Paul Boddie <paul@boddie.org.uk>
paul@41 7
paul@41 8
This program is free software; you can redistribute it and/or modify it under
paul@41 9
the terms of the GNU General Public License as published by the Free Software
paul@41 10
Foundation; either version 3 of the License, or (at your option) any later
paul@41 11
version.
paul@41 12
paul@41 13
This program is distributed in the hope that it will be useful, but WITHOUT
paul@41 14
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
paul@41 15
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
paul@41 16
details.
paul@41 17
paul@41 18
You should have received a copy of the GNU General Public License along with
paul@41 19
this program.  If not, see <http://www.gnu.org/licenses/>.
paul@41 20
"""
paul@41 21
paul@85 22
from moinformat.serialisers.moin.moin import MoinSerialiser
paul@41 23
paul@85 24
class MoinTableSerialiser(MoinSerialiser):
paul@41 25
paul@41 26
    "Serialisation of the page."
paul@41 27
paul@301 28
    input_formats = ["table"]
paul@301 29
paul@41 30
    def init(self):
paul@41 31
        self.first_cell = False
paul@41 32
        self.first_row = False
paul@41 33
paul@41 34
    def start_table(self):
paul@41 35
        self.first_row = True
paul@41 36
paul@305 37
    def start_table_cell(self, attrs, leading, padding):
paul@41 38
        if not self.first_cell:
paul@305 39
            self.out(leading)
paul@41 40
            self.out("||")
paul@305 41
            self.out(padding)
paul@41 42
        else:
paul@41 43
            self.first_cell = False
paul@41 44
paul@305 45
    def start_table_row(self, leading, padding):
paul@41 46
        self.first_cell = True
paul@41 47
        if not self.first_row:
paul@305 48
            self.out(leading)
paul@41 49
            self.out("==")
paul@305 50
            self.out(padding)
paul@41 51
        else:
paul@41 52
            self.first_row = False
paul@41 53
paul@41 54
    def end_table_row(self, trailing):
paul@41 55
        self.out(trailing)
paul@41 56
paul@149 57
    def continuation(self, text):
paul@149 58
        self.out(text)
paul@149 59
paul@85 60
serialiser = MoinTableSerialiser
paul@41 61
paul@41 62
# vim: tabstop=4 expandtab shiftwidth=4