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@128 | 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@85 | 28 | format = "html" |
paul@85 | 29 | |
paul@52 | 30 | def _region_tag(self, type): |
paul@52 | 31 | |
paul@52 | 32 | # NOTE: Need to support types in general. |
paul@52 | 33 | |
paul@56 | 34 | type = type and type.split()[0] |
paul@52 | 35 | |
paul@52 | 36 | if type == "inline": |
paul@52 | 37 | return "tt" |
paul@52 | 38 | elif type in (None, "python"): |
paul@52 | 39 | return "pre" |
paul@52 | 40 | else: |
paul@52 | 41 | return "span" |
paul@52 | 42 | |
paul@67 | 43 | def start_region(self, level, indent, type, extra): |
paul@98 | 44 | |
paul@98 | 45 | # Generate attributes, joining them when preparing the tag. |
paul@98 | 46 | |
paul@38 | 47 | l = [] |
paul@38 | 48 | out = l.append |
paul@98 | 49 | |
paul@38 | 50 | if level: |
paul@38 | 51 | out("level-%d" % level) |
paul@38 | 52 | |
paul@38 | 53 | if indent: |
paul@38 | 54 | out("indent-%d" % indent) |
paul@38 | 55 | |
paul@38 | 56 | # NOTE: Encode type details for CSS. |
paul@38 | 57 | |
paul@52 | 58 | out("type-%s" % escape_attr(type or "opaque")) |
paul@38 | 59 | |
paul@52 | 60 | tag = self._region_tag(type) |
paul@52 | 61 | self.out("<%s class='%s'>" % (tag, " ".join(l))) |
paul@38 | 62 | |
paul@67 | 63 | def end_region(self, level, indent, type, extra): |
paul@52 | 64 | tag = self._region_tag(type) |
paul@52 | 65 | self.out("</%s>" % tag) |
paul@38 | 66 | |
paul@38 | 67 | def start_block(self): |
paul@38 | 68 | self.out("<p>") |
paul@38 | 69 | |
paul@38 | 70 | def end_block(self): |
paul@38 | 71 | self.out("</p>") |
paul@38 | 72 | |
paul@38 | 73 | def start_defitem(self, pad, extra): |
paul@38 | 74 | self.out("<dd>") |
paul@38 | 75 | |
paul@38 | 76 | def end_defitem(self, pad, extra): |
paul@38 | 77 | self.out("</dd>") |
paul@38 | 78 | |
paul@122 | 79 | def start_defterm(self, pad, extra): |
paul@38 | 80 | self.out("<dt>") |
paul@38 | 81 | |
paul@122 | 82 | def end_defterm(self, pad, extra): |
paul@38 | 83 | self.out("</dt>") |
paul@38 | 84 | |
paul@38 | 85 | def start_emphasis(self): |
paul@38 | 86 | self.out("<em>") |
paul@38 | 87 | |
paul@38 | 88 | def end_emphasis(self): |
paul@38 | 89 | self.out("</em>") |
paul@38 | 90 | |
paul@128 | 91 | def start_heading(self, level, extra, pad, identifier): |
paul@128 | 92 | self.out("<h%d id='%s'>" % (level, escape_attr(self.linker.make_id(identifier)))) |
paul@38 | 93 | |
paul@38 | 94 | def end_heading(self, level, pad, extra): |
paul@38 | 95 | self.out("</h%d>" % level) |
paul@38 | 96 | |
paul@38 | 97 | def start_larger(self): |
paul@38 | 98 | self.out("<big>") |
paul@38 | 99 | |
paul@38 | 100 | def end_larger(self): |
paul@38 | 101 | self.out("</big>") |
paul@38 | 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@89 | 142 | def start_macro(self, name, args, nodes): |
paul@89 | 143 | self.out("<span class='macro'>") |
paul@87 | 144 | |
paul@87 | 145 | # Fallback case for when macros are not replaced. |
paul@87 | 146 | |
paul@89 | 147 | if not nodes: |
paul@89 | 148 | self.out(escape_text("<<")) |
paul@89 | 149 | self.out("<span class='name'>%s</span>" % escape_text(name)) |
paul@89 | 150 | if args: |
paul@89 | 151 | self.out("(") |
paul@89 | 152 | first = True |
paul@89 | 153 | for arg in args: |
paul@89 | 154 | if not first: |
paul@89 | 155 | self.out(",") |
paul@89 | 156 | self.out("<span class='arg'>%s</span>" % escape_text(arg)) |
paul@89 | 157 | first = False |
paul@89 | 158 | if args: |
paul@89 | 159 | self.out(")") |
paul@89 | 160 | self.out(escape_text(">>")) |
paul@87 | 161 | |
paul@87 | 162 | def end_macro(self): |
paul@89 | 163 | self.out("</span>") |
paul@87 | 164 | |
paul@38 | 165 | def start_monospace(self): |
paul@38 | 166 | self.out("<tt>") |
paul@38 | 167 | |
paul@38 | 168 | def end_monospace(self): |
paul@38 | 169 | self.out("</tt>") |
paul@38 | 170 | |
paul@38 | 171 | def start_smaller(self): |
paul@38 | 172 | self.out("<small>") |
paul@38 | 173 | |
paul@38 | 174 | def end_smaller(self): |
paul@38 | 175 | self.out("</small>") |
paul@38 | 176 | |
paul@48 | 177 | def start_strikethrough(self): |
paul@48 | 178 | self.out("<del>") |
paul@48 | 179 | |
paul@48 | 180 | def end_strikethrough(self): |
paul@48 | 181 | self.out("</del>") |
paul@48 | 182 | |
paul@38 | 183 | def start_strong(self): |
paul@38 | 184 | self.out("<strong>") |
paul@38 | 185 | |
paul@38 | 186 | def end_strong(self): |
paul@38 | 187 | self.out("</strong>") |
paul@38 | 188 | |
paul@38 | 189 | def start_subscript(self): |
paul@38 | 190 | self.out("<sub>") |
paul@38 | 191 | |
paul@38 | 192 | def end_subscript(self): |
paul@38 | 193 | self.out("</sub>") |
paul@38 | 194 | |
paul@38 | 195 | def start_superscript(self): |
paul@38 | 196 | self.out("<sup>") |
paul@38 | 197 | |
paul@38 | 198 | def end_superscript(self): |
paul@38 | 199 | self.out("</sup>") |
paul@38 | 200 | |
paul@38 | 201 | def start_table(self): |
paul@38 | 202 | self.out("<table>") |
paul@38 | 203 | |
paul@38 | 204 | def end_table(self): |
paul@38 | 205 | self.out("</table>") |
paul@38 | 206 | |
paul@38 | 207 | def start_table_attrs(self): |
paul@38 | 208 | pass |
paul@38 | 209 | |
paul@38 | 210 | def end_table_attrs(self): |
paul@38 | 211 | pass |
paul@38 | 212 | |
paul@38 | 213 | def start_table_cell(self, attrs): |
paul@38 | 214 | self.out("<td") |
paul@102 | 215 | |
paul@102 | 216 | # Handle the attributes separately from their container. |
paul@102 | 217 | |
paul@38 | 218 | if attrs and not attrs.empty(): |
paul@102 | 219 | for attr in attrs.nodes: |
paul@102 | 220 | attr.to_string(self) |
paul@102 | 221 | |
paul@38 | 222 | self.out(">") |
paul@38 | 223 | |
paul@38 | 224 | def end_table_cell(self): |
paul@38 | 225 | self.out("</td>") |
paul@38 | 226 | |
paul@38 | 227 | def start_table_row(self): |
paul@38 | 228 | self.out("<tr>") |
paul@38 | 229 | |
paul@38 | 230 | def end_table_row(self, trailing): |
paul@38 | 231 | self.out("</tr>") |
paul@38 | 232 | |
paul@38 | 233 | def start_underline(self): |
paul@38 | 234 | self.out("<span style='text-decoration: underline'>") |
paul@38 | 235 | |
paul@38 | 236 | def end_underline(self): |
paul@38 | 237 | self.out("</span>") |
paul@38 | 238 | |
paul@116 | 239 | def anchor(self, target): |
paul@128 | 240 | self.out("<a name='%s' />" % escape_attr(self.linker.make_id(target))) |
paul@116 | 241 | |
paul@38 | 242 | def break_(self): |
paul@38 | 243 | pass |
paul@38 | 244 | |
paul@151 | 245 | def comment(self, comment, extra): |
paul@151 | 246 | pass |
paul@151 | 247 | |
paul@151 | 248 | def directive(self, directive, extra): |
paul@151 | 249 | pass |
paul@151 | 250 | |
paul@106 | 251 | def linebreak(self): |
paul@106 | 252 | self.out("<br />") |
paul@106 | 253 | |
paul@167 | 254 | def _link(self, target, nodes, tag, attr): |
paul@214 | 255 | link = self.linker and self.linker.translate(target) or None |
paul@167 | 256 | |
paul@214 | 257 | self.out('<%s %s="%s"' % (tag, attr, escape_attr(link.get_target()))) |
paul@167 | 258 | |
paul@167 | 259 | if nodes: |
paul@167 | 260 | for node in nodes[1:]: |
paul@167 | 261 | self.out(" ") |
paul@167 | 262 | node.to_string(self) |
paul@167 | 263 | |
paul@167 | 264 | self.out(">") |
paul@167 | 265 | |
paul@167 | 266 | if nodes: |
paul@167 | 267 | nodes[0].to_string(self) |
paul@167 | 268 | else: |
paul@214 | 269 | self.out(escape_text(link.get_label())) |
paul@167 | 270 | |
paul@167 | 271 | self.out("</%s>" % tag) |
paul@167 | 272 | |
paul@167 | 273 | def link(self, target, nodes): |
paul@167 | 274 | self._link(target, nodes, "a", "href") |
paul@167 | 275 | |
paul@167 | 276 | def link_label(self, nodes): |
paul@167 | 277 | for node in nodes: |
paul@167 | 278 | node.to_string(self) |
paul@167 | 279 | |
paul@167 | 280 | def link_parameter(self, key_value): |
paul@167 | 281 | if len(key_value) == 1: |
paul@167 | 282 | self.out(key_value[0]) |
paul@167 | 283 | else: |
paul@167 | 284 | key, value = key_value |
paul@167 | 285 | self.out("%s='%s'" % (key, escape_attr(value))) |
paul@167 | 286 | |
paul@38 | 287 | def rule(self, length): |
paul@38 | 288 | self.out("<hr style='height: %dpt' />" % min(length, 10)) |
paul@38 | 289 | |
paul@102 | 290 | def table_attrs(self, nodes): |
paul@102 | 291 | |
paul@102 | 292 | # Skip the attributes in their original form. |
paul@102 | 293 | |
paul@102 | 294 | pass |
paul@102 | 295 | |
paul@38 | 296 | def table_attr(self, name, value, concise, quote): |
paul@38 | 297 | self.out(" %s%s" % (escape_text(name), value is not None and |
paul@38 | 298 | "='%s'" % escape_attr(value) or "")) |
paul@38 | 299 | |
paul@38 | 300 | def text(self, s): |
paul@38 | 301 | self.out(escape_text(s)) |
paul@38 | 302 | |
paul@167 | 303 | def transclusion(self, target, nodes): |
paul@167 | 304 | self._link(target, nodes, "img", "src") |
paul@167 | 305 | |
paul@169 | 306 | def verbatim(self, s): |
paul@169 | 307 | self.text(s) |
paul@169 | 308 | |
paul@39 | 309 | serialiser = HTMLSerialiser |
paul@39 | 310 | |
paul@38 | 311 | # vim: tabstop=4 expandtab shiftwidth=4 |