MoinLight

Annotated moinformat/serialisers/common.py

143:a20caaa18295
2018-08-06 Paul Boddie Add the pagename as an attribute on serialiser instances.
paul@38 1
#!/usr/bin/env python
paul@38 2
paul@38 3
"""
paul@38 4
Moin serialiser support.
paul@38 5
paul@46 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@38 22
class Serialiser:
paul@38 23
paul@38 24
    "General serialisation support."
paul@38 25
paul@85 26
    format = None # defined by subclasses
paul@85 27
paul@143 28
    def __init__(self, output, formats=None, linker=None, pagename=None):
paul@46 29
paul@46 30
        """
paul@100 31
        Initialise the serialiser with an 'output' context, an optional
paul@143 32
        'formats' mapping from names to serialiser classes, an optional 'linker'
paul@143 33
        object for translating links, and an optional 'pagename'.
paul@46 34
        """
paul@46 35
paul@100 36
        self.output = output
paul@46 37
        self.formats = formats
paul@94 38
        self.linker = linker
paul@143 39
        self.pagename = pagename
paul@100 40
paul@100 41
        # Initialise a callable for use in serialisation.
paul@100 42
paul@100 43
        self.out = output.out
paul@100 44
paul@100 45
        # Initialisation of any other state.
paul@100 46
paul@39 47
        self.init()
paul@39 48
paul@39 49
    def init(self):
paul@39 50
paul@39 51
        "Initialisation method to be overridden by subclasses."
paul@39 52
paul@39 53
        pass
paul@38 54
paul@46 55
    def __repr__(self):
paul@143 56
        return "%s(%r, %r, %r, %r)" % (self.__class__.__name__, self.output,
paul@143 57
                                       self.formats, self.linker, self.pagename)
paul@46 58
paul@100 59
    def get_serialiser(self, format):
paul@100 60
paul@100 61
        """
paul@100 62
        Return a serialiser for the given 'format'. Return self if no suitable
paul@100 63
        serialiser can be obtained.
paul@100 64
        """
paul@100 65
paul@100 66
        cls = self.formats and self.formats.get(format)
paul@100 67
        if cls:
paul@100 68
            return self.instantiate(cls)
paul@100 69
        else:
paul@100 70
            return self
paul@100 71
paul@100 72
    def get_output(self):
paul@100 73
paul@100 74
        "Return the output as a string."
paul@100 75
paul@100 76
        return self.output.to_string()
paul@100 77
paul@100 78
    def instantiate(self, cls):
paul@100 79
paul@100 80
        """
paul@100 81
        Instantiate 'cls' and return the result if 'cls' is a different class to
paul@100 82
        this instance. Otherwise, return this instance.
paul@100 83
        """
paul@100 84
paul@100 85
        if cls is self.__class__:
paul@100 86
            return self
paul@100 87
        else:
paul@143 88
            return cls(self.output, self.formats, self.linker, self.pagename)
paul@100 89
paul@38 90
def escape_attr(s):
paul@38 91
paul@38 92
    "Escape XML document attribute."
paul@38 93
paul@38 94
    return escape_text(s).replace("'", "&apos;").replace('"', "&quot;")
paul@38 95
paul@38 96
def escape_text(s):
paul@38 97
paul@38 98
    "Escape XML document text."
paul@38 99
paul@38 100
    return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
paul@38 101
paul@38 102
# vim: tabstop=4 expandtab shiftwidth=4