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