paul@38 | 1 | #!/usr/bin/env python |
paul@38 | 2 | |
paul@38 | 3 | """ |
paul@38 | 4 | HTML serialiser. |
paul@38 | 5 | |
paul@240 | 6 | Copyright (C) 2017, 2018, 2019 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@240 | 23 | from moinformat.tree.moin import LinkLabel, LinkParameter, Text |
paul@240 | 24 | from moinformat.utils.links import parse_link_target |
paul@38 | 25 | |
paul@38 | 26 | class HTMLSerialiser(Serialiser): |
paul@38 | 27 | |
paul@38 | 28 | "Serialisation of the page." |
paul@38 | 29 | |
paul@85 | 30 | format = "html" |
paul@85 | 31 | |
paul@52 | 32 | def _region_tag(self, type): |
paul@52 | 33 | |
paul@52 | 34 | # NOTE: Need to support types in general. |
paul@52 | 35 | |
paul@56 | 36 | type = type and type.split()[0] |
paul@52 | 37 | |
paul@52 | 38 | if type == "inline": |
paul@52 | 39 | return "tt" |
paul@52 | 40 | elif type in (None, "python"): |
paul@52 | 41 | return "pre" |
paul@52 | 42 | else: |
paul@52 | 43 | return "span" |
paul@52 | 44 | |
paul@67 | 45 | def start_region(self, level, indent, type, extra): |
paul@98 | 46 | |
paul@98 | 47 | # Generate attributes, joining them when preparing the tag. |
paul@98 | 48 | |
paul@38 | 49 | l = [] |
paul@38 | 50 | out = l.append |
paul@98 | 51 | |
paul@38 | 52 | if level: |
paul@38 | 53 | out("level-%d" % level) |
paul@38 | 54 | |
paul@38 | 55 | if indent: |
paul@38 | 56 | out("indent-%d" % indent) |
paul@38 | 57 | |
paul@38 | 58 | # NOTE: Encode type details for CSS. |
paul@38 | 59 | |
paul@52 | 60 | out("type-%s" % escape_attr(type or "opaque")) |
paul@38 | 61 | |
paul@52 | 62 | tag = self._region_tag(type) |
paul@52 | 63 | self.out("<%s class='%s'>" % (tag, " ".join(l))) |
paul@38 | 64 | |
paul@67 | 65 | def end_region(self, level, indent, type, extra): |
paul@52 | 66 | tag = self._region_tag(type) |
paul@52 | 67 | self.out("</%s>" % tag) |
paul@38 | 68 | |
paul@38 | 69 | def start_block(self): |
paul@38 | 70 | self.out("<p>") |
paul@38 | 71 | |
paul@38 | 72 | def end_block(self): |
paul@38 | 73 | self.out("</p>") |
paul@38 | 74 | |
paul@38 | 75 | def start_defitem(self, pad, extra): |
paul@38 | 76 | self.out("<dd>") |
paul@38 | 77 | |
paul@38 | 78 | def end_defitem(self, pad, extra): |
paul@38 | 79 | self.out("</dd>") |
paul@38 | 80 | |
paul@122 | 81 | def start_defterm(self, pad, extra): |
paul@38 | 82 | self.out("<dt>") |
paul@38 | 83 | |
paul@122 | 84 | def end_defterm(self, pad, extra): |
paul@38 | 85 | self.out("</dt>") |
paul@38 | 86 | |
paul@38 | 87 | def start_emphasis(self): |
paul@38 | 88 | self.out("<em>") |
paul@38 | 89 | |
paul@38 | 90 | def end_emphasis(self): |
paul@38 | 91 | self.out("</em>") |
paul@38 | 92 | |
paul@128 | 93 | def start_heading(self, level, extra, pad, identifier): |
paul@128 | 94 | self.out("<h%d id='%s'>" % (level, escape_attr(self.linker.make_id(identifier)))) |
paul@38 | 95 | |
paul@38 | 96 | def end_heading(self, level, pad, extra): |
paul@38 | 97 | self.out("</h%d>" % level) |
paul@38 | 98 | |
paul@38 | 99 | def start_larger(self): |
paul@38 | 100 | self.out("<big>") |
paul@38 | 101 | |
paul@38 | 102 | def end_larger(self): |
paul@38 | 103 | self.out("</big>") |
paul@38 | 104 | |
paul@47 | 105 | def start_linktext(self): |
paul@47 | 106 | pass |
paul@47 | 107 | |
paul@47 | 108 | def end_linktext(self): |
paul@47 | 109 | pass |
paul@47 | 110 | |
paul@50 | 111 | list_tags = { |
paul@50 | 112 | "i" : "lower-roman", |
paul@50 | 113 | "I" : "upper-roman", |
paul@50 | 114 | "a" : "lower-latin", |
paul@50 | 115 | "A" : "upper-latin", |
paul@50 | 116 | } |
paul@43 | 117 | |
paul@50 | 118 | def _get_list_tag(self, marker): |
paul@50 | 119 | if marker: |
paul@50 | 120 | if marker[0].isdigit(): |
paul@50 | 121 | return "ol", "decimal" |
paul@50 | 122 | style_type = self.list_tags.get(marker[0]) |
paul@50 | 123 | if style_type: |
paul@50 | 124 | return "ol", style_type |
paul@43 | 125 | |
paul@50 | 126 | return "ul", None |
paul@50 | 127 | |
paul@51 | 128 | def start_list(self, indent, marker, num): |
paul@50 | 129 | tag, style_type = self._get_list_tag(marker) |
paul@50 | 130 | style = style_type and ' style="list-style-type: %s"' % escape_attr(style_type) or "" |
paul@51 | 131 | start = style_type and num is not None and ' start="%s"' % escape_attr(num) or "" |
paul@51 | 132 | self.out("<%s%s%s>" % (tag, style, start)) |
paul@50 | 133 | |
paul@51 | 134 | def end_list(self, indent, marker, num): |
paul@50 | 135 | tag, style = self._get_list_tag(marker) |
paul@50 | 136 | self.out("</%s>" % tag) |
paul@43 | 137 | |
paul@51 | 138 | def start_listitem(self, indent, marker, space, num): |
paul@38 | 139 | self.out("<li>") |
paul@38 | 140 | |
paul@51 | 141 | def end_listitem(self, indent, marker, space, num): |
paul@38 | 142 | self.out("</li>") |
paul@38 | 143 | |
paul@89 | 144 | def start_macro(self, name, args, nodes): |
paul@89 | 145 | self.out("<span class='macro'>") |
paul@87 | 146 | |
paul@87 | 147 | # Fallback case for when macros are not replaced. |
paul@87 | 148 | |
paul@89 | 149 | if not nodes: |
paul@89 | 150 | self.out(escape_text("<<")) |
paul@89 | 151 | self.out("<span class='name'>%s</span>" % escape_text(name)) |
paul@89 | 152 | if args: |
paul@89 | 153 | self.out("(") |
paul@89 | 154 | first = True |
paul@89 | 155 | for arg in args: |
paul@89 | 156 | if not first: |
paul@89 | 157 | self.out(",") |
paul@89 | 158 | self.out("<span class='arg'>%s</span>" % escape_text(arg)) |
paul@89 | 159 | first = False |
paul@89 | 160 | if args: |
paul@89 | 161 | self.out(")") |
paul@89 | 162 | self.out(escape_text(">>")) |
paul@87 | 163 | |
paul@87 | 164 | def end_macro(self): |
paul@89 | 165 | self.out("</span>") |
paul@87 | 166 | |
paul@38 | 167 | def start_monospace(self): |
paul@38 | 168 | self.out("<tt>") |
paul@38 | 169 | |
paul@38 | 170 | def end_monospace(self): |
paul@38 | 171 | self.out("</tt>") |
paul@38 | 172 | |
paul@38 | 173 | def start_smaller(self): |
paul@38 | 174 | self.out("<small>") |
paul@38 | 175 | |
paul@38 | 176 | def end_smaller(self): |
paul@38 | 177 | self.out("</small>") |
paul@38 | 178 | |
paul@48 | 179 | def start_strikethrough(self): |
paul@48 | 180 | self.out("<del>") |
paul@48 | 181 | |
paul@48 | 182 | def end_strikethrough(self): |
paul@48 | 183 | self.out("</del>") |
paul@48 | 184 | |
paul@38 | 185 | def start_strong(self): |
paul@38 | 186 | self.out("<strong>") |
paul@38 | 187 | |
paul@38 | 188 | def end_strong(self): |
paul@38 | 189 | self.out("</strong>") |
paul@38 | 190 | |
paul@38 | 191 | def start_subscript(self): |
paul@38 | 192 | self.out("<sub>") |
paul@38 | 193 | |
paul@38 | 194 | def end_subscript(self): |
paul@38 | 195 | self.out("</sub>") |
paul@38 | 196 | |
paul@38 | 197 | def start_superscript(self): |
paul@38 | 198 | self.out("<sup>") |
paul@38 | 199 | |
paul@38 | 200 | def end_superscript(self): |
paul@38 | 201 | self.out("</sup>") |
paul@38 | 202 | |
paul@38 | 203 | def start_table(self): |
paul@38 | 204 | self.out("<table>") |
paul@38 | 205 | |
paul@38 | 206 | def end_table(self): |
paul@38 | 207 | self.out("</table>") |
paul@38 | 208 | |
paul@38 | 209 | def start_table_attrs(self): |
paul@38 | 210 | pass |
paul@38 | 211 | |
paul@38 | 212 | def end_table_attrs(self): |
paul@38 | 213 | pass |
paul@38 | 214 | |
paul@38 | 215 | def start_table_cell(self, attrs): |
paul@38 | 216 | self.out("<td") |
paul@102 | 217 | |
paul@102 | 218 | # Handle the attributes separately from their container. |
paul@102 | 219 | |
paul@38 | 220 | if attrs and not attrs.empty(): |
paul@102 | 221 | for attr in attrs.nodes: |
paul@102 | 222 | attr.to_string(self) |
paul@102 | 223 | |
paul@38 | 224 | self.out(">") |
paul@38 | 225 | |
paul@38 | 226 | def end_table_cell(self): |
paul@38 | 227 | self.out("</td>") |
paul@38 | 228 | |
paul@38 | 229 | def start_table_row(self): |
paul@38 | 230 | self.out("<tr>") |
paul@38 | 231 | |
paul@38 | 232 | def end_table_row(self, trailing): |
paul@38 | 233 | self.out("</tr>") |
paul@38 | 234 | |
paul@38 | 235 | def start_underline(self): |
paul@38 | 236 | self.out("<span style='text-decoration: underline'>") |
paul@38 | 237 | |
paul@38 | 238 | def end_underline(self): |
paul@38 | 239 | self.out("</span>") |
paul@38 | 240 | |
paul@116 | 241 | def anchor(self, target): |
paul@128 | 242 | self.out("<a name='%s' />" % escape_attr(self.linker.make_id(target))) |
paul@116 | 243 | |
paul@38 | 244 | def break_(self): |
paul@38 | 245 | pass |
paul@38 | 246 | |
paul@151 | 247 | def comment(self, comment, extra): |
paul@151 | 248 | pass |
paul@151 | 249 | |
paul@151 | 250 | def directive(self, directive, extra): |
paul@240 | 251 | name, text = directive.split(None, 1) |
paul@240 | 252 | |
paul@240 | 253 | # Produce a readable redirect. |
paul@240 | 254 | |
paul@240 | 255 | if name.lower() == "redirect": |
paul@240 | 256 | self.start_block() |
paul@240 | 257 | |
paul@240 | 258 | # Process the redirect argument as a link target, producing a link |
paul@240 | 259 | # element. |
paul@240 | 260 | |
paul@240 | 261 | target = parse_link_target(text, self.metadata) |
paul@240 | 262 | self._link(target, [LinkLabel([Text(text)])], "a", "href") |
paul@240 | 263 | |
paul@240 | 264 | self.end_block() |
paul@151 | 265 | |
paul@106 | 266 | def linebreak(self): |
paul@106 | 267 | self.out("<br />") |
paul@106 | 268 | |
paul@167 | 269 | def _link(self, target, nodes, tag, attr): |
paul@214 | 270 | link = self.linker and self.linker.translate(target) or None |
paul@167 | 271 | |
paul@214 | 272 | self.out('<%s %s="%s"' % (tag, attr, escape_attr(link.get_target()))) |
paul@167 | 273 | |
paul@218 | 274 | # Provide link parameters as attributes. |
paul@218 | 275 | |
paul@167 | 276 | if nodes: |
paul@218 | 277 | for node in nodes: |
paul@218 | 278 | if isinstance(node, LinkParameter): |
paul@218 | 279 | self.out(" ") |
paul@218 | 280 | node.to_string(self) |
paul@167 | 281 | |
paul@220 | 282 | # Close the tag if an image. |
paul@220 | 283 | |
paul@220 | 284 | if tag == "img": |
paul@220 | 285 | self.out(" />") |
paul@167 | 286 | |
paul@218 | 287 | # Provide the link label if specified. Otherwise, use a generated |
paul@218 | 288 | # default for the label. |
paul@218 | 289 | |
paul@167 | 290 | else: |
paul@220 | 291 | self.out(">") |
paul@167 | 292 | |
paul@220 | 293 | for node in nodes or []: |
paul@220 | 294 | if isinstance(node, LinkLabel): |
paul@220 | 295 | node.to_string(self) |
paul@220 | 296 | break |
paul@220 | 297 | else: |
paul@220 | 298 | self.out(escape_text(link.get_label())) |
paul@220 | 299 | |
paul@220 | 300 | self.out("</%s>" % tag) |
paul@167 | 301 | |
paul@167 | 302 | def link(self, target, nodes): |
paul@167 | 303 | self._link(target, nodes, "a", "href") |
paul@167 | 304 | |
paul@167 | 305 | def link_label(self, nodes): |
paul@167 | 306 | for node in nodes: |
paul@167 | 307 | node.to_string(self) |
paul@167 | 308 | |
paul@167 | 309 | def link_parameter(self, key_value): |
paul@167 | 310 | if len(key_value) == 1: |
paul@167 | 311 | self.out(key_value[0]) |
paul@167 | 312 | else: |
paul@167 | 313 | key, value = key_value |
paul@167 | 314 | self.out("%s='%s'" % (key, escape_attr(value))) |
paul@167 | 315 | |
paul@38 | 316 | def rule(self, length): |
paul@38 | 317 | self.out("<hr style='height: %dpt' />" % min(length, 10)) |
paul@38 | 318 | |
paul@102 | 319 | def table_attrs(self, nodes): |
paul@102 | 320 | |
paul@102 | 321 | # Skip the attributes in their original form. |
paul@102 | 322 | |
paul@102 | 323 | pass |
paul@102 | 324 | |
paul@38 | 325 | def table_attr(self, name, value, concise, quote): |
paul@38 | 326 | self.out(" %s%s" % (escape_text(name), value is not None and |
paul@38 | 327 | "='%s'" % escape_attr(value) or "")) |
paul@38 | 328 | |
paul@38 | 329 | def text(self, s): |
paul@38 | 330 | self.out(escape_text(s)) |
paul@38 | 331 | |
paul@167 | 332 | def transclusion(self, target, nodes): |
paul@167 | 333 | self._link(target, nodes, "img", "src") |
paul@167 | 334 | |
paul@169 | 335 | def verbatim(self, s): |
paul@169 | 336 | self.text(s) |
paul@169 | 337 | |
paul@39 | 338 | serialiser = HTMLSerialiser |
paul@39 | 339 | |
paul@38 | 340 | # vim: tabstop=4 expandtab shiftwidth=4 |