paul@38 | 1 | #!/usr/bin/env python |
paul@38 | 2 | |
paul@38 | 3 | """ |
paul@38 | 4 | Moin serialiser support. |
paul@38 | 5 | |
paul@38 | 6 | Copyright (C) 2017 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@38 | 26 | def __init__(self, out): |
paul@38 | 27 | self.out = out |
paul@39 | 28 | self.init() |
paul@39 | 29 | |
paul@39 | 30 | def init(self): |
paul@39 | 31 | |
paul@39 | 32 | "Initialisation method to be overridden by subclasses." |
paul@39 | 33 | |
paul@39 | 34 | pass |
paul@38 | 35 | |
paul@38 | 36 | def escape_attr(s): |
paul@38 | 37 | |
paul@38 | 38 | "Escape XML document attribute." |
paul@38 | 39 | |
paul@38 | 40 | return escape_text(s).replace("'", "'").replace('"', """) |
paul@38 | 41 | |
paul@38 | 42 | def escape_text(s): |
paul@38 | 43 | |
paul@38 | 44 | "Escape XML document text." |
paul@38 | 45 | |
paul@38 | 46 | return s.replace("&", "&").replace("<", "<").replace(">", ">") |
paul@38 | 47 | |
paul@38 | 48 | # vim: tabstop=4 expandtab shiftwidth=4 |