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@16 | 53 | def start_defitem(self, pad, extra): |
paul@16 | 54 | self.out((extra and "\n" + extra + "::" or "") + pad) |
paul@16 | 55 | |
paul@16 | 56 | def end_defitem(self, pad, extra): |
paul@16 | 57 | pass |
paul@16 | 58 | |
paul@16 | 59 | def start_defterm(self, pad): |
paul@16 | 60 | self.out(pad) |
paul@16 | 61 | |
paul@16 | 62 | def end_defterm(self, pad): |
paul@16 | 63 | self.out("::") |
paul@16 | 64 | |
paul@17 | 65 | def start_emphasis(self): |
paul@17 | 66 | self.out("''") |
paul@17 | 67 | |
paul@17 | 68 | def end_emphasis(self): |
paul@17 | 69 | self.out("''") |
paul@17 | 70 | |
paul@13 | 71 | def start_heading(self, level, extra, pad): |
paul@13 | 72 | self.out(extra + "=" * level + pad) |
paul@13 | 73 | |
paul@13 | 74 | def end_heading(self, level, pad, extra): |
paul@13 | 75 | self.out(pad + "=" * level + extra) |
paul@13 | 76 | |
paul@14 | 77 | def start_listitem(self, indent, marker, space): |
paul@14 | 78 | self.out("%s%s%s" % (indent * " ", marker, space)) |
paul@11 | 79 | |
paul@14 | 80 | def end_listitem(self, indent, marker): |
paul@11 | 81 | pass |
paul@11 | 82 | |
paul@18 | 83 | def start_monospace(self): |
paul@18 | 84 | self.out("`") |
paul@18 | 85 | |
paul@18 | 86 | def end_monospace(self): |
paul@18 | 87 | self.out("`") |
paul@18 | 88 | |
paul@17 | 89 | def start_strong(self): |
paul@17 | 90 | self.out("'''") |
paul@17 | 91 | |
paul@17 | 92 | def end_strong(self): |
paul@17 | 93 | self.out("'''") |
paul@17 | 94 | |
paul@20 | 95 | def start_subscript(self): |
paul@20 | 96 | self.out(",,") |
paul@20 | 97 | |
paul@20 | 98 | def end_subscript(self): |
paul@20 | 99 | self.out(",,") |
paul@20 | 100 | |
paul@20 | 101 | def start_superscript(self): |
paul@20 | 102 | self.out("^") |
paul@20 | 103 | |
paul@20 | 104 | def end_superscript(self): |
paul@20 | 105 | self.out("^") |
paul@20 | 106 | |
paul@19 | 107 | def start_underline(self): |
paul@19 | 108 | self.out("__") |
paul@19 | 109 | |
paul@19 | 110 | def end_underline(self): |
paul@19 | 111 | self.out("__") |
paul@19 | 112 | |
paul@15 | 113 | def break_(self): |
paul@15 | 114 | self.out("\n") |
paul@15 | 115 | |
paul@12 | 116 | def rule(self, length): |
paul@12 | 117 | self.out("-" * length) |
paul@12 | 118 | |
paul@11 | 119 | def text(self, s): |
paul@11 | 120 | self.out(s) |
paul@11 | 121 | |
paul@11 | 122 | class HTMLSerialiser(Serialiser): |
paul@11 | 123 | |
paul@11 | 124 | "Serialisation of the page." |
paul@11 | 125 | |
paul@11 | 126 | def start_region(self, level, indent, type): |
paul@11 | 127 | l = [] |
paul@11 | 128 | out = l.append |
paul@11 | 129 | if level: |
paul@11 | 130 | out("level-%d" % level) |
paul@11 | 131 | |
paul@11 | 132 | if indent: |
paul@11 | 133 | out("indent-%d" % indent) |
paul@11 | 134 | |
paul@11 | 135 | # NOTE: Encode type details for CSS. |
paul@11 | 136 | |
paul@11 | 137 | if type: |
paul@11 | 138 | out("type-%s" % escape(type, True)) |
paul@11 | 139 | |
paul@11 | 140 | self.out("<span class='%s'>" % " ".join(l)) |
paul@11 | 141 | |
paul@11 | 142 | def end_region(self, level, indent, type): |
paul@11 | 143 | self.out("</span>") |
paul@11 | 144 | |
paul@15 | 145 | def start_block(self): |
paul@11 | 146 | self.out("<p>") |
paul@11 | 147 | |
paul@15 | 148 | def end_block(self): |
paul@11 | 149 | self.out("</p>") |
paul@11 | 150 | |
paul@16 | 151 | def start_defitem(self, pad, extra): |
paul@16 | 152 | self.out("<dd>") |
paul@16 | 153 | |
paul@16 | 154 | def end_defitem(self, pad, extra): |
paul@16 | 155 | self.out("</dd>") |
paul@16 | 156 | |
paul@16 | 157 | def start_defterm(self, pad): |
paul@16 | 158 | self.out("<dt>") |
paul@16 | 159 | |
paul@16 | 160 | def end_defterm(self, pad): |
paul@16 | 161 | self.out("</dt>") |
paul@16 | 162 | |
paul@17 | 163 | def start_emphasis(self): |
paul@17 | 164 | self.out("<em>") |
paul@17 | 165 | |
paul@17 | 166 | def end_emphasis(self): |
paul@17 | 167 | self.out("</em>") |
paul@17 | 168 | |
paul@13 | 169 | def start_heading(self, level, extra, pad): |
paul@13 | 170 | self.out("<h%d>" % level) |
paul@13 | 171 | |
paul@13 | 172 | def end_heading(self, level, pad, extra): |
paul@13 | 173 | self.out("</h%d>" % level) |
paul@13 | 174 | |
paul@14 | 175 | def start_listitem(self, indent, marker, space): |
paul@11 | 176 | self.out("<li>") |
paul@11 | 177 | |
paul@14 | 178 | def end_listitem(self, indent, marker): |
paul@11 | 179 | self.out("</li>") |
paul@11 | 180 | |
paul@18 | 181 | def start_monospace(self): |
paul@18 | 182 | self.out("<tt>") |
paul@18 | 183 | |
paul@18 | 184 | def end_monospace(self): |
paul@18 | 185 | self.out("</tt>") |
paul@18 | 186 | |
paul@17 | 187 | def start_strong(self): |
paul@17 | 188 | self.out("<strong>") |
paul@17 | 189 | |
paul@17 | 190 | def end_strong(self): |
paul@17 | 191 | self.out("</strong>") |
paul@17 | 192 | |
paul@20 | 193 | def start_subscript(self): |
paul@20 | 194 | self.out("<sub>") |
paul@20 | 195 | |
paul@20 | 196 | def end_subscript(self): |
paul@20 | 197 | self.out("</sub>") |
paul@20 | 198 | |
paul@20 | 199 | def start_superscript(self): |
paul@20 | 200 | self.out("<sup>") |
paul@20 | 201 | |
paul@20 | 202 | def end_superscript(self): |
paul@20 | 203 | self.out("</sup>") |
paul@20 | 204 | |
paul@19 | 205 | def start_underline(self): |
paul@19 | 206 | self.out("<span style='text-decoration: underline'>") |
paul@19 | 207 | |
paul@19 | 208 | def end_underline(self): |
paul@19 | 209 | self.out("</span>") |
paul@19 | 210 | |
paul@15 | 211 | def break_(self): |
paul@15 | 212 | pass |
paul@15 | 213 | |
paul@12 | 214 | def rule(self, length): |
paul@12 | 215 | self.out("<hr style='height: %dpt' />" % min(length, 10)) |
paul@12 | 216 | |
paul@11 | 217 | def text(self, s): |
paul@11 | 218 | self.out(escape(s)) |
paul@11 | 219 | |
paul@11 | 220 | # Top-level functions. |
paul@11 | 221 | |
paul@11 | 222 | def serialise(doc, serialiser=MoinSerialiser): |
paul@11 | 223 | l = [] |
paul@11 | 224 | doc.to_string(serialiser(l.append)) |
paul@11 | 225 | return "".join(l) |
paul@11 | 226 | |
paul@11 | 227 | # vim: tabstop=4 expandtab shiftwidth=4 |