1 #!/usr/bin/env python 2 3 """ 4 HTML serialiser. 5 6 Copyright (C) 2017, 2018 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 24 class HTMLSerialiser(Serialiser): 25 26 "Serialisation of the page." 27 28 format = "html" 29 30 def _region_tag(self, type): 31 32 # NOTE: Need to support types in general. 33 34 type = type and type.split()[0] 35 36 if type == "inline": 37 return "tt" 38 elif type in (None, "python"): 39 return "pre" 40 else: 41 return "span" 42 43 def start_region(self, level, indent, type, extra): 44 45 # Generate attributes, joining them when preparing the tag. 46 47 l = [] 48 out = l.append 49 50 if level: 51 out("level-%d" % level) 52 53 if indent: 54 out("indent-%d" % indent) 55 56 # NOTE: Encode type details for CSS. 57 58 out("type-%s" % escape_attr(type or "opaque")) 59 60 tag = self._region_tag(type) 61 self.out("<%s class='%s'>" % (tag, " ".join(l))) 62 63 def end_region(self, level, indent, type, extra): 64 tag = self._region_tag(type) 65 self.out("</%s>" % tag) 66 67 def start_block(self): 68 self.out("<p>") 69 70 def end_block(self): 71 self.out("</p>") 72 73 def start_defitem(self, pad, extra): 74 self.out("<dd>") 75 76 def end_defitem(self, pad, extra): 77 self.out("</dd>") 78 79 def start_defterm(self, pad, extra): 80 self.out("<dt>") 81 82 def end_defterm(self, pad, extra): 83 self.out("</dt>") 84 85 def start_emphasis(self): 86 self.out("<em>") 87 88 def end_emphasis(self): 89 self.out("</em>") 90 91 def start_heading(self, level, extra, pad, identifier): 92 self.out("<h%d id='%s'>" % (level, escape_attr(self.linker.make_id(identifier)))) 93 94 def end_heading(self, level, pad, extra): 95 self.out("</h%d>" % level) 96 97 def start_larger(self): 98 self.out("<big>") 99 100 def end_larger(self): 101 self.out("</big>") 102 103 def start_link(self, target, nodes): 104 label = None 105 if self.linker: 106 target, label = self.linker.translate(target) 107 self.out('<a href="%s">' % escape_attr(target)) 108 if not nodes: 109 self.out(escape_text(label or target)) 110 111 def end_link(self): 112 self.out("</a>") 113 114 def start_linktext(self): 115 pass 116 117 def end_linktext(self): 118 pass 119 120 list_tags = { 121 "i" : "lower-roman", 122 "I" : "upper-roman", 123 "a" : "lower-latin", 124 "A" : "upper-latin", 125 } 126 127 def _get_list_tag(self, marker): 128 if marker: 129 if marker[0].isdigit(): 130 return "ol", "decimal" 131 style_type = self.list_tags.get(marker[0]) 132 if style_type: 133 return "ol", style_type 134 135 return "ul", None 136 137 def start_list(self, indent, marker, num): 138 tag, style_type = self._get_list_tag(marker) 139 style = style_type and ' style="list-style-type: %s"' % escape_attr(style_type) or "" 140 start = style_type and num is not None and ' start="%s"' % escape_attr(num) or "" 141 self.out("<%s%s%s>" % (tag, style, start)) 142 143 def end_list(self, indent, marker, num): 144 tag, style = self._get_list_tag(marker) 145 self.out("</%s>" % tag) 146 147 def start_listitem(self, indent, marker, space, num): 148 self.out("<li>") 149 150 def end_listitem(self, indent, marker, space, num): 151 self.out("</li>") 152 153 def start_macro(self, name, args, nodes): 154 self.out("<span class='macro'>") 155 156 # Fallback case for when macros are not replaced. 157 158 if not nodes: 159 self.out(escape_text("<<")) 160 self.out("<span class='name'>%s</span>" % escape_text(name)) 161 if args: 162 self.out("(") 163 first = True 164 for arg in args: 165 if not first: 166 self.out(",") 167 self.out("<span class='arg'>%s</span>" % escape_text(arg)) 168 first = False 169 if args: 170 self.out(")") 171 self.out(escape_text(">>")) 172 173 def end_macro(self): 174 self.out("</span>") 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): 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): 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 pass 261 262 def linebreak(self): 263 self.out("<br />") 264 265 def rule(self, length): 266 self.out("<hr style='height: %dpt' />" % min(length, 10)) 267 268 def table_attrs(self, nodes): 269 270 # Skip the attributes in their original form. 271 272 pass 273 274 def table_attr(self, name, value, concise, quote): 275 self.out(" %s%s" % (escape_text(name), value is not None and 276 "='%s'" % escape_attr(value) or "")) 277 278 def text(self, s): 279 self.out(escape_text(s)) 280 281 serialiser = HTMLSerialiser 282 283 # vim: tabstop=4 expandtab shiftwidth=4