1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/moinformat/serialisers/moin.py Tue Dec 12 18:17:08 2017 +0100
1.3 @@ -0,0 +1,166 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Moin wiki text serialiser.
1.8 +
1.9 +Copyright (C) 2017 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from moinformat.serialisers.common import escape_attr, escape_text, Serialiser
1.26 +
1.27 +class MoinSerialiser(Serialiser):
1.28 +
1.29 + "Serialisation of the page."
1.30 +
1.31 + def start_region(self, level, indent, type):
1.32 + out = self.out
1.33 + if level:
1.34 + out(" " * indent + "{" * level)
1.35 + if type and level:
1.36 + out("#!%s\n" % type)
1.37 +
1.38 + def end_region(self, level, indent, type):
1.39 + out = self.out
1.40 + if level:
1.41 + out("}" * level)
1.42 +
1.43 + def start_block(self):
1.44 + pass
1.45 +
1.46 + def end_block(self):
1.47 + pass
1.48 +
1.49 + def start_defitem(self, pad, extra):
1.50 + self.out((extra and "\n" + extra + "::" or "") + pad)
1.51 +
1.52 + def end_defitem(self, pad, extra):
1.53 + pass
1.54 +
1.55 + def start_defterm(self, pad):
1.56 + self.out(pad)
1.57 +
1.58 + def end_defterm(self, pad):
1.59 + self.out("::")
1.60 +
1.61 + def start_emphasis(self):
1.62 + self.out("''")
1.63 +
1.64 + def end_emphasis(self):
1.65 + self.out("''")
1.66 +
1.67 + def start_heading(self, level, extra, pad):
1.68 + self.out(extra + "=" * level + pad)
1.69 +
1.70 + def end_heading(self, level, pad, extra):
1.71 + self.out(pad + "=" * level + extra)
1.72 +
1.73 + def start_larger(self):
1.74 + self.out("~+")
1.75 +
1.76 + def end_larger(self):
1.77 + self.out("+~")
1.78 +
1.79 + def start_listitem(self, indent, marker, space):
1.80 + self.out("%s%s%s" % (indent * " ", marker, space))
1.81 +
1.82 + def end_listitem(self, indent, marker):
1.83 + pass
1.84 +
1.85 + def start_monospace(self):
1.86 + self.out("`")
1.87 +
1.88 + def end_monospace(self):
1.89 + self.out("`")
1.90 +
1.91 + def start_smaller(self):
1.92 + self.out("~-")
1.93 +
1.94 + def end_smaller(self):
1.95 + self.out("-~")
1.96 +
1.97 + def start_strong(self):
1.98 + self.out("'''")
1.99 +
1.100 + def end_strong(self):
1.101 + self.out("'''")
1.102 +
1.103 + def start_subscript(self):
1.104 + self.out(",,")
1.105 +
1.106 + def end_subscript(self):
1.107 + self.out(",,")
1.108 +
1.109 + def start_superscript(self):
1.110 + self.out("^")
1.111 +
1.112 + def end_superscript(self):
1.113 + self.out("^")
1.114 +
1.115 + def start_table(self):
1.116 + pass
1.117 +
1.118 + def end_table(self):
1.119 + pass
1.120 +
1.121 + def start_table_attrs(self):
1.122 + self.out("<")
1.123 +
1.124 + def end_table_attrs(self):
1.125 + self.out(">")
1.126 +
1.127 + def start_table_cell(self, attrs):
1.128 + self.out("||")
1.129 + if attrs and not attrs.empty():
1.130 + attrs.to_string(self)
1.131 +
1.132 + def end_table_cell(self):
1.133 + pass
1.134 +
1.135 + def start_table_row(self):
1.136 + pass
1.137 +
1.138 + def end_table_row(self, trailing):
1.139 + self.out("||")
1.140 + self.out(trailing)
1.141 +
1.142 + def start_underline(self):
1.143 + self.out("__")
1.144 +
1.145 + def end_underline(self):
1.146 + self.out("__")
1.147 +
1.148 + def break_(self):
1.149 + self.out("\n")
1.150 +
1.151 + def rule(self, length):
1.152 + self.out("-" * length)
1.153 +
1.154 + def table_attr(self, name, value, concise, quote):
1.155 + if concise:
1.156 + if name == "colour": self.out(value)
1.157 + elif name == "colspan": self.out("-%s" % value)
1.158 + elif name == "halign" : self.out(value == "left" and "(" or value == "right" and ")" or ":")
1.159 + elif name == "rowspan": self.out("|%s" % value)
1.160 + elif name == "valign" : self.out(value == "top" and "^" or "v")
1.161 + elif name == "width" : self.out(value)
1.162 + else:
1.163 + self.out("%s%s" % (escape_text(name), value is not None and
1.164 + "=%s%s%s" % (quote or '"', escape_attr(value), quote or '"') or ""))
1.165 +
1.166 + def text(self, s):
1.167 + self.out(s)
1.168 +
1.169 +# vim: tabstop=4 expandtab shiftwidth=4