paul@38 | 1 | #!/usr/bin/env python |
paul@38 | 2 | |
paul@38 | 3 | """ |
paul@38 | 4 | HTML 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 HTMLSerialiser(Serialiser): |
paul@38 | 25 | |
paul@38 | 26 | "Serialisation of the page." |
paul@38 | 27 | |
paul@52 | 28 | def _region_tag(self, type): |
paul@52 | 29 | |
paul@52 | 30 | # NOTE: Need to support types in general. |
paul@52 | 31 | |
paul@56 | 32 | type = type and type.split()[0] |
paul@52 | 33 | |
paul@52 | 34 | if type == "inline": |
paul@52 | 35 | return "tt" |
paul@52 | 36 | elif type in (None, "python"): |
paul@52 | 37 | return "pre" |
paul@52 | 38 | else: |
paul@52 | 39 | return "span" |
paul@52 | 40 | |
paul@38 | 41 | def start_region(self, level, indent, type): |
paul@38 | 42 | l = [] |
paul@38 | 43 | out = l.append |
paul@38 | 44 | if level: |
paul@38 | 45 | out("level-%d" % level) |
paul@38 | 46 | |
paul@38 | 47 | if indent: |
paul@38 | 48 | out("indent-%d" % indent) |
paul@38 | 49 | |
paul@38 | 50 | # NOTE: Encode type details for CSS. |
paul@38 | 51 | |
paul@52 | 52 | out("type-%s" % escape_attr(type or "opaque")) |
paul@38 | 53 | |
paul@52 | 54 | tag = self._region_tag(type) |
paul@52 | 55 | self.out("<%s class='%s'>" % (tag, " ".join(l))) |
paul@38 | 56 | |
paul@38 | 57 | def end_region(self, level, indent, type): |
paul@52 | 58 | tag = self._region_tag(type) |
paul@52 | 59 | self.out("</%s>" % tag) |
paul@38 | 60 | |
paul@38 | 61 | def start_block(self): |
paul@38 | 62 | self.out("<p>") |
paul@38 | 63 | |
paul@38 | 64 | def end_block(self): |
paul@38 | 65 | self.out("</p>") |
paul@38 | 66 | |
paul@38 | 67 | def start_defitem(self, pad, extra): |
paul@38 | 68 | self.out("<dd>") |
paul@38 | 69 | |
paul@38 | 70 | def end_defitem(self, pad, extra): |
paul@38 | 71 | self.out("</dd>") |
paul@38 | 72 | |
paul@38 | 73 | def start_defterm(self, pad): |
paul@38 | 74 | self.out("<dt>") |
paul@38 | 75 | |
paul@38 | 76 | def end_defterm(self, pad): |
paul@38 | 77 | self.out("</dt>") |
paul@38 | 78 | |
paul@38 | 79 | def start_emphasis(self): |
paul@38 | 80 | self.out("<em>") |
paul@38 | 81 | |
paul@38 | 82 | def end_emphasis(self): |
paul@38 | 83 | self.out("</em>") |
paul@38 | 84 | |
paul@38 | 85 | def start_heading(self, level, extra, pad): |
paul@38 | 86 | self.out("<h%d>" % level) |
paul@38 | 87 | |
paul@38 | 88 | def end_heading(self, level, pad, extra): |
paul@38 | 89 | self.out("</h%d>" % level) |
paul@38 | 90 | |
paul@38 | 91 | def start_larger(self): |
paul@38 | 92 | self.out("<big>") |
paul@38 | 93 | |
paul@38 | 94 | def end_larger(self): |
paul@38 | 95 | self.out("</big>") |
paul@38 | 96 | |
paul@47 | 97 | def start_link(self, target): |
paul@47 | 98 | self.out('<a href="%s">' % escape_attr(target)) |
paul@47 | 99 | |
paul@47 | 100 | def end_link(self): |
paul@47 | 101 | self.out("</a>") |
paul@47 | 102 | |
paul@47 | 103 | def start_linktext(self): |
paul@47 | 104 | pass |
paul@47 | 105 | |
paul@47 | 106 | def end_linktext(self): |
paul@47 | 107 | pass |
paul@47 | 108 | |
paul@50 | 109 | list_tags = { |
paul@50 | 110 | "i" : "lower-roman", |
paul@50 | 111 | "I" : "upper-roman", |
paul@50 | 112 | "a" : "lower-latin", |
paul@50 | 113 | "A" : "upper-latin", |
paul@50 | 114 | } |
paul@43 | 115 | |
paul@50 | 116 | def _get_list_tag(self, marker): |
paul@50 | 117 | if marker: |
paul@50 | 118 | if marker[0].isdigit(): |
paul@50 | 119 | return "ol", "decimal" |
paul@50 | 120 | style_type = self.list_tags.get(marker[0]) |
paul@50 | 121 | if style_type: |
paul@50 | 122 | return "ol", style_type |
paul@43 | 123 | |
paul@50 | 124 | return "ul", None |
paul@50 | 125 | |
paul@51 | 126 | def start_list(self, indent, marker, num): |
paul@50 | 127 | tag, style_type = self._get_list_tag(marker) |
paul@50 | 128 | style = style_type and ' style="list-style-type: %s"' % escape_attr(style_type) or "" |
paul@51 | 129 | start = style_type and num is not None and ' start="%s"' % escape_attr(num) or "" |
paul@51 | 130 | self.out("<%s%s%s>" % (tag, style, start)) |
paul@50 | 131 | |
paul@51 | 132 | def end_list(self, indent, marker, num): |
paul@50 | 133 | tag, style = self._get_list_tag(marker) |
paul@50 | 134 | self.out("</%s>" % tag) |
paul@43 | 135 | |
paul@51 | 136 | def start_listitem(self, indent, marker, space, num): |
paul@38 | 137 | self.out("<li>") |
paul@38 | 138 | |
paul@51 | 139 | def end_listitem(self, indent, marker, space, num): |
paul@38 | 140 | self.out("</li>") |
paul@38 | 141 | |
paul@38 | 142 | def start_monospace(self): |
paul@38 | 143 | self.out("<tt>") |
paul@38 | 144 | |
paul@38 | 145 | def end_monospace(self): |
paul@38 | 146 | self.out("</tt>") |
paul@38 | 147 | |
paul@38 | 148 | def start_smaller(self): |
paul@38 | 149 | self.out("<small>") |
paul@38 | 150 | |
paul@38 | 151 | def end_smaller(self): |
paul@38 | 152 | self.out("</small>") |
paul@38 | 153 | |
paul@48 | 154 | def start_strikethrough(self): |
paul@48 | 155 | self.out("<del>") |
paul@48 | 156 | |
paul@48 | 157 | def end_strikethrough(self): |
paul@48 | 158 | self.out("</del>") |
paul@48 | 159 | |
paul@38 | 160 | def start_strong(self): |
paul@38 | 161 | self.out("<strong>") |
paul@38 | 162 | |
paul@38 | 163 | def end_strong(self): |
paul@38 | 164 | self.out("</strong>") |
paul@38 | 165 | |
paul@38 | 166 | def start_subscript(self): |
paul@38 | 167 | self.out("<sub>") |
paul@38 | 168 | |
paul@38 | 169 | def end_subscript(self): |
paul@38 | 170 | self.out("</sub>") |
paul@38 | 171 | |
paul@38 | 172 | def start_superscript(self): |
paul@38 | 173 | self.out("<sup>") |
paul@38 | 174 | |
paul@38 | 175 | def end_superscript(self): |
paul@38 | 176 | self.out("</sup>") |
paul@38 | 177 | |
paul@38 | 178 | def start_table(self): |
paul@38 | 179 | self.out("<table>") |
paul@38 | 180 | |
paul@38 | 181 | def end_table(self): |
paul@38 | 182 | self.out("</table>") |
paul@38 | 183 | |
paul@38 | 184 | def start_table_attrs(self): |
paul@38 | 185 | pass |
paul@38 | 186 | |
paul@38 | 187 | def end_table_attrs(self): |
paul@38 | 188 | pass |
paul@38 | 189 | |
paul@38 | 190 | def start_table_cell(self, attrs): |
paul@38 | 191 | self.out("<td") |
paul@38 | 192 | if attrs and not attrs.empty(): |
paul@38 | 193 | attrs.to_string(self) |
paul@38 | 194 | self.out(">") |
paul@38 | 195 | |
paul@38 | 196 | def end_table_cell(self): |
paul@38 | 197 | self.out("</td>") |
paul@38 | 198 | |
paul@38 | 199 | def start_table_row(self): |
paul@38 | 200 | self.out("<tr>") |
paul@38 | 201 | |
paul@38 | 202 | def end_table_row(self, trailing): |
paul@38 | 203 | self.out("</tr>") |
paul@38 | 204 | |
paul@38 | 205 | def start_underline(self): |
paul@38 | 206 | self.out("<span style='text-decoration: underline'>") |
paul@38 | 207 | |
paul@38 | 208 | def end_underline(self): |
paul@38 | 209 | self.out("</span>") |
paul@38 | 210 | |
paul@38 | 211 | def break_(self): |
paul@38 | 212 | pass |
paul@38 | 213 | |
paul@38 | 214 | def rule(self, length): |
paul@38 | 215 | self.out("<hr style='height: %dpt' />" % min(length, 10)) |
paul@38 | 216 | |
paul@38 | 217 | def table_attr(self, name, value, concise, quote): |
paul@38 | 218 | self.out(" %s%s" % (escape_text(name), value is not None and |
paul@38 | 219 | "='%s'" % escape_attr(value) or "")) |
paul@38 | 220 | |
paul@38 | 221 | def text(self, s): |
paul@38 | 222 | self.out(escape_text(s)) |
paul@38 | 223 | |
paul@39 | 224 | serialiser = HTMLSerialiser |
paul@39 | 225 | |
paul@38 | 226 | # vim: tabstop=4 expandtab shiftwidth=4 |