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 l = [] 45 out = l.append 46 if level: 47 out("level-%d" % level) 48 49 if indent: 50 out("indent-%d" % indent) 51 52 # NOTE: Encode type details for CSS. 53 54 out("type-%s" % escape_attr(type or "opaque")) 55 56 tag = self._region_tag(type) 57 self.out("<%s class='%s'>" % (tag, " ".join(l))) 58 59 def end_region(self, level, indent, type, extra): 60 tag = self._region_tag(type) 61 self.out("</%s>" % tag) 62 63 def start_block(self): 64 self.out("<p>") 65 66 def end_block(self): 67 self.out("</p>") 68 69 def start_defitem(self, pad, extra): 70 self.out("<dd>") 71 72 def end_defitem(self, pad, extra): 73 self.out("</dd>") 74 75 def start_defterm(self, pad): 76 self.out("<dt>") 77 78 def end_defterm(self, pad): 79 self.out("</dt>") 80 81 def start_emphasis(self): 82 self.out("<em>") 83 84 def end_emphasis(self): 85 self.out("</em>") 86 87 def start_heading(self, level, extra, pad): 88 self.out("<h%d>" % level) 89 90 def end_heading(self, level, pad, extra): 91 self.out("</h%d>" % level) 92 93 def start_larger(self): 94 self.out("<big>") 95 96 def end_larger(self): 97 self.out("</big>") 98 99 def start_link(self, target): 100 if self.linker: 101 target = self.linker.translate(target) 102 self.out('<a href="%s">' % escape_attr(target)) 103 104 def end_link(self): 105 self.out("</a>") 106 107 def start_linktext(self): 108 pass 109 110 def end_linktext(self): 111 pass 112 113 list_tags = { 114 "i" : "lower-roman", 115 "I" : "upper-roman", 116 "a" : "lower-latin", 117 "A" : "upper-latin", 118 } 119 120 def _get_list_tag(self, marker): 121 if marker: 122 if marker[0].isdigit(): 123 return "ol", "decimal" 124 style_type = self.list_tags.get(marker[0]) 125 if style_type: 126 return "ol", style_type 127 128 return "ul", None 129 130 def start_list(self, indent, marker, num): 131 tag, style_type = self._get_list_tag(marker) 132 style = style_type and ' style="list-style-type: %s"' % escape_attr(style_type) or "" 133 start = style_type and num is not None and ' start="%s"' % escape_attr(num) or "" 134 self.out("<%s%s%s>" % (tag, style, start)) 135 136 def end_list(self, indent, marker, num): 137 tag, style = self._get_list_tag(marker) 138 self.out("</%s>" % tag) 139 140 def start_listitem(self, indent, marker, space, num): 141 self.out("<li>") 142 143 def end_listitem(self, indent, marker, space, num): 144 self.out("</li>") 145 146 def start_macro(self, name, args, nodes): 147 self.out("<span class='macro'>") 148 149 # Fallback case for when macros are not replaced. 150 151 if not nodes: 152 self.out(escape_text("<<")) 153 self.out("<span class='name'>%s</span>" % escape_text(name)) 154 if args: 155 self.out("(") 156 first = True 157 for arg in args: 158 if not first: 159 self.out(",") 160 self.out("<span class='arg'>%s</span>" % escape_text(arg)) 161 first = False 162 if args: 163 self.out(")") 164 self.out(escape_text(">>")) 165 166 def end_macro(self): 167 self.out("</span>") 168 169 def start_monospace(self): 170 self.out("<tt>") 171 172 def end_monospace(self): 173 self.out("</tt>") 174 175 def start_smaller(self): 176 self.out("<small>") 177 178 def end_smaller(self): 179 self.out("</small>") 180 181 def start_strikethrough(self): 182 self.out("<del>") 183 184 def end_strikethrough(self): 185 self.out("</del>") 186 187 def start_strong(self): 188 self.out("<strong>") 189 190 def end_strong(self): 191 self.out("</strong>") 192 193 def start_subscript(self): 194 self.out("<sub>") 195 196 def end_subscript(self): 197 self.out("</sub>") 198 199 def start_superscript(self): 200 self.out("<sup>") 201 202 def end_superscript(self): 203 self.out("</sup>") 204 205 def start_table(self): 206 self.out("<table>") 207 208 def end_table(self): 209 self.out("</table>") 210 211 def start_table_attrs(self): 212 pass 213 214 def end_table_attrs(self): 215 pass 216 217 def start_table_cell(self, attrs): 218 self.out("<td") 219 if attrs and not attrs.empty(): 220 attrs.to_string(self) 221 self.out(">") 222 223 def end_table_cell(self): 224 self.out("</td>") 225 226 def start_table_row(self): 227 self.out("<tr>") 228 229 def end_table_row(self, trailing): 230 self.out("</tr>") 231 232 def start_underline(self): 233 self.out("<span style='text-decoration: underline'>") 234 235 def end_underline(self): 236 self.out("</span>") 237 238 def break_(self): 239 pass 240 241 def rule(self, length): 242 self.out("<hr style='height: %dpt' />" % min(length, 10)) 243 244 def table_attr(self, name, value, concise, quote): 245 self.out(" %s%s" % (escape_text(name), value is not None and 246 "='%s'" % escape_attr(value) or "")) 247 248 def text(self, s): 249 self.out(escape_text(s)) 250 251 serialiser = HTMLSerialiser 252 253 # vim: tabstop=4 expandtab shiftwidth=4