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