1 #!/usr/bin/env python 2 3 """ 4 Moin wiki text serialiser. 5 6 Copyright (C) 2017 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): 29 out = self.out 30 if level: 31 out(" " * indent + "{" * level) 32 if type and level: 33 out("#!%s\n" % type) 34 35 def end_region(self, level, indent, type): 36 out = self.out 37 if level: 38 out("}" * level) 39 40 def start_block(self): 41 pass 42 43 def end_block(self): 44 pass 45 46 def start_defitem(self, pad, extra): 47 self.out((extra and "\n" + extra + "::" or "") + pad) 48 49 def end_defitem(self, pad, extra): 50 pass 51 52 def start_defterm(self, pad): 53 self.out(pad) 54 55 def end_defterm(self, pad): 56 self.out("::") 57 58 def start_emphasis(self): 59 self.out("''") 60 61 def end_emphasis(self): 62 self.out("''") 63 64 def start_heading(self, level, extra, pad): 65 self.out(extra + "=" * level + pad) 66 67 def end_heading(self, level, pad, extra): 68 self.out(pad + "=" * level + extra) 69 70 def start_larger(self): 71 self.out("~+") 72 73 def end_larger(self): 74 self.out("+~") 75 76 def start_listitem(self, indent, marker, space): 77 self.out("%s%s%s" % (indent * " ", marker, space)) 78 79 def end_listitem(self, indent, marker): 80 pass 81 82 def start_monospace(self): 83 self.out("`") 84 85 def end_monospace(self): 86 self.out("`") 87 88 def start_smaller(self): 89 self.out("~-") 90 91 def end_smaller(self): 92 self.out("-~") 93 94 def start_strong(self): 95 self.out("'''") 96 97 def end_strong(self): 98 self.out("'''") 99 100 def start_subscript(self): 101 self.out(",,") 102 103 def end_subscript(self): 104 self.out(",,") 105 106 def start_superscript(self): 107 self.out("^") 108 109 def end_superscript(self): 110 self.out("^") 111 112 def start_table(self): 113 pass 114 115 def end_table(self): 116 pass 117 118 def start_table_attrs(self): 119 self.out("<") 120 121 def end_table_attrs(self): 122 self.out(">") 123 124 def start_table_cell(self, attrs): 125 self.out("||") 126 if attrs and not attrs.empty(): 127 attrs.to_string(self) 128 129 def end_table_cell(self): 130 pass 131 132 def start_table_row(self): 133 pass 134 135 def end_table_row(self, trailing): 136 self.out("||") 137 self.out(trailing) 138 139 def start_underline(self): 140 self.out("__") 141 142 def end_underline(self): 143 self.out("__") 144 145 def break_(self): 146 self.out("\n") 147 148 def rule(self, length): 149 self.out("-" * length) 150 151 def table_attr(self, name, value, concise, quote): 152 if concise: 153 if name == "colour": self.out(value) 154 elif name == "colspan": self.out("-%s" % value) 155 elif name == "halign" : self.out(value == "left" and "(" or value == "right" and ")" or ":") 156 elif name == "rowspan": self.out("|%s" % value) 157 elif name == "valign" : self.out(value == "top" and "^" or "v") 158 elif name == "width" : self.out(value) 159 else: 160 self.out("%s%s" % (escape_text(name), value is not None and 161 "=%s%s%s" % (quote or '"', escape_attr(value), quote or '"') or "")) 162 163 def text(self, s): 164 self.out(s) 165 166 # vim: tabstop=4 expandtab shiftwidth=4