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, 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): 105 if self.linker: 106 target = self.linker.translate(target) 107 self.out('<a href="%s">' % escape_attr(target)) 108 109 def end_link(self): 110 self.out("</a>") 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): 152 self.out("<span class='macro'>") 153 154 # Fallback case for when macros are not replaced. 155 156 if not nodes: 157 self.out(escape_text("<<")) 158 self.out("<span class='name'>%s</span>" % escape_text(name)) 159 if args: 160 self.out("(") 161 first = True 162 for arg in args: 163 if not first: 164 self.out(",") 165 self.out("<span class='arg'>%s</span>" % escape_text(arg)) 166 first = False 167 if args: 168 self.out(")") 169 self.out(escape_text(">>")) 170 171 def end_macro(self): 172 self.out("</span>") 173 174 def start_monospace(self): 175 self.out("<tt>") 176 177 def end_monospace(self): 178 self.out("</tt>") 179 180 def start_smaller(self): 181 self.out("<small>") 182 183 def end_smaller(self): 184 self.out("</small>") 185 186 def start_strikethrough(self): 187 self.out("<del>") 188 189 def end_strikethrough(self): 190 self.out("</del>") 191 192 def start_strong(self): 193 self.out("<strong>") 194 195 def end_strong(self): 196 self.out("</strong>") 197 198 def start_subscript(self): 199 self.out("<sub>") 200 201 def end_subscript(self): 202 self.out("</sub>") 203 204 def start_superscript(self): 205 self.out("<sup>") 206 207 def end_superscript(self): 208 self.out("</sup>") 209 210 def start_table(self): 211 self.out("<table>") 212 213 def end_table(self): 214 self.out("</table>") 215 216 def start_table_attrs(self): 217 pass 218 219 def end_table_attrs(self): 220 pass 221 222 def start_table_cell(self, attrs): 223 self.out("<td") 224 225 # Handle the attributes separately from their container. 226 227 if attrs and not attrs.empty(): 228 for attr in attrs.nodes: 229 attr.to_string(self) 230 231 self.out(">") 232 233 def end_table_cell(self): 234 self.out("</td>") 235 236 def start_table_row(self): 237 self.out("<tr>") 238 239 def end_table_row(self, trailing): 240 self.out("</tr>") 241 242 def start_underline(self): 243 self.out("<span style='text-decoration: underline'>") 244 245 def end_underline(self): 246 self.out("</span>") 247 248 def break_(self): 249 pass 250 251 def linebreak(self): 252 self.out("<br />") 253 254 def rule(self, length): 255 self.out("<hr style='height: %dpt' />" % min(length, 10)) 256 257 def table_attrs(self, nodes): 258 259 # Skip the attributes in their original form. 260 261 pass 262 263 def table_attr(self, name, value, concise, quote): 264 self.out(" %s%s" % (escape_text(name), value is not None and 265 "='%s'" % escape_attr(value) or "")) 266 267 def text(self, s): 268 self.out(escape_text(s)) 269 270 serialiser = HTMLSerialiser 271 272 # vim: tabstop=4 expandtab shiftwidth=4