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_monospace(self): 106 self.out("`") 107 108 def end_monospace(self): 109 self.out("`") 110 111 def start_smaller(self): 112 self.out("~-") 113 114 def end_smaller(self): 115 self.out("-~") 116 117 def start_strong(self): 118 self.out("'''") 119 120 def end_strong(self): 121 self.out("'''") 122 123 def start_strikethrough(self): 124 self.out("--(") 125 126 def end_strikethrough(self): 127 self.out(")--") 128 129 def start_subscript(self): 130 self.out(",,") 131 132 def end_subscript(self): 133 self.out(",,") 134 135 def start_superscript(self): 136 self.out("^") 137 138 def end_superscript(self): 139 self.out("^") 140 141 def start_table(self): 142 pass 143 144 def end_table(self): 145 pass 146 147 def start_table_attrs(self): 148 self.out("<") 149 150 def end_table_attrs(self): 151 self.out(">") 152 153 def start_table_cell(self, attrs): 154 self.out("||") 155 if attrs and not attrs.empty(): 156 attrs.to_string(self) 157 158 def end_table_cell(self): 159 pass 160 161 def start_table_row(self): 162 pass 163 164 def end_table_row(self, trailing): 165 self.out("||") 166 self.out(trailing) 167 168 def start_underline(self): 169 self.out("__") 170 171 def end_underline(self): 172 self.out("__") 173 174 def break_(self): 175 self.out("\n") 176 177 def continuation(self, text): 178 self.out(text) 179 180 def macro(self, name, args): 181 self.out("<<%s%s>>" % (name, args and "(%s)" % ",".join(args) or "")) 182 183 def rule(self, length): 184 self.out("-" * length) 185 186 def table_attr(self, name, value, concise, quote): 187 if concise: 188 if name == "colour": self.out(value) 189 elif name == "colspan": self.out("-%s" % value) 190 elif name == "halign" : self.out(value == "left" and "(" or value == "right" and ")" or ":") 191 elif name == "rowspan": self.out("|%s" % value) 192 elif name == "valign" : self.out(value == "top" and "^" or "v") 193 elif name == "width" : self.out(value) 194 else: 195 self.out("%s%s" % (escape_text(name), value is not None and 196 "=%s%s%s" % (quote or '"', escape_attr(value), quote or '"') or "")) 197 198 def text(self, s): 199 self.out(s) 200 201 serialiser = MoinSerialiser 202 203 # vim: tabstop=4 expandtab shiftwidth=4