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 extra + "::" or "") + pad) 53 54 def end_defitem(self, pad, extra): 55 pass 56 57 def start_defterm(self, pad, extra): 58 self.out(pad) 59 60 def end_defterm(self, pad, extra): 61 self.out("::" + extra) 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, identifier): 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_list(self, indent, marker, num): 82 pass 83 84 def end_list(self, indent, marker, num): 85 pass 86 87 def start_listitem(self, indent, marker, space, num): 88 self.out("%s%s%s%s" % (indent * " ", marker, num and "#%s" % num or "", space)) 89 90 def end_listitem(self, indent, marker, space, num): 91 pass 92 93 def start_macro(self, name, args, nodes): 94 95 # Fallback case for when macros are not replaced. 96 97 if not nodes: 98 self.out("<<%s%s>>" % (name, args and "(%s)" % ",".join(args) or "")) 99 100 def end_macro(self): 101 pass 102 103 def start_monospace(self): 104 self.out("`") 105 106 def end_monospace(self): 107 self.out("`") 108 109 def start_smaller(self): 110 self.out("~-") 111 112 def end_smaller(self): 113 self.out("-~") 114 115 def start_strong(self): 116 self.out("'''") 117 118 def end_strong(self): 119 self.out("'''") 120 121 def start_strikethrough(self): 122 self.out("--(") 123 124 def end_strikethrough(self): 125 self.out(")--") 126 127 def start_subscript(self): 128 self.out(",,") 129 130 def end_subscript(self): 131 self.out(",,") 132 133 def start_superscript(self): 134 self.out("^") 135 136 def end_superscript(self): 137 self.out("^") 138 139 def start_table(self): 140 pass 141 142 def end_table(self): 143 pass 144 145 def start_table_attrs(self): 146 self.out("<") 147 148 def end_table_attrs(self): 149 self.out(">") 150 151 def start_table_cell(self, attrs): 152 self.out("||") 153 154 def end_table_cell(self): 155 pass 156 157 def start_table_row(self): 158 pass 159 160 def end_table_row(self, trailing): 161 self.out("||") 162 self.out(trailing) 163 164 def start_underline(self): 165 self.out("__") 166 167 def end_underline(self): 168 self.out("__") 169 170 def anchor(self, target): 171 self.out("((%s))" % target) 172 173 def break_(self): 174 self.out("\n") 175 176 def comment(self, comment, extra): 177 self.out("##%s%s" % (comment, extra)) 178 179 def directive(self, directive, extra): 180 self.out("#%s%s" % (directive, extra)) 181 182 def linebreak(self): 183 self.out(r"\\") 184 185 def link(self, target, nodes): 186 self.out("[[%s" % target) 187 for node in nodes: 188 self.out("|") 189 node.to_string(self) 190 self.out("]]") 191 192 def link_label(self, nodes): 193 for node in nodes: 194 node.to_string(self) 195 196 def link_parameter(self, key_value): 197 if len(key_value) == 1: 198 self.out(key_value[0]) 199 else: 200 self.out("=".join(key_value)) 201 202 def rule(self, length): 203 self.out("-" * length) 204 205 def table_attrs(self, nodes): 206 for node in nodes: 207 node.to_string(self) 208 209 def table_attr(self, name, value, concise, quote): 210 if concise: 211 if name == "colour": self.out(value) 212 elif name == "colspan": self.out("-%s" % value) 213 elif name == "halign" : self.out(value == "left" and "(" or value == "right" and ")" or ":") 214 elif name == "rowspan": self.out("|%s" % value) 215 elif name == "valign" : self.out(value == "top" and "^" or "v") 216 elif name == "width" : self.out(value) 217 else: 218 self.out("%s%s" % (escape_text(name), value is not None and 219 "=%s%s%s" % (quote or '"', escape_attr(value), quote or '"') or "")) 220 221 def text(self, s): 222 self.out(s) 223 224 def transclusion(self, target, nodes): 225 self.out("{{%s" % target) 226 for node in nodes: 227 self.out("|") 228 node.to_string(self) 229 self.out("}}") 230 231 serialiser = MoinSerialiser 232 233 # vim: tabstop=4 expandtab shiftwidth=4