1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/moinformat/serialisers/html/moin.py Tue Jul 24 15:43:20 2018 +0200
1.3 @@ -0,0 +1,254 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +HTML serialiser.
1.8 +
1.9 +Copyright (C) 2017, 2018 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 HTMLSerialiser(Serialiser):
1.28 +
1.29 + "Serialisation of the page."
1.30 +
1.31 + format = "html"
1.32 +
1.33 + def _region_tag(self, type):
1.34 +
1.35 + # NOTE: Need to support types in general.
1.36 +
1.37 + type = type and type.split()[0]
1.38 +
1.39 + if type == "inline":
1.40 + return "tt"
1.41 + elif type in (None, "python"):
1.42 + return "pre"
1.43 + else:
1.44 + return "span"
1.45 +
1.46 + def start_region(self, level, indent, type, extra):
1.47 + l = []
1.48 + out = l.append
1.49 + if level:
1.50 + out("level-%d" % level)
1.51 +
1.52 + if indent:
1.53 + out("indent-%d" % indent)
1.54 +
1.55 + # NOTE: Encode type details for CSS.
1.56 +
1.57 + out("type-%s" % escape_attr(type or "opaque"))
1.58 +
1.59 + tag = self._region_tag(type)
1.60 + self.out("<%s class='%s'>" % (tag, " ".join(l)))
1.61 +
1.62 + def end_region(self, level, indent, type, extra):
1.63 + tag = self._region_tag(type)
1.64 + self.out("</%s>" % tag)
1.65 +
1.66 + def start_block(self):
1.67 + self.out("<p>")
1.68 +
1.69 + def end_block(self):
1.70 + self.out("</p>")
1.71 +
1.72 + def start_defitem(self, pad, extra):
1.73 + self.out("<dd>")
1.74 +
1.75 + def end_defitem(self, pad, extra):
1.76 + self.out("</dd>")
1.77 +
1.78 + def start_defterm(self, pad):
1.79 + self.out("<dt>")
1.80 +
1.81 + def end_defterm(self, pad):
1.82 + self.out("</dt>")
1.83 +
1.84 + def start_emphasis(self):
1.85 + self.out("<em>")
1.86 +
1.87 + def end_emphasis(self):
1.88 + self.out("</em>")
1.89 +
1.90 + def start_heading(self, level, extra, pad):
1.91 + self.out("<h%d>" % level)
1.92 +
1.93 + def end_heading(self, level, pad, extra):
1.94 + self.out("</h%d>" % level)
1.95 +
1.96 + def start_larger(self):
1.97 + self.out("<big>")
1.98 +
1.99 + def end_larger(self):
1.100 + self.out("</big>")
1.101 +
1.102 + def start_link(self, target):
1.103 + self.out('<a href="%s">' % escape_attr(target))
1.104 +
1.105 + def end_link(self):
1.106 + self.out("</a>")
1.107 +
1.108 + def start_linktext(self):
1.109 + pass
1.110 +
1.111 + def end_linktext(self):
1.112 + pass
1.113 +
1.114 + list_tags = {
1.115 + "i" : "lower-roman",
1.116 + "I" : "upper-roman",
1.117 + "a" : "lower-latin",
1.118 + "A" : "upper-latin",
1.119 + }
1.120 +
1.121 + def _get_list_tag(self, marker):
1.122 + if marker:
1.123 + if marker[0].isdigit():
1.124 + return "ol", "decimal"
1.125 + style_type = self.list_tags.get(marker[0])
1.126 + if style_type:
1.127 + return "ol", style_type
1.128 +
1.129 + return "ul", None
1.130 +
1.131 + def start_list(self, indent, marker, num):
1.132 + tag, style_type = self._get_list_tag(marker)
1.133 + style = style_type and ' style="list-style-type: %s"' % escape_attr(style_type) or ""
1.134 + start = style_type and num is not None and ' start="%s"' % escape_attr(num) or ""
1.135 + self.out("<%s%s%s>" % (tag, style, start))
1.136 +
1.137 + def end_list(self, indent, marker, num):
1.138 + tag, style = self._get_list_tag(marker)
1.139 + self.out("</%s>" % tag)
1.140 +
1.141 + def start_listitem(self, indent, marker, space, num):
1.142 + self.out("<li>")
1.143 +
1.144 + def end_listitem(self, indent, marker, space, num):
1.145 + self.out("</li>")
1.146 +
1.147 + def start_monospace(self):
1.148 + self.out("<tt>")
1.149 +
1.150 + def end_monospace(self):
1.151 + self.out("</tt>")
1.152 +
1.153 + def start_smaller(self):
1.154 + self.out("<small>")
1.155 +
1.156 + def end_smaller(self):
1.157 + self.out("</small>")
1.158 +
1.159 + def start_strikethrough(self):
1.160 + self.out("<del>")
1.161 +
1.162 + def end_strikethrough(self):
1.163 + self.out("</del>")
1.164 +
1.165 + def start_strong(self):
1.166 + self.out("<strong>")
1.167 +
1.168 + def end_strong(self):
1.169 + self.out("</strong>")
1.170 +
1.171 + def start_subscript(self):
1.172 + self.out("<sub>")
1.173 +
1.174 + def end_subscript(self):
1.175 + self.out("</sub>")
1.176 +
1.177 + def start_superscript(self):
1.178 + self.out("<sup>")
1.179 +
1.180 + def end_superscript(self):
1.181 + self.out("</sup>")
1.182 +
1.183 + def start_table(self):
1.184 + self.out("<table>")
1.185 +
1.186 + def end_table(self):
1.187 + self.out("</table>")
1.188 +
1.189 + def start_table_attrs(self):
1.190 + pass
1.191 +
1.192 + def end_table_attrs(self):
1.193 + pass
1.194 +
1.195 + def start_table_cell(self, attrs):
1.196 + self.out("<td")
1.197 + if attrs and not attrs.empty():
1.198 + attrs.to_string(self)
1.199 + self.out(">")
1.200 +
1.201 + def end_table_cell(self):
1.202 + self.out("</td>")
1.203 +
1.204 + def start_table_row(self):
1.205 + self.out("<tr>")
1.206 +
1.207 + def end_table_row(self, trailing):
1.208 + self.out("</tr>")
1.209 +
1.210 + def start_underline(self):
1.211 + self.out("<span style='text-decoration: underline'>")
1.212 +
1.213 + def end_underline(self):
1.214 + self.out("</span>")
1.215 +
1.216 + def break_(self):
1.217 + pass
1.218 +
1.219 + def macro(self, name, args):
1.220 +
1.221 + # NOTE: Special case.
1.222 +
1.223 + if name == "BR":
1.224 + self.out("<br />")
1.225 + return
1.226 +
1.227 + # Fallback case.
1.228 +
1.229 + self.out("<span class='macro'>")
1.230 + self.out(escape_text("<<"))
1.231 + self.out("<span class='name'>%s</span>" % escape_text(name))
1.232 + if args:
1.233 + self.out("(")
1.234 + first = True
1.235 + for arg in args:
1.236 + if not first:
1.237 + self.out(",")
1.238 + self.out("<span class='arg'>%s</span>" % escape_text(arg))
1.239 + first = False
1.240 + if args:
1.241 + self.out(")")
1.242 + self.out(escape_text(">>"))
1.243 + self.out("</span>")
1.244 +
1.245 + def rule(self, length):
1.246 + self.out("<hr style='height: %dpt' />" % min(length, 10))
1.247 +
1.248 + def table_attr(self, name, value, concise, quote):
1.249 + self.out(" %s%s" % (escape_text(name), value is not None and
1.250 + "='%s'" % escape_attr(value) or ""))
1.251 +
1.252 + def text(self, s):
1.253 + self.out(escape_text(s))
1.254 +
1.255 +serialiser = HTMLSerialiser
1.256 +
1.257 +# vim: tabstop=4 expandtab shiftwidth=4