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