# HG changeset patch # User Paul Boddie # Date 1513115600 -3600 # Node ID f753f631d055e00dc047f55821612231ca9c97b1 # Parent bc22df23b35e03d7498d855c7f25489216d6beae Added serialiser switching for regions of different types, adding table region serialisation support and a test of such regions. diff -r bc22df23b35e -r f753f631d055 moinformat/serialisers/table.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/moinformat/serialisers/table.py Tue Dec 12 22:53:20 2017 +0100 @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +""" +Moin wiki table serialiser. + +Copyright (C) 2017 Paul Boddie + +This program is free software; you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation; either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +""" + +from moinformat.serialisers.moin import MoinSerialiser + +class TableSerialiser(MoinSerialiser): + + "Serialisation of the page." + + def init(self): + self.first_cell = False + self.first_row = False + + def start_table(self): + self.first_row = True + + def start_table_cell(self, attrs): + if not self.first_cell: + self.out("||") + else: + self.first_cell = False + + if attrs and not attrs.empty(): + attrs.to_string(self) + + def start_table_row(self): + self.first_cell = True + if not self.first_row: + self.out("==") + else: + self.first_row = False + + def end_table_row(self, trailing): + self.out(trailing) + +serialiser = TableSerialiser + +# vim: tabstop=4 expandtab shiftwidth=4 diff -r bc22df23b35e -r f753f631d055 moinformat/tree.py --- a/moinformat/tree.py Tue Dec 12 21:08:53 2017 +0100 +++ b/moinformat/tree.py Tue Dec 12 22:53:20 2017 +0100 @@ -19,6 +19,8 @@ this program. If not, see . """ +from moinformat.serialisers import serialisers + class Container: "A container of document nodes." @@ -129,7 +131,13 @@ def to_string(self, out): out.start_region(self.level, self.indent, self.type) - self._to_string(out) + + # Obtain a serialiser for the region and serialise. + + region_out = serialisers.get(self.type) + region_out = region_out and region_out(out.out) or out + self._to_string(region_out) + out.end_region(self.level, self.indent, self.type) diff -r bc22df23b35e -r f753f631d055 tests/test13.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test13.txt Tue Dec 12 22:53:20 2017 +0100 @@ -0,0 +1,9 @@ +Wiki format + +{{{#!table +'''Cell 1''' || Cell 2 +== +Cell 3 || Cell 4 +}}} + +Wiki format again