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