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