paul@38 | 1 | #!/usr/bin/env python |
paul@38 | 2 | |
paul@38 | 3 | """ |
paul@38 | 4 | Moin wiki text serialiser. |
paul@38 | 5 | |
paul@47 | 6 | Copyright (C) 2017, 2018 Paul Boddie <paul@boddie.org.uk> |
paul@38 | 7 | |
paul@38 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@38 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@38 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@38 | 11 | version. |
paul@38 | 12 | |
paul@38 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@38 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@38 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@38 | 16 | details. |
paul@38 | 17 | |
paul@38 | 18 | You should have received a copy of the GNU General Public License along with |
paul@38 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@38 | 20 | """ |
paul@38 | 21 | |
paul@38 | 22 | from moinformat.serialisers.common import escape_attr, escape_text, Serialiser |
paul@38 | 23 | |
paul@38 | 24 | class MoinSerialiser(Serialiser): |
paul@38 | 25 | |
paul@38 | 26 | "Serialisation of the page." |
paul@38 | 27 | |
paul@85 | 28 | format = "moin" |
paul@85 | 29 | |
paul@67 | 30 | def start_region(self, level, indent, type, extra): |
paul@38 | 31 | out = self.out |
paul@38 | 32 | if level: |
paul@38 | 33 | out(" " * indent + "{" * level) |
paul@52 | 34 | |
paul@52 | 35 | # Produce a header for regions within a top-level region. |
paul@52 | 36 | |
paul@52 | 37 | if type and type != "inline" and level: |
paul@38 | 38 | out("#!%s\n" % type) |
paul@38 | 39 | |
paul@67 | 40 | def end_region(self, level, indent, type, extra): |
paul@38 | 41 | out = self.out |
paul@38 | 42 | if level: |
paul@67 | 43 | out("%s%s" % ("}" * level, extra or "")) |
paul@38 | 44 | |
paul@38 | 45 | def start_block(self): |
paul@38 | 46 | pass |
paul@38 | 47 | |
paul@38 | 48 | def end_block(self): |
paul@38 | 49 | pass |
paul@38 | 50 | |
paul@38 | 51 | def start_defitem(self, pad, extra): |
paul@122 | 52 | self.out((extra and extra + "::" or "") + pad) |
paul@38 | 53 | |
paul@38 | 54 | def end_defitem(self, pad, extra): |
paul@38 | 55 | pass |
paul@38 | 56 | |
paul@122 | 57 | def start_defterm(self, pad, extra): |
paul@38 | 58 | self.out(pad) |
paul@38 | 59 | |
paul@122 | 60 | def end_defterm(self, pad, extra): |
paul@122 | 61 | self.out("::" + extra) |
paul@38 | 62 | |
paul@38 | 63 | def start_emphasis(self): |
paul@38 | 64 | self.out("''") |
paul@38 | 65 | |
paul@38 | 66 | def end_emphasis(self): |
paul@38 | 67 | self.out("''") |
paul@38 | 68 | |
paul@128 | 69 | def start_heading(self, level, extra, pad, identifier): |
paul@38 | 70 | self.out(extra + "=" * level + pad) |
paul@38 | 71 | |
paul@38 | 72 | def end_heading(self, level, pad, extra): |
paul@38 | 73 | self.out(pad + "=" * level + extra) |
paul@38 | 74 | |
paul@38 | 75 | def start_larger(self): |
paul@38 | 76 | self.out("~+") |
paul@38 | 77 | |
paul@38 | 78 | def end_larger(self): |
paul@38 | 79 | self.out("+~") |
paul@38 | 80 | |
paul@51 | 81 | def start_list(self, indent, marker, num): |
paul@43 | 82 | pass |
paul@43 | 83 | |
paul@51 | 84 | def end_list(self, indent, marker, num): |
paul@43 | 85 | pass |
paul@43 | 86 | |
paul@51 | 87 | def start_listitem(self, indent, marker, space, num): |
paul@55 | 88 | self.out("%s%s%s%s" % (indent * " ", marker, num and "#%s" % num or "", space)) |
paul@38 | 89 | |
paul@51 | 90 | def end_listitem(self, indent, marker, space, num): |
paul@38 | 91 | pass |
paul@38 | 92 | |
paul@89 | 93 | def start_macro(self, name, args, nodes): |
paul@89 | 94 | |
paul@89 | 95 | # Fallback case for when macros are not replaced. |
paul@89 | 96 | |
paul@89 | 97 | if not nodes: |
paul@89 | 98 | self.out("<<%s%s>>" % (name, args and "(%s)" % ",".join(args) or "")) |
paul@87 | 99 | |
paul@87 | 100 | def end_macro(self): |
paul@87 | 101 | pass |
paul@87 | 102 | |
paul@38 | 103 | def start_monospace(self): |
paul@38 | 104 | self.out("`") |
paul@38 | 105 | |
paul@38 | 106 | def end_monospace(self): |
paul@38 | 107 | self.out("`") |
paul@38 | 108 | |
paul@38 | 109 | def start_smaller(self): |
paul@38 | 110 | self.out("~-") |
paul@38 | 111 | |
paul@38 | 112 | def end_smaller(self): |
paul@38 | 113 | self.out("-~") |
paul@38 | 114 | |
paul@38 | 115 | def start_strong(self): |
paul@38 | 116 | self.out("'''") |
paul@38 | 117 | |
paul@38 | 118 | def end_strong(self): |
paul@38 | 119 | self.out("'''") |
paul@38 | 120 | |
paul@48 | 121 | def start_strikethrough(self): |
paul@48 | 122 | self.out("--(") |
paul@48 | 123 | |
paul@48 | 124 | def end_strikethrough(self): |
paul@48 | 125 | self.out(")--") |
paul@48 | 126 | |
paul@38 | 127 | def start_subscript(self): |
paul@38 | 128 | self.out(",,") |
paul@38 | 129 | |
paul@38 | 130 | def end_subscript(self): |
paul@38 | 131 | self.out(",,") |
paul@38 | 132 | |
paul@38 | 133 | def start_superscript(self): |
paul@38 | 134 | self.out("^") |
paul@38 | 135 | |
paul@38 | 136 | def end_superscript(self): |
paul@38 | 137 | self.out("^") |
paul@38 | 138 | |
paul@38 | 139 | def start_table(self): |
paul@38 | 140 | pass |
paul@38 | 141 | |
paul@38 | 142 | def end_table(self): |
paul@38 | 143 | pass |
paul@38 | 144 | |
paul@38 | 145 | def start_table_attrs(self): |
paul@38 | 146 | self.out("<") |
paul@38 | 147 | |
paul@38 | 148 | def end_table_attrs(self): |
paul@38 | 149 | self.out(">") |
paul@38 | 150 | |
paul@38 | 151 | def start_table_cell(self, attrs): |
paul@38 | 152 | self.out("||") |
paul@38 | 153 | |
paul@38 | 154 | def end_table_cell(self): |
paul@38 | 155 | pass |
paul@38 | 156 | |
paul@38 | 157 | def start_table_row(self): |
paul@38 | 158 | pass |
paul@38 | 159 | |
paul@38 | 160 | def end_table_row(self, trailing): |
paul@38 | 161 | self.out("||") |
paul@38 | 162 | self.out(trailing) |
paul@38 | 163 | |
paul@38 | 164 | def start_underline(self): |
paul@38 | 165 | self.out("__") |
paul@38 | 166 | |
paul@38 | 167 | def end_underline(self): |
paul@38 | 168 | self.out("__") |
paul@38 | 169 | |
paul@116 | 170 | def anchor(self, target): |
paul@116 | 171 | self.out("((%s))" % target) |
paul@116 | 172 | |
paul@38 | 173 | def break_(self): |
paul@38 | 174 | self.out("\n") |
paul@38 | 175 | |
paul@151 | 176 | def comment(self, comment, extra): |
paul@151 | 177 | self.out("##%s%s" % (comment, extra)) |
paul@151 | 178 | |
paul@151 | 179 | def directive(self, directive, extra): |
paul@151 | 180 | self.out("#%s%s" % (directive, extra)) |
paul@151 | 181 | |
paul@106 | 182 | def linebreak(self): |
paul@106 | 183 | self.out(r"\\") |
paul@106 | 184 | |
paul@167 | 185 | def link(self, target, nodes): |
paul@167 | 186 | self.out("[[%s" % target) |
paul@167 | 187 | for node in nodes: |
paul@167 | 188 | self.out("|") |
paul@167 | 189 | node.to_string(self) |
paul@167 | 190 | self.out("]]") |
paul@167 | 191 | |
paul@167 | 192 | def link_label(self, nodes): |
paul@167 | 193 | for node in nodes: |
paul@167 | 194 | node.to_string(self) |
paul@167 | 195 | |
paul@167 | 196 | def link_parameter(self, key_value): |
paul@167 | 197 | if len(key_value) == 1: |
paul@167 | 198 | self.out(key_value[0]) |
paul@167 | 199 | else: |
paul@167 | 200 | self.out("=".join(key_value)) |
paul@167 | 201 | |
paul@38 | 202 | def rule(self, length): |
paul@38 | 203 | self.out("-" * length) |
paul@38 | 204 | |
paul@102 | 205 | def table_attrs(self, nodes): |
paul@102 | 206 | for node in nodes: |
paul@102 | 207 | node.to_string(self) |
paul@102 | 208 | |
paul@38 | 209 | def table_attr(self, name, value, concise, quote): |
paul@38 | 210 | if concise: |
paul@38 | 211 | if name == "colour": self.out(value) |
paul@38 | 212 | elif name == "colspan": self.out("-%s" % value) |
paul@38 | 213 | elif name == "halign" : self.out(value == "left" and "(" or value == "right" and ")" or ":") |
paul@38 | 214 | elif name == "rowspan": self.out("|%s" % value) |
paul@38 | 215 | elif name == "valign" : self.out(value == "top" and "^" or "v") |
paul@38 | 216 | elif name == "width" : self.out(value) |
paul@38 | 217 | else: |
paul@38 | 218 | self.out("%s%s" % (escape_text(name), value is not None and |
paul@38 | 219 | "=%s%s%s" % (quote or '"', escape_attr(value), quote or '"') or "")) |
paul@38 | 220 | |
paul@38 | 221 | def text(self, s): |
paul@38 | 222 | self.out(s) |
paul@38 | 223 | |
paul@167 | 224 | def transclusion(self, target, nodes): |
paul@167 | 225 | self.out("{{%s" % target) |
paul@167 | 226 | for node in nodes: |
paul@167 | 227 | self.out("|") |
paul@167 | 228 | node.to_string(self) |
paul@167 | 229 | self.out("}}") |
paul@167 | 230 | |
paul@39 | 231 | serialiser = MoinSerialiser |
paul@39 | 232 | |
paul@38 | 233 | # vim: tabstop=4 expandtab shiftwidth=4 |