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