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