MoinLight

Annotated moinformat/serialisers/common.py

176:47af441b48bf
2018-11-26 Paul Boddie Support linking to stylesheets based on the collection of available files.
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@165 28
    def __init__(self, metadata, serialisers=None):
paul@46 29
paul@46 30
        """
paul@165 31
        Initialise the serialiser with the given 'metadata' and optional
paul@165 32
        'serialisers'.
paul@46 33
        """
paul@46 34
paul@165 35
        self.metadata = metadata
paul@165 36
        self.serialisers = serialisers
paul@165 37
paul@165 38
        # Obtain essential metadata.
paul@165 39
paul@165 40
        self.output = metadata.get_output()
paul@165 41
        self.linker = metadata.get_linker()
paul@100 42
paul@100 43
        # Initialise a callable for use in serialisation.
paul@100 44
paul@165 45
        self.out = self.output.out
paul@100 46
paul@100 47
        # Initialisation of any other state.
paul@100 48
paul@39 49
        self.init()
paul@39 50
paul@39 51
    def init(self):
paul@39 52
paul@39 53
        "Initialisation method to be overridden by subclasses."
paul@39 54
paul@39 55
        pass
paul@38 56
paul@46 57
    def __repr__(self):
paul@165 58
        return "%s(%r)" % (self.__class__.__name__, self.metadata)
paul@165 59
paul@165 60
    def reset(self):
paul@165 61
        self.output.reset()
paul@46 62
paul@100 63
    def get_serialiser(self, format):
paul@100 64
paul@100 65
        """
paul@100 66
        Return a serialiser for the given 'format'. Return self if no suitable
paul@100 67
        serialiser can be obtained.
paul@100 68
        """
paul@100 69
paul@165 70
        cls = self.serialisers.get(format)
paul@100 71
        if cls:
paul@100 72
            return self.instantiate(cls)
paul@100 73
        else:
paul@100 74
            return self
paul@100 75
paul@100 76
    def get_output(self):
paul@100 77
paul@100 78
        "Return the output as a string."
paul@100 79
paul@100 80
        return self.output.to_string()
paul@100 81
paul@100 82
    def instantiate(self, cls):
paul@100 83
paul@100 84
        """
paul@100 85
        Instantiate 'cls' and return the result if 'cls' is a different class to
paul@100 86
        this instance. Otherwise, return this instance.
paul@100 87
        """
paul@100 88
paul@100 89
        if cls is self.__class__:
paul@100 90
            return self
paul@100 91
        else:
paul@165 92
            return cls(self.metadata, self.serialisers)
paul@100 93
paul@38 94
def escape_attr(s):
paul@38 95
paul@38 96
    "Escape XML document attribute."
paul@38 97
paul@38 98
    return escape_text(s).replace("'", "&apos;").replace('"', "&quot;")
paul@38 99
paul@38 100
def escape_text(s):
paul@38 101
paul@38 102
    "Escape XML document text."
paul@38 103
paul@38 104
    return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
paul@38 105
paul@38 106
# vim: tabstop=4 expandtab shiftwidth=4