paul@11 | 1 | #!/usr/bin/env python |
paul@11 | 2 | |
paul@11 | 3 | """ |
paul@11 | 4 | Moin wiki serialisers. |
paul@11 | 5 | |
paul@11 | 6 | Copyright (C) 2017 Paul Boddie <paul@boddie.org.uk> |
paul@11 | 7 | |
paul@11 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@11 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@11 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@11 | 11 | version. |
paul@11 | 12 | |
paul@11 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@11 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@11 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@11 | 16 | details. |
paul@11 | 17 | |
paul@11 | 18 | You should have received a copy of the GNU General Public License along with |
paul@11 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@11 | 20 | """ |
paul@11 | 21 | |
paul@11 | 22 | from cgi import escape |
paul@11 | 23 | |
paul@11 | 24 | class Serialiser: |
paul@11 | 25 | |
paul@11 | 26 | "General serialisation support." |
paul@11 | 27 | |
paul@11 | 28 | def __init__(self, out): |
paul@11 | 29 | self.out = out |
paul@11 | 30 | |
paul@11 | 31 | class MoinSerialiser(Serialiser): |
paul@11 | 32 | |
paul@11 | 33 | "Serialisation of the page." |
paul@11 | 34 | |
paul@11 | 35 | def start_region(self, level, indent, type): |
paul@11 | 36 | out = self.out |
paul@11 | 37 | if level: |
paul@11 | 38 | out(" " * indent + "{" * level) |
paul@11 | 39 | if type and level: |
paul@11 | 40 | out("#!%s\n" % type) |
paul@11 | 41 | |
paul@11 | 42 | def end_region(self, level, indent, type): |
paul@11 | 43 | out = self.out |
paul@11 | 44 | if level: |
paul@11 | 45 | out("}" * level) |
paul@11 | 46 | |
paul@15 | 47 | def start_block(self): |
paul@11 | 48 | pass |
paul@11 | 49 | |
paul@15 | 50 | def end_block(self): |
paul@15 | 51 | pass |
paul@11 | 52 | |
paul@13 | 53 | def start_heading(self, level, extra, pad): |
paul@13 | 54 | self.out(extra + "=" * level + pad) |
paul@13 | 55 | |
paul@13 | 56 | def end_heading(self, level, pad, extra): |
paul@13 | 57 | self.out(pad + "=" * level + extra) |
paul@13 | 58 | |
paul@14 | 59 | def start_listitem(self, indent, marker, space): |
paul@14 | 60 | self.out("%s%s%s" % (indent * " ", marker, space)) |
paul@11 | 61 | |
paul@14 | 62 | def end_listitem(self, indent, marker): |
paul@11 | 63 | pass |
paul@11 | 64 | |
paul@15 | 65 | def break_(self): |
paul@15 | 66 | self.out("\n") |
paul@15 | 67 | |
paul@12 | 68 | def rule(self, length): |
paul@12 | 69 | self.out("-" * length) |
paul@12 | 70 | |
paul@11 | 71 | def text(self, s): |
paul@11 | 72 | self.out(s) |
paul@11 | 73 | |
paul@11 | 74 | class HTMLSerialiser(Serialiser): |
paul@11 | 75 | |
paul@11 | 76 | "Serialisation of the page." |
paul@11 | 77 | |
paul@11 | 78 | def start_region(self, level, indent, type): |
paul@11 | 79 | l = [] |
paul@11 | 80 | out = l.append |
paul@11 | 81 | if level: |
paul@11 | 82 | out("level-%d" % level) |
paul@11 | 83 | |
paul@11 | 84 | if indent: |
paul@11 | 85 | out("indent-%d" % indent) |
paul@11 | 86 | |
paul@11 | 87 | # NOTE: Encode type details for CSS. |
paul@11 | 88 | |
paul@11 | 89 | if type: |
paul@11 | 90 | out("type-%s" % escape(type, True)) |
paul@11 | 91 | |
paul@11 | 92 | self.out("<span class='%s'>" % " ".join(l)) |
paul@11 | 93 | |
paul@11 | 94 | def end_region(self, level, indent, type): |
paul@11 | 95 | self.out("</span>") |
paul@11 | 96 | |
paul@15 | 97 | def start_block(self): |
paul@11 | 98 | self.out("<p>") |
paul@11 | 99 | |
paul@15 | 100 | def end_block(self): |
paul@11 | 101 | self.out("</p>") |
paul@11 | 102 | |
paul@13 | 103 | def start_heading(self, level, extra, pad): |
paul@13 | 104 | self.out("<h%d>" % level) |
paul@13 | 105 | |
paul@13 | 106 | def end_heading(self, level, pad, extra): |
paul@13 | 107 | self.out("</h%d>" % level) |
paul@13 | 108 | |
paul@14 | 109 | def start_listitem(self, indent, marker, space): |
paul@11 | 110 | self.out("<li>") |
paul@11 | 111 | |
paul@14 | 112 | def end_listitem(self, indent, marker): |
paul@11 | 113 | self.out("</li>") |
paul@11 | 114 | |
paul@15 | 115 | def break_(self): |
paul@15 | 116 | pass |
paul@15 | 117 | |
paul@12 | 118 | def rule(self, length): |
paul@12 | 119 | self.out("<hr style='height: %dpt' />" % min(length, 10)) |
paul@12 | 120 | |
paul@11 | 121 | def text(self, s): |
paul@11 | 122 | self.out(escape(s)) |
paul@11 | 123 | |
paul@11 | 124 | # Top-level functions. |
paul@11 | 125 | |
paul@11 | 126 | def serialise(doc, serialiser=MoinSerialiser): |
paul@11 | 127 | l = [] |
paul@11 | 128 | doc.to_string(serialiser(l.append)) |
paul@11 | 129 | return "".join(l) |
paul@11 | 130 | |
paul@11 | 131 | # vim: tabstop=4 expandtab shiftwidth=4 |