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@94 | 28 | def __init__(self, out=None, formats=None, linker=None): |
paul@46 | 29 | |
paul@46 | 30 | """ |
paul@94 | 31 | Initialise the serialiser with an 'out' callable, an optional 'formats' |
paul@94 | 32 | mapping from names to serialiser classes, and an optional 'linker' |
paul@94 | 33 | object for translating links. |
paul@46 | 34 | """ |
paul@46 | 35 | |
paul@38 | 36 | self.out = out |
paul@46 | 37 | self.formats = formats |
paul@94 | 38 | self.linker = linker |
paul@39 | 39 | self.init() |
paul@39 | 40 | |
paul@39 | 41 | def init(self): |
paul@39 | 42 | |
paul@39 | 43 | "Initialisation method to be overridden by subclasses." |
paul@39 | 44 | |
paul@39 | 45 | pass |
paul@38 | 46 | |
paul@46 | 47 | def __repr__(self): |
paul@94 | 48 | return "%s(%r, %r, %r)" % (self.__class__.__name__, self.out, |
paul@94 | 49 | self.formats, self.linker) |
paul@46 | 50 | |
paul@38 | 51 | def escape_attr(s): |
paul@38 | 52 | |
paul@38 | 53 | "Escape XML document attribute." |
paul@38 | 54 | |
paul@38 | 55 | return escape_text(s).replace("'", "'").replace('"', """) |
paul@38 | 56 | |
paul@38 | 57 | def escape_text(s): |
paul@38 | 58 | |
paul@38 | 59 | "Escape XML document text." |
paul@38 | 60 | |
paul@38 | 61 | return s.replace("&", "&").replace("<", "<").replace(">", ">") |
paul@38 | 62 | |
paul@38 | 63 | # vim: tabstop=4 expandtab shiftwidth=4 |