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 label = None 106 if self.linker: 107 target, label = self.linker.translate(target) 108 self.out('<a href="%s">' % escape_attr(target)) 109 if not nodes: 110 self.out(escape_text(label or target)) 111 112 def end_link(self): 113 self.out("</a>") 114 115 def start_linktext(self): 116 pass 117 118 def end_linktext(self): 119 pass 120 121 list_tags = { 122 "i" : "lower-roman", 123 "I" : "upper-roman", 124 "a" : "lower-latin", 125 "A" : "upper-latin", 126 } 127 128 def _get_list_tag(self, marker): 129 if marker: 130 if marker[0].isdigit(): 131 return "ol", "decimal" 132 style_type = self.list_tags.get(marker[0]) 133 if style_type: 134 return "ol", style_type 135 136 return "ul", None 137 138 def start_list(self, indent, marker, num): 139 tag, style_type = self._get_list_tag(marker) 140 style = style_type and ' style="list-style-type: %s"' % escape_attr(style_type) or "" 141 start = style_type and num is not None and ' start="%s"' % escape_attr(num) or "" 142 self.out("<%s%s%s>" % (tag, style, start)) 143 144 def end_list(self, indent, marker, num): 145 tag, style = self._get_list_tag(marker) 146 self.out("</%s>" % tag) 147 148 def start_listitem(self, indent, marker, space, num): 149 self.out("<li>") 150 151 def end_listitem(self, indent, marker, space, num): 152 self.out("</li>") 153 154 def start_macro(self, name, args, nodes): 155 self.out("<span class='macro'>") 156 157 # Fallback case for when macros are not replaced. 158 159 if not nodes: 160 self.out(escape_text("<<")) 161 self.out("<span class='name'>%s</span>" % escape_text(name)) 162 if args: 163 self.out("(") 164 first = True 165 for arg in args: 166 if not first: 167 self.out(",") 168 self.out("<span class='arg'>%s</span>" % escape_text(arg)) 169 first = False 170 if args: 171 self.out(")") 172 self.out(escape_text(">>")) 173 174 def end_macro(self): 175 self.out("</span>") 176 177 def start_monospace(self): 178 self.out("<tt>") 179 180 def end_monospace(self): 181 self.out("</tt>") 182 183 def start_smaller(self): 184 self.out("<small>") 185 186 def end_smaller(self): 187 self.out("</small>") 188 189 def start_strikethrough(self): 190 self.out("<del>") 191 192 def end_strikethrough(self): 193 self.out("</del>") 194 195 def start_strong(self): 196 self.out("<strong>") 197 198 def end_strong(self): 199 self.out("</strong>") 200 201 def start_subscript(self): 202 self.out("<sub>") 203 204 def end_subscript(self): 205 self.out("</sub>") 206 207 def start_superscript(self): 208 self.out("<sup>") 209 210 def end_superscript(self): 211 self.out("</sup>") 212 213 def start_table(self): 214 self.out("<table>") 215 216 def end_table(self): 217 self.out("</table>") 218 219 def start_table_attrs(self): 220 pass 221 222 def end_table_attrs(self): 223 pass 224 225 def start_table_cell(self, attrs): 226 self.out("<td") 227 228 # Handle the attributes separately from their container. 229 230 if attrs and not attrs.empty(): 231 for attr in attrs.nodes: 232 attr.to_string(self) 233 234 self.out(">") 235 236 def end_table_cell(self): 237 self.out("</td>") 238 239 def start_table_row(self): 240 self.out("<tr>") 241 242 def end_table_row(self, trailing): 243 self.out("</tr>") 244 245 def start_underline(self): 246 self.out("<span style='text-decoration: underline'>") 247 248 def end_underline(self): 249 self.out("</span>") 250 251 def anchor(self, target): 252 self.out("<a name='%s' />" % escape_attr(make_id(target))) 253 254 def break_(self): 255 pass 256 257 def linebreak(self): 258 self.out("<br />") 259 260 def rule(self, length): 261 self.out("<hr style='height: %dpt' />" % min(length, 10)) 262 263 def table_attrs(self, nodes): 264 265 # Skip the attributes in their original form. 266 267 pass 268 269 def table_attr(self, name, value, concise, quote): 270 self.out(" %s%s" % (escape_text(name), value is not None and 271 "='%s'" % escape_attr(value) or "")) 272 273 def text(self, s): 274 self.out(escape_text(s)) 275 276 serialiser = HTMLSerialiser 277 278 # vim: tabstop=4 expandtab shiftwidth=4