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 def start_region(self, level, indent, type, extra): 29 out = self.out 30 if level: 31 out(" " * indent + "{" * level) 32 33 # Produce a header for regions within a top-level region. 34 35 if type and type != "inline" and level: 36 out("#!%s\n" % type) 37 38 def end_region(self, level, indent, type, extra): 39 out = self.out 40 if level: 41 out("%s%s" % ("}" * level, extra or "")) 42 43 def start_block(self): 44 pass 45 46 def end_block(self): 47 pass 48 49 def start_defitem(self, pad, extra): 50 self.out((extra and "\n" + extra + "::" or "") + pad) 51 52 def end_defitem(self, pad, extra): 53 pass 54 55 def start_defterm(self, pad): 56 self.out(pad) 57 58 def end_defterm(self, pad): 59 self.out("::") 60 61 def start_emphasis(self): 62 self.out("''") 63 64 def end_emphasis(self): 65 self.out("''") 66 67 def start_heading(self, level, extra, pad): 68 self.out(extra + "=" * level + pad) 69 70 def end_heading(self, level, pad, extra): 71 self.out(pad + "=" * level + extra) 72 73 def start_larger(self): 74 self.out("~+") 75 76 def end_larger(self): 77 self.out("+~") 78 79 def start_link(self, target): 80 self.out("[[%s" % target) 81 82 def end_link(self): 83 self.out("]]") 84 85 def start_linktext(self): 86 self.out("|") 87 88 def end_linktext(self): 89 pass 90 91 def start_list(self, indent, marker, num): 92 pass 93 94 def end_list(self, indent, marker, num): 95 pass 96 97 def start_listitem(self, indent, marker, space, num): 98 self.out("%s%s%s%s" % (indent * " ", marker, num and "#%s" % num or "", space)) 99 100 def end_listitem(self, indent, marker, space, num): 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 if attrs and not attrs.empty(): 154 attrs.to_string(self) 155 156 def end_table_cell(self): 157 pass 158 159 def start_table_row(self): 160 pass 161 162 def end_table_row(self, trailing): 163 self.out("||") 164 self.out(trailing) 165 166 def start_underline(self): 167 self.out("__") 168 169 def end_underline(self): 170 self.out("__") 171 172 def break_(self): 173 self.out("\n") 174 175 def continuation(self, text): 176 self.out(text) 177 178 def rule(self, length): 179 self.out("-" * length) 180 181 def table_attr(self, name, value, concise, quote): 182 if concise: 183 if name == "colour": self.out(value) 184 elif name == "colspan": self.out("-%s" % value) 185 elif name == "halign" : self.out(value == "left" and "(" or value == "right" and ")" or ":") 186 elif name == "rowspan": self.out("|%s" % value) 187 elif name == "valign" : self.out(value == "top" and "^" or "v") 188 elif name == "width" : self.out(value) 189 else: 190 self.out("%s%s" % (escape_text(name), value is not None and 191 "=%s%s%s" % (quote or '"', escape_attr(value), quote or '"') or "")) 192 193 def text(self, s): 194 self.out(s) 195 196 serialiser = MoinSerialiser 197 198 # vim: tabstop=4 expandtab shiftwidth=4