1 #!/usr/bin/env python 2 3 """ 4 Moin wiki text serialiser. 5 6 Copyright (C) 2017, 2018 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 moinformat.serialisers.common import escape_attr, escape_text, Serialiser 23 24 class MoinSerialiser(Serialiser): 25 26 "Serialisation of the page." 27 28 format = "moin" 29 30 def start_region(self, level, indent, type, extra): 31 out = self.out 32 if level: 33 out(" " * indent + "{" * level) 34 35 # Produce a header for regions within a top-level region. 36 37 if type and type != "inline" and level: 38 out("#!%s\n" % type) 39 40 def end_region(self, level, indent, type, extra): 41 out = self.out 42 if level: 43 out("%s%s" % ("}" * level, extra or "")) 44 45 def start_block(self): 46 pass 47 48 def end_block(self): 49 pass 50 51 def start_defitem(self, pad, extra): 52 self.out((extra and "\n" + extra + "::" or "") + pad) 53 54 def end_defitem(self, pad, extra): 55 pass 56 57 def start_defterm(self, pad): 58 self.out(pad) 59 60 def end_defterm(self, pad): 61 self.out("::") 62 63 def start_emphasis(self): 64 self.out("''") 65 66 def end_emphasis(self): 67 self.out("''") 68 69 def start_heading(self, level, extra, pad): 70 self.out(extra + "=" * level + pad) 71 72 def end_heading(self, level, pad, extra): 73 self.out(pad + "=" * level + extra) 74 75 def start_larger(self): 76 self.out("~+") 77 78 def end_larger(self): 79 self.out("+~") 80 81 def start_link(self, target): 82 self.out("[[%s" % target) 83 84 def end_link(self): 85 self.out("]]") 86 87 def start_linktext(self): 88 self.out("|") 89 90 def end_linktext(self): 91 pass 92 93 def start_list(self, indent, marker, num): 94 pass 95 96 def end_list(self, indent, marker, num): 97 pass 98 99 def start_listitem(self, indent, marker, space, num): 100 self.out("%s%s%s%s" % (indent * " ", marker, num and "#%s" % num or "", space)) 101 102 def end_listitem(self, indent, marker, space, num): 103 pass 104 105 def start_macro(self, name, args): 106 self.out("<<%s%s>>" % (name, args and "(%s)" % ",".join(args) or "")) 107 108 def end_macro(self): 109 pass 110 111 def start_monospace(self): 112 self.out("`") 113 114 def end_monospace(self): 115 self.out("`") 116 117 def start_smaller(self): 118 self.out("~-") 119 120 def end_smaller(self): 121 self.out("-~") 122 123 def start_strong(self): 124 self.out("'''") 125 126 def end_strong(self): 127 self.out("'''") 128 129 def start_strikethrough(self): 130 self.out("--(") 131 132 def end_strikethrough(self): 133 self.out(")--") 134 135 def start_subscript(self): 136 self.out(",,") 137 138 def end_subscript(self): 139 self.out(",,") 140 141 def start_superscript(self): 142 self.out("^") 143 144 def end_superscript(self): 145 self.out("^") 146 147 def start_table(self): 148 pass 149 150 def end_table(self): 151 pass 152 153 def start_table_attrs(self): 154 self.out("<") 155 156 def end_table_attrs(self): 157 self.out(">") 158 159 def start_table_cell(self, attrs): 160 self.out("||") 161 if attrs and not attrs.empty(): 162 attrs.to_string(self) 163 164 def end_table_cell(self): 165 pass 166 167 def start_table_row(self): 168 pass 169 170 def end_table_row(self, trailing): 171 self.out("||") 172 self.out(trailing) 173 174 def start_underline(self): 175 self.out("__") 176 177 def end_underline(self): 178 self.out("__") 179 180 def break_(self): 181 self.out("\n") 182 183 def continuation(self, text): 184 self.out(text) 185 186 def rule(self, length): 187 self.out("-" * length) 188 189 def table_attr(self, name, value, concise, quote): 190 if concise: 191 if name == "colour": self.out(value) 192 elif name == "colspan": self.out("-%s" % value) 193 elif name == "halign" : self.out(value == "left" and "(" or value == "right" and ")" or ":") 194 elif name == "rowspan": self.out("|%s" % value) 195 elif name == "valign" : self.out(value == "top" and "^" or "v") 196 elif name == "width" : self.out(value) 197 else: 198 self.out("%s%s" % (escape_text(name), value is not None and 199 "=%s%s%s" % (quote or '"', escape_attr(value), quote or '"') or "")) 200 201 def text(self, s): 202 self.out(s) 203 204 serialiser = MoinSerialiser 205 206 # vim: tabstop=4 expandtab shiftwidth=4