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