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