1.1 --- a/moinformat/serialisers/__init__.py Fri Jul 13 16:59:27 2018 +0200
1.2 +++ b/moinformat/serialisers/__init__.py Fri Jul 13 18:53:55 2018 +0200
1.3 @@ -3,7 +3,7 @@
1.4 """
1.5 Moin wiki serialisers.
1.6
1.7 -Copyright (C) 2017 Paul Boddie <paul@boddie.org.uk>
1.8 +Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk>
1.9
1.10 This program is free software; you can redistribute it and/or modify it under
1.11 the terms of the GNU General Public License as published by the Free Software
1.12 @@ -25,8 +25,15 @@
1.13 # Top-level functions.
1.14
1.15 def serialise(doc, serialiser=MoinSerialiser):
1.16 +
1.17 + "Serialise 'doc' using 'serialiser' or the Moin serialiser if omitted."
1.18 +
1.19 + # Permit serialisation back to source form if Moin format is used.
1.20 +
1.21 + formats = serialiser is MoinSerialiser and serialisers or None
1.22 +
1.23 l = []
1.24 - doc.to_string(serialiser(l.append))
1.25 + doc.to_string(serialiser(l.append, formats))
1.26 return "".join(l)
1.27
1.28 # vim: tabstop=4 expandtab shiftwidth=4
2.1 --- a/moinformat/serialisers/common.py Fri Jul 13 16:59:27 2018 +0200
2.2 +++ b/moinformat/serialisers/common.py Fri Jul 13 18:53:55 2018 +0200
2.3 @@ -3,7 +3,7 @@
2.4 """
2.5 Moin serialiser support.
2.6
2.7 -Copyright (C) 2017 Paul Boddie <paul@boddie.org.uk>
2.8 +Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk>
2.9
2.10 This program is free software; you can redistribute it and/or modify it under
2.11 the terms of the GNU General Public License as published by the Free Software
2.12 @@ -23,8 +23,15 @@
2.13
2.14 "General serialisation support."
2.15
2.16 - def __init__(self, out):
2.17 + def __init__(self, out, formats=None):
2.18 +
2.19 + """
2.20 + Initialise the serialiser with an 'out' callable and an optional
2.21 + 'formats' mapping from names to serialiser classes.
2.22 + """
2.23 +
2.24 self.out = out
2.25 + self.formats = formats
2.26 self.init()
2.27
2.28 def init(self):
2.29 @@ -33,6 +40,9 @@
2.30
2.31 pass
2.32
2.33 + def __repr__(self):
2.34 + return "%s(%r, %r)" % (self.__class__.__name__, self.out, self.formats)
2.35 +
2.36 def escape_attr(s):
2.37
2.38 "Escape XML document attribute."
3.1 --- a/moinformat/tree.py Fri Jul 13 16:59:27 2018 +0200
3.2 +++ b/moinformat/tree.py Fri Jul 13 18:53:55 2018 +0200
3.3 @@ -3,7 +3,7 @@
3.4 """
3.5 Moin wiki format document tree nodes.
3.6
3.7 -Copyright (C) 2017 Paul Boddie <paul@boddie.org.uk>
3.8 +Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk>
3.9
3.10 This program is free software; you can redistribute it and/or modify it under
3.11 the terms of the GNU General Public License as published by the Free Software
3.12 @@ -19,8 +19,6 @@
3.13 this program. If not, see <http://www.gnu.org/licenses/>.
3.14 """
3.15
3.16 -from moinformat.serialisers import serialisers
3.17 -
3.18 class Container:
3.19
3.20 "A container of document nodes."
3.21 @@ -132,10 +130,13 @@
3.22 def to_string(self, out):
3.23 out.start_region(self.level, self.indent, self.type)
3.24
3.25 - # Obtain a serialiser for the region and serialise.
3.26 + # Obtain a serialiser for the region, if appropriate.
3.27
3.28 - region_out = serialisers.get(self.type)
3.29 - region_out = region_out and region_out(out.out) or out
3.30 + serialiser = out.formats and out.formats.get(self.type)
3.31 + region_out = serialiser and serialiser(out.out) or out
3.32 +
3.33 + # Serialise the region.
3.34 +
3.35 self._to_string(region_out)
3.36
3.37 out.end_region(self.level, self.indent, self.type)