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@11 | 47 | def start_block(self, final): |
paul@11 | 48 | pass |
paul@11 | 49 | |
paul@11 | 50 | def end_block(self, final): |
paul@11 | 51 | if not final: |
paul@11 | 52 | self.out("\n") |
paul@11 | 53 | |
paul@13 | 54 | def start_heading(self, level, extra, pad): |
paul@13 | 55 | self.out(extra + "=" * level + pad) |
paul@13 | 56 | |
paul@13 | 57 | def end_heading(self, level, pad, extra): |
paul@13 | 58 | self.out(pad + "=" * level + extra) |
paul@13 | 59 | |
paul@11 | 60 | def start_listitem(self): |
paul@11 | 61 | self.out(" *") |
paul@11 | 62 | |
paul@11 | 63 | def end_listitem(self): |
paul@11 | 64 | pass |
paul@11 | 65 | |
paul@12 | 66 | def rule(self, length): |
paul@12 | 67 | self.out("-" * length) |
paul@12 | 68 | |
paul@11 | 69 | def text(self, s): |
paul@11 | 70 | self.out(s) |
paul@11 | 71 | |
paul@11 | 72 | class HTMLSerialiser(Serialiser): |
paul@11 | 73 | |
paul@11 | 74 | "Serialisation of the page." |
paul@11 | 75 | |
paul@11 | 76 | def start_region(self, level, indent, type): |
paul@11 | 77 | l = [] |
paul@11 | 78 | out = l.append |
paul@11 | 79 | if level: |
paul@11 | 80 | out("level-%d" % level) |
paul@11 | 81 | |
paul@11 | 82 | if indent: |
paul@11 | 83 | out("indent-%d" % indent) |
paul@11 | 84 | |
paul@11 | 85 | # NOTE: Encode type details for CSS. |
paul@11 | 86 | |
paul@11 | 87 | if type: |
paul@11 | 88 | out("type-%s" % escape(type, True)) |
paul@11 | 89 | |
paul@11 | 90 | self.out("<span class='%s'>" % " ".join(l)) |
paul@11 | 91 | |
paul@11 | 92 | def end_region(self, level, indent, type): |
paul@11 | 93 | self.out("</span>") |
paul@11 | 94 | |
paul@11 | 95 | def start_block(self, final): |
paul@11 | 96 | self.out("<p>") |
paul@11 | 97 | |
paul@11 | 98 | def end_block(self, final): |
paul@11 | 99 | self.out("</p>") |
paul@11 | 100 | |
paul@13 | 101 | def start_heading(self, level, extra, pad): |
paul@13 | 102 | self.out("<h%d>" % level) |
paul@13 | 103 | |
paul@13 | 104 | def end_heading(self, level, pad, extra): |
paul@13 | 105 | self.out("</h%d>" % level) |
paul@13 | 106 | |
paul@11 | 107 | def start_listitem(self): |
paul@11 | 108 | self.out("<li>") |
paul@11 | 109 | |
paul@11 | 110 | def end_listitem(self): |
paul@11 | 111 | self.out("</li>") |
paul@11 | 112 | |
paul@12 | 113 | def rule(self, length): |
paul@12 | 114 | self.out("<hr style='height: %dpt' />" % min(length, 10)) |
paul@12 | 115 | |
paul@11 | 116 | def text(self, s): |
paul@11 | 117 | self.out(escape(s)) |
paul@11 | 118 | |
paul@11 | 119 | # Top-level functions. |
paul@11 | 120 | |
paul@11 | 121 | def serialise(doc, serialiser=MoinSerialiser): |
paul@11 | 122 | l = [] |
paul@11 | 123 | doc.to_string(serialiser(l.append)) |
paul@11 | 124 | return "".join(l) |
paul@11 | 125 | |
paul@11 | 126 | # vim: tabstop=4 expandtab shiftwidth=4 |