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, make_id, \ 23 Serialiser 24 25 class HTMLSerialiser(Serialiser): 26 27 "Serialisation of the page." 28 29 format = "html" 30 31 def _region_tag(self, type): 32 33 # NOTE: Need to support types in general. 34 35 type = type and type.split()[0] 36 37 if type == "inline": 38 return "tt" 39 elif type in (None, "python"): 40 return "pre" 41 else: 42 return "span" 43 44 def start_region(self, level, indent, type, extra): 45 46 # Generate attributes, joining them when preparing the tag. 47 48 l = [] 49 out = l.append 50 51 if level: 52 out("level-%d" % level) 53 54 if indent: 55 out("indent-%d" % indent) 56 57 # NOTE: Encode type details for CSS. 58 59 out("type-%s" % escape_attr(type or "opaque")) 60 61 tag = self._region_tag(type) 62 self.out("<%s class='%s'>" % (tag, " ".join(l))) 63 64 def end_region(self, level, indent, type, extra): 65 tag = self._region_tag(type) 66 self.out("</%s>" % tag) 67 68 def start_block(self): 69 self.out("<p>") 70 71 def end_block(self): 72 self.out("</p>") 73 74 def start_defitem(self, pad, extra): 75 self.out("<dd>") 76 77 def end_defitem(self, pad, extra): 78 self.out("</dd>") 79 80 def start_defterm(self, pad): 81 self.out("<dt>") 82 83 def end_defterm(self, pad): 84 self.out("</dt>") 85 86 def start_emphasis(self): 87 self.out("<em>") 88 89 def end_emphasis(self): 90 self.out("</em>") 91 92 def start_heading(self, level, extra, pad, text): 93 self.out("<h%d id='%s'>" % (level, escape_attr(make_id(text)))) 94 95 def end_heading(self, level, pad, extra): 96 self.out("</h%d>" % level) 97 98 def start_larger(self): 99 self.out("<big>") 100 101 def end_larger(self): 102 self.out("</big>") 103 104 def start_link(self, target, nodes): 105 if self.linker: 106 target = self.linker.translate(target) 107 self.out('<a href="%s">' % escape_attr(target)) 108 if not nodes: 109 self.out(escape_text(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(make_id(target))) 252 253 def break_(self): 254 pass 255 256 def linebreak(self): 257 self.out("<br />") 258 259 def rule(self, length): 260 self.out("<hr style='height: %dpt' />" % min(length, 10)) 261 262 def table_attrs(self, nodes): 263 264 # Skip the attributes in their original form. 265 266 pass 267 268 def table_attr(self, name, value, concise, quote): 269 self.out(" %s%s" % (escape_text(name), value is not None and 270 "='%s'" % escape_attr(value) or "")) 271 272 def text(self, s): 273 self.out(escape_text(s)) 274 275 serialiser = HTMLSerialiser 276 277 # vim: tabstop=4 expandtab shiftwidth=4