paul@38 | 1 | #!/usr/bin/env python |
paul@38 | 2 | |
paul@38 | 3 | """ |
paul@38 | 4 | HTML serialiser. |
paul@38 | 5 | |
paul@331 | 6 | Copyright (C) 2017, 2018, 2019, 2021, 2022, |
paul@331 | 7 | 2023 Paul Boddie <paul@boddie.org.uk> |
paul@38 | 8 | |
paul@38 | 9 | This program is free software; you can redistribute it and/or modify it under |
paul@38 | 10 | the terms of the GNU General Public License as published by the Free Software |
paul@38 | 11 | Foundation; either version 3 of the License, or (at your option) any later |
paul@38 | 12 | version. |
paul@38 | 13 | |
paul@38 | 14 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@38 | 15 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@38 | 16 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@38 | 17 | details. |
paul@38 | 18 | |
paul@38 | 19 | You should have received a copy of the GNU General Public License along with |
paul@38 | 20 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@38 | 21 | """ |
paul@38 | 22 | |
paul@128 | 23 | from moinformat.serialisers.common import escape_attr, escape_text, Serialiser |
paul@240 | 24 | from moinformat.tree.moin import LinkLabel, LinkParameter, Text |
paul@240 | 25 | from moinformat.utils.links import parse_link_target |
paul@38 | 26 | |
paul@38 | 27 | class HTMLSerialiser(Serialiser): |
paul@38 | 28 | |
paul@38 | 29 | "Serialisation of the page." |
paul@38 | 30 | |
paul@301 | 31 | input_formats = ["moin", "wiki"] |
paul@301 | 32 | formats = ["html"] |
paul@85 | 33 | |
paul@331 | 34 | # Support methods. |
paul@47 | 35 | |
paul@50 | 36 | list_tags = { |
paul@50 | 37 | "i" : "lower-roman", |
paul@50 | 38 | "I" : "upper-roman", |
paul@50 | 39 | "a" : "lower-latin", |
paul@50 | 40 | "A" : "upper-latin", |
paul@50 | 41 | } |
paul@43 | 42 | |
paul@50 | 43 | def _get_list_tag(self, marker): |
paul@50 | 44 | if marker: |
paul@50 | 45 | if marker[0].isdigit(): |
paul@50 | 46 | return "ol", "decimal" |
paul@50 | 47 | style_type = self.list_tags.get(marker[0]) |
paul@50 | 48 | if style_type: |
paul@50 | 49 | return "ol", style_type |
paul@43 | 50 | |
paul@50 | 51 | return "ul", None |
paul@50 | 52 | |
paul@331 | 53 | def _link(self, target, nodes, tag, attr): |
paul@331 | 54 | link = self.linker and self.linker.translate(target) or None |
paul@331 | 55 | |
paul@331 | 56 | self.out('<%s %s="%s"' % (tag, attr, escape_attr(link.get_target()))) |
paul@331 | 57 | |
paul@331 | 58 | # Provide link parameters as attributes. |
paul@331 | 59 | |
paul@331 | 60 | if nodes: |
paul@331 | 61 | for node in nodes: |
paul@331 | 62 | if isinstance(node, LinkParameter): |
paul@331 | 63 | self.out(" ") |
paul@331 | 64 | node.visit(self) |
paul@331 | 65 | |
paul@331 | 66 | # Close the tag if an image. |
paul@331 | 67 | |
paul@331 | 68 | if tag == "img": |
paul@331 | 69 | self.out(" />") |
paul@331 | 70 | |
paul@331 | 71 | # Provide the link label if specified. Otherwise, use a generated |
paul@331 | 72 | # default for the label. |
paul@331 | 73 | |
paul@331 | 74 | else: |
paul@331 | 75 | self.out(">") |
paul@331 | 76 | |
paul@331 | 77 | for node in nodes or []: |
paul@331 | 78 | if isinstance(node, LinkLabel): |
paul@331 | 79 | node.visit(self) |
paul@331 | 80 | break |
paul@331 | 81 | else: |
paul@331 | 82 | self.out(escape_text(link.get_label())) |
paul@331 | 83 | |
paul@331 | 84 | self.out("</%s>" % tag) |
paul@331 | 85 | |
paul@331 | 86 | def _region_tag(self, type): |
paul@331 | 87 | |
paul@331 | 88 | # NOTE: Need to support types in general. |
paul@331 | 89 | |
paul@331 | 90 | type = type and type.split()[0] |
paul@50 | 91 | |
paul@331 | 92 | if type == "inline": |
paul@331 | 93 | return "tt" |
paul@331 | 94 | elif type in (None, "python"): |
paul@331 | 95 | return "pre" |
paul@331 | 96 | else: |
paul@331 | 97 | return "span" |
paul@331 | 98 | |
paul@331 | 99 | # Node handler methods. |
paul@331 | 100 | |
paul@331 | 101 | def region(self, region): |
paul@331 | 102 | tag = self._region_tag(region.type) |
paul@331 | 103 | |
paul@331 | 104 | # Generate attributes, joining them when preparing the tag. |
paul@331 | 105 | |
paul@331 | 106 | attrs = [] |
paul@331 | 107 | attr = attrs.append |
paul@331 | 108 | |
paul@331 | 109 | if region.level: |
paul@344 | 110 | attr("region-level-%d" % region.level) |
paul@331 | 111 | |
paul@331 | 112 | if region.indent: |
paul@344 | 113 | attr("region-indent-%d" % region.indent) |
paul@331 | 114 | |
paul@331 | 115 | # NOTE: Encode type details for CSS. |
paul@331 | 116 | |
paul@344 | 117 | attr("region-type-%s" % escape_attr(region.type or "opaque")) |
paul@331 | 118 | |
paul@331 | 119 | # Inline regions must preserve "indent" as space in the text. |
paul@331 | 120 | |
paul@331 | 121 | if region.type == "inline" and region.indent: |
paul@331 | 122 | self.out(" " * region.indent) |
paul@331 | 123 | |
paul@331 | 124 | self.out("<%s class='%s'>" % (tag, " ".join(attrs))) |
paul@331 | 125 | |
paul@331 | 126 | # Serialise the region content. |
paul@331 | 127 | |
paul@332 | 128 | self.visit_region(region) |
paul@331 | 129 | |
paul@331 | 130 | # End the region with the previous serialiser. |
paul@331 | 131 | |
paul@50 | 132 | self.out("</%s>" % tag) |
paul@43 | 133 | |
paul@331 | 134 | # Block node methods. |
paul@331 | 135 | |
paul@331 | 136 | def block(self, block): |
paul@331 | 137 | self.out("<p>") |
paul@331 | 138 | self.container(block) |
paul@331 | 139 | self.out("</p>") |
paul@331 | 140 | |
paul@331 | 141 | def defitem(self, defitem): |
paul@331 | 142 | self.out("<dd>") |
paul@331 | 143 | self.container(defitem) |
paul@331 | 144 | self.out("</dd>") |
paul@331 | 145 | |
paul@331 | 146 | def defterm(self, defterm): |
paul@331 | 147 | self.out("<dt>") |
paul@331 | 148 | self.container(defterm) |
paul@331 | 149 | self.out("</dt>") |
paul@331 | 150 | |
paul@331 | 151 | def fontstyle(self, fontstyle): |
paul@331 | 152 | if fontstyle.emphasis: |
paul@331 | 153 | self.out("<em>") |
paul@331 | 154 | elif fontstyle.strong: |
paul@331 | 155 | self.out("<strong>") |
paul@331 | 156 | self.container(fontstyle) |
paul@331 | 157 | if fontstyle.emphasis: |
paul@331 | 158 | self.out("</em>") |
paul@331 | 159 | elif fontstyle.strong: |
paul@331 | 160 | self.out("</strong>") |
paul@331 | 161 | |
paul@331 | 162 | def heading(self, heading): |
paul@331 | 163 | self.out("<h%d id='%s'>" % ( |
paul@331 | 164 | heading.level, |
paul@331 | 165 | escape_attr(self.linker.make_id(heading.identifier)))) |
paul@331 | 166 | self.container(heading) |
paul@331 | 167 | self.out("</h%d>" % heading.level) |
paul@331 | 168 | |
paul@331 | 169 | def larger(self, larger): |
paul@331 | 170 | self.out("<big>") |
paul@331 | 171 | self.container(larger) |
paul@331 | 172 | self.out("</big>") |
paul@331 | 173 | |
paul@331 | 174 | def list(self, list): |
paul@331 | 175 | tag, style_type = self._get_list_tag(list.marker) |
paul@331 | 176 | style = style_type and \ |
paul@331 | 177 | ' style="list-style-type: %s"' % escape_attr(style_type) or "" |
paul@331 | 178 | start = style_type and \ |
paul@331 | 179 | list.num is not None and ' start="%s"' % escape_attr(list.num) or "" |
paul@331 | 180 | self.out("<%s%s%s>" % (tag, style, start)) |
paul@331 | 181 | self.container(list) |
paul@331 | 182 | self.out("</%s>" % tag) |
paul@331 | 183 | |
paul@331 | 184 | def listitem(self, listitem): |
paul@38 | 185 | self.out("<li>") |
paul@331 | 186 | self.container(listitem) |
paul@38 | 187 | self.out("</li>") |
paul@38 | 188 | |
paul@331 | 189 | def macro(self, macro): |
paul@314 | 190 | |
paul@314 | 191 | # Special case of a deliberately unexpanded macro. |
paul@314 | 192 | |
paul@331 | 193 | if macro.nodes is None: |
paul@314 | 194 | return |
paul@314 | 195 | |
paul@331 | 196 | tag = macro.inline and "span" or "div" |
paul@331 | 197 | self.out("<%s class='macro %s'>" % (tag, escape_text(macro.name))) |
paul@87 | 198 | |
paul@87 | 199 | # Fallback case for when macros are not replaced. |
paul@87 | 200 | |
paul@331 | 201 | if not macro.nodes: |
paul@89 | 202 | self.out(escape_text("<<")) |
paul@331 | 203 | self.out("<span class='name'>%s</span>" % escape_text(macro.name)) |
paul@331 | 204 | if macro.args: |
paul@89 | 205 | self.out("(") |
paul@89 | 206 | first = True |
paul@331 | 207 | for arg in macro.args: |
paul@89 | 208 | if not first: |
paul@89 | 209 | self.out(",") |
paul@89 | 210 | self.out("<span class='arg'>%s</span>" % escape_text(arg)) |
paul@89 | 211 | first = False |
paul@331 | 212 | if macro.args: |
paul@89 | 213 | self.out(")") |
paul@89 | 214 | self.out(escape_text(">>")) |
paul@87 | 215 | |
paul@331 | 216 | # Produce the expanded macro content. |
paul@331 | 217 | |
paul@331 | 218 | else: |
paul@331 | 219 | self.container(macro) |
paul@331 | 220 | |
paul@331 | 221 | tag = macro.inline and "span" or "div" |
paul@262 | 222 | self.out("</%s>" % tag) |
paul@87 | 223 | |
paul@331 | 224 | def monospace(self, monospace): |
paul@38 | 225 | self.out("<tt>") |
paul@331 | 226 | self.container(monospace) |
paul@38 | 227 | self.out("</tt>") |
paul@38 | 228 | |
paul@331 | 229 | def smaller(self, smaller): |
paul@38 | 230 | self.out("<small>") |
paul@331 | 231 | self.container(smaller) |
paul@38 | 232 | self.out("</small>") |
paul@38 | 233 | |
paul@331 | 234 | def strikethrough(self, strikethrough): |
paul@48 | 235 | self.out("<del>") |
paul@331 | 236 | self.container(strikethrough) |
paul@48 | 237 | self.out("</del>") |
paul@48 | 238 | |
paul@332 | 239 | def subscript(self, subscript): |
paul@38 | 240 | self.out("<sub>") |
paul@331 | 241 | self.container(subscript) |
paul@38 | 242 | self.out("</sub>") |
paul@38 | 243 | |
paul@331 | 244 | def superscript(self, superscript): |
paul@38 | 245 | self.out("<sup>") |
paul@331 | 246 | self.container(superscript) |
paul@38 | 247 | self.out("</sup>") |
paul@38 | 248 | |
paul@331 | 249 | def table(self, table): |
paul@38 | 250 | self.out("<table>") |
paul@331 | 251 | self.container(table) |
paul@38 | 252 | self.out("</table>") |
paul@38 | 253 | |
paul@331 | 254 | def table_cell(self, table_cell): |
paul@38 | 255 | self.out("<td") |
paul@102 | 256 | |
paul@102 | 257 | # Handle the attributes separately from their container. |
paul@102 | 258 | |
paul@331 | 259 | if table_cell.attrs and not table_cell.attrs.empty(): |
paul@331 | 260 | for attr in table_cell.attrs.nodes: |
paul@331 | 261 | attr.visit(self) |
paul@102 | 262 | |
paul@38 | 263 | self.out(">") |
paul@331 | 264 | self.container(table_cell) |
paul@38 | 265 | self.out("</td>") |
paul@38 | 266 | |
paul@331 | 267 | def table_row(self, table_row): |
paul@38 | 268 | self.out("<tr>") |
paul@331 | 269 | self.container(table_row) |
paul@38 | 270 | self.out("</tr>") |
paul@38 | 271 | |
paul@331 | 272 | def underline(self, underline): |
paul@38 | 273 | self.out("<span style='text-decoration: underline'>") |
paul@331 | 274 | self.container(underline) |
paul@38 | 275 | self.out("</span>") |
paul@38 | 276 | |
paul@331 | 277 | # Inline node methods. |
paul@116 | 278 | |
paul@331 | 279 | def anchor(self, anchor): |
paul@331 | 280 | self.out("<a name='%s' />" % escape_attr(self.linker.make_id(anchor.target))) |
paul@331 | 281 | |
paul@331 | 282 | def break_(self, break_): |
paul@38 | 283 | pass |
paul@38 | 284 | |
paul@331 | 285 | def comment(self, comment): |
paul@151 | 286 | pass |
paul@151 | 287 | |
paul@331 | 288 | def directive(self, directive): |
paul@298 | 289 | |
paul@298 | 290 | # Obtain a blank value if the value is missing. |
paul@298 | 291 | |
paul@331 | 292 | name, text = (directive.directive.split(None, 1) + [""])[:2] |
paul@240 | 293 | |
paul@240 | 294 | # Produce a readable redirect. |
paul@240 | 295 | |
paul@240 | 296 | if name.lower() == "redirect": |
paul@349 | 297 | self.out("<p>") |
paul@240 | 298 | |
paul@240 | 299 | # Process the redirect argument as a link target, producing a link |
paul@240 | 300 | # element. |
paul@240 | 301 | |
paul@240 | 302 | target = parse_link_target(text, self.metadata) |
paul@240 | 303 | self._link(target, [LinkLabel([Text(text)])], "a", "href") |
paul@240 | 304 | |
paul@349 | 305 | self.out("</p>") |
paul@151 | 306 | |
paul@331 | 307 | def linebreak(self, linebreak): |
paul@106 | 308 | self.out("<br />") |
paul@106 | 309 | |
paul@331 | 310 | def link(self, link): |
paul@331 | 311 | self._link(link.target, link.nodes, "a", "href") |
paul@218 | 312 | |
paul@331 | 313 | def link_label(self, link_label): |
paul@331 | 314 | self.container(link_label) |
paul@220 | 315 | |
paul@331 | 316 | def link_parameter(self, link_parameter): |
paul@331 | 317 | s = link_parameter.text_content() |
paul@331 | 318 | key_value = s.split("=", 1) |
paul@167 | 319 | |
paul@167 | 320 | if len(key_value) == 1: |
paul@167 | 321 | self.out(key_value[0]) |
paul@167 | 322 | else: |
paul@167 | 323 | key, value = key_value |
paul@167 | 324 | self.out("%s='%s'" % (key, escape_attr(value))) |
paul@167 | 325 | |
paul@331 | 326 | def nbsp(self, nbsp): |
paul@316 | 327 | self.out(" ") |
paul@316 | 328 | |
paul@331 | 329 | def rule(self, rule): |
paul@331 | 330 | self.out("<hr style='height: %dpt' />" % min(rule.height, 10)) |
paul@38 | 331 | |
paul@331 | 332 | def table_attrs(self, table_attrs): |
paul@102 | 333 | |
paul@102 | 334 | # Skip the attributes in their original form. |
paul@102 | 335 | |
paul@102 | 336 | pass |
paul@102 | 337 | |
paul@331 | 338 | def table_attr(self, table_attr): |
paul@331 | 339 | self.out(" %s%s" % ( |
paul@331 | 340 | escape_text(table_attr.name), |
paul@331 | 341 | table_attr.value is not None and |
paul@331 | 342 | "='%s'" % escape_attr(table_attr.value) or "")) |
paul@38 | 343 | |
paul@331 | 344 | def text(self, text): |
paul@331 | 345 | self.out(escape_text(text.s)) |
paul@38 | 346 | |
paul@331 | 347 | def transclusion(self, transclusion): |
paul@331 | 348 | self._link(transclusion.target, transclusion.nodes, "img", "src") |
paul@167 | 349 | |
paul@331 | 350 | def verbatim(self, verbatim): |
paul@331 | 351 | self.out(escape_text(verbatim.text)) |
paul@169 | 352 | |
paul@39 | 353 | serialiser = HTMLSerialiser |
paul@39 | 354 | |
paul@38 | 355 | # vim: tabstop=4 expandtab shiftwidth=4 |