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@23 | 77 | def start_larger(self): |
paul@23 | 78 | self.out("~+") |
paul@23 | 79 | |
paul@23 | 80 | def end_larger(self): |
paul@23 | 81 | self.out("+~") |
paul@23 | 82 | |
paul@14 | 83 | def start_listitem(self, indent, marker, space): |
paul@14 | 84 | self.out("%s%s%s" % (indent * " ", marker, space)) |
paul@11 | 85 | |
paul@14 | 86 | def end_listitem(self, indent, marker): |
paul@11 | 87 | pass |
paul@11 | 88 | |
paul@18 | 89 | def start_monospace(self): |
paul@18 | 90 | self.out("`") |
paul@18 | 91 | |
paul@18 | 92 | def end_monospace(self): |
paul@18 | 93 | self.out("`") |
paul@18 | 94 | |
paul@23 | 95 | def start_smaller(self): |
paul@23 | 96 | self.out("~-") |
paul@23 | 97 | |
paul@23 | 98 | def end_smaller(self): |
paul@23 | 99 | self.out("-~") |
paul@23 | 100 | |
paul@17 | 101 | def start_strong(self): |
paul@17 | 102 | self.out("'''") |
paul@17 | 103 | |
paul@17 | 104 | def end_strong(self): |
paul@17 | 105 | self.out("'''") |
paul@17 | 106 | |
paul@20 | 107 | def start_subscript(self): |
paul@20 | 108 | self.out(",,") |
paul@20 | 109 | |
paul@20 | 110 | def end_subscript(self): |
paul@20 | 111 | self.out(",,") |
paul@20 | 112 | |
paul@20 | 113 | def start_superscript(self): |
paul@20 | 114 | self.out("^") |
paul@20 | 115 | |
paul@20 | 116 | def end_superscript(self): |
paul@20 | 117 | self.out("^") |
paul@20 | 118 | |
paul@24 | 119 | def start_table_cell(self): |
paul@24 | 120 | pass |
paul@24 | 121 | |
paul@24 | 122 | def end_table_cell(self): |
paul@24 | 123 | self.out("||") |
paul@24 | 124 | |
paul@24 | 125 | def start_table_row(self): |
paul@24 | 126 | self.out("||") |
paul@24 | 127 | |
paul@24 | 128 | def end_table_row(self, trailing): |
paul@24 | 129 | self.out(trailing) |
paul@24 | 130 | |
paul@19 | 131 | def start_underline(self): |
paul@19 | 132 | self.out("__") |
paul@19 | 133 | |
paul@19 | 134 | def end_underline(self): |
paul@19 | 135 | self.out("__") |
paul@19 | 136 | |
paul@15 | 137 | def break_(self): |
paul@15 | 138 | self.out("\n") |
paul@15 | 139 | |
paul@12 | 140 | def rule(self, length): |
paul@12 | 141 | self.out("-" * length) |
paul@12 | 142 | |
paul@11 | 143 | def text(self, s): |
paul@11 | 144 | self.out(s) |
paul@11 | 145 | |
paul@11 | 146 | class HTMLSerialiser(Serialiser): |
paul@11 | 147 | |
paul@11 | 148 | "Serialisation of the page." |
paul@11 | 149 | |
paul@11 | 150 | def start_region(self, level, indent, type): |
paul@11 | 151 | l = [] |
paul@11 | 152 | out = l.append |
paul@11 | 153 | if level: |
paul@11 | 154 | out("level-%d" % level) |
paul@11 | 155 | |
paul@11 | 156 | if indent: |
paul@11 | 157 | out("indent-%d" % indent) |
paul@11 | 158 | |
paul@11 | 159 | # NOTE: Encode type details for CSS. |
paul@11 | 160 | |
paul@11 | 161 | if type: |
paul@11 | 162 | out("type-%s" % escape(type, True)) |
paul@11 | 163 | |
paul@11 | 164 | self.out("<span class='%s'>" % " ".join(l)) |
paul@11 | 165 | |
paul@11 | 166 | def end_region(self, level, indent, type): |
paul@11 | 167 | self.out("</span>") |
paul@11 | 168 | |
paul@15 | 169 | def start_block(self): |
paul@11 | 170 | self.out("<p>") |
paul@11 | 171 | |
paul@15 | 172 | def end_block(self): |
paul@11 | 173 | self.out("</p>") |
paul@11 | 174 | |
paul@16 | 175 | def start_defitem(self, pad, extra): |
paul@16 | 176 | self.out("<dd>") |
paul@16 | 177 | |
paul@16 | 178 | def end_defitem(self, pad, extra): |
paul@16 | 179 | self.out("</dd>") |
paul@16 | 180 | |
paul@16 | 181 | def start_defterm(self, pad): |
paul@16 | 182 | self.out("<dt>") |
paul@16 | 183 | |
paul@16 | 184 | def end_defterm(self, pad): |
paul@16 | 185 | self.out("</dt>") |
paul@16 | 186 | |
paul@17 | 187 | def start_emphasis(self): |
paul@17 | 188 | self.out("<em>") |
paul@17 | 189 | |
paul@17 | 190 | def end_emphasis(self): |
paul@17 | 191 | self.out("</em>") |
paul@17 | 192 | |
paul@13 | 193 | def start_heading(self, level, extra, pad): |
paul@13 | 194 | self.out("<h%d>" % level) |
paul@13 | 195 | |
paul@13 | 196 | def end_heading(self, level, pad, extra): |
paul@13 | 197 | self.out("</h%d>" % level) |
paul@13 | 198 | |
paul@23 | 199 | def start_larger(self): |
paul@23 | 200 | self.out("<big>") |
paul@23 | 201 | |
paul@23 | 202 | def end_larger(self): |
paul@23 | 203 | self.out("</big>") |
paul@23 | 204 | |
paul@14 | 205 | def start_listitem(self, indent, marker, space): |
paul@11 | 206 | self.out("<li>") |
paul@11 | 207 | |
paul@14 | 208 | def end_listitem(self, indent, marker): |
paul@11 | 209 | self.out("</li>") |
paul@11 | 210 | |
paul@18 | 211 | def start_monospace(self): |
paul@18 | 212 | self.out("<tt>") |
paul@18 | 213 | |
paul@18 | 214 | def end_monospace(self): |
paul@18 | 215 | self.out("</tt>") |
paul@18 | 216 | |
paul@23 | 217 | def start_smaller(self): |
paul@23 | 218 | self.out("<small>") |
paul@23 | 219 | |
paul@23 | 220 | def end_smaller(self): |
paul@23 | 221 | self.out("</small>") |
paul@23 | 222 | |
paul@17 | 223 | def start_strong(self): |
paul@17 | 224 | self.out("<strong>") |
paul@17 | 225 | |
paul@17 | 226 | def end_strong(self): |
paul@17 | 227 | self.out("</strong>") |
paul@17 | 228 | |
paul@20 | 229 | def start_subscript(self): |
paul@20 | 230 | self.out("<sub>") |
paul@20 | 231 | |
paul@20 | 232 | def end_subscript(self): |
paul@20 | 233 | self.out("</sub>") |
paul@20 | 234 | |
paul@20 | 235 | def start_superscript(self): |
paul@20 | 236 | self.out("<sup>") |
paul@20 | 237 | |
paul@20 | 238 | def end_superscript(self): |
paul@20 | 239 | self.out("</sup>") |
paul@20 | 240 | |
paul@24 | 241 | def start_table_cell(self): |
paul@24 | 242 | self.out("<td>") |
paul@24 | 243 | |
paul@24 | 244 | def end_table_cell(self): |
paul@24 | 245 | self.out("</td>") |
paul@24 | 246 | |
paul@24 | 247 | def start_table_row(self): |
paul@24 | 248 | self.out("<tr>") |
paul@24 | 249 | |
paul@24 | 250 | def end_table_row(self, trailing): |
paul@24 | 251 | self.out("</tr>") |
paul@24 | 252 | |
paul@19 | 253 | def start_underline(self): |
paul@19 | 254 | self.out("<span style='text-decoration: underline'>") |
paul@19 | 255 | |
paul@19 | 256 | def end_underline(self): |
paul@19 | 257 | self.out("</span>") |
paul@19 | 258 | |
paul@15 | 259 | def break_(self): |
paul@15 | 260 | pass |
paul@15 | 261 | |
paul@12 | 262 | def rule(self, length): |
paul@12 | 263 | self.out("<hr style='height: %dpt' />" % min(length, 10)) |
paul@12 | 264 | |
paul@11 | 265 | def text(self, s): |
paul@11 | 266 | self.out(escape(s)) |
paul@11 | 267 | |
paul@11 | 268 | # Top-level functions. |
paul@11 | 269 | |
paul@11 | 270 | def serialise(doc, serialiser=MoinSerialiser): |
paul@11 | 271 | l = [] |
paul@11 | 272 | doc.to_string(serialiser(l.append)) |
paul@11 | 273 | return "".join(l) |
paul@11 | 274 | |
paul@11 | 275 | # vim: tabstop=4 expandtab shiftwidth=4 |