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