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