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, num): 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 start = style_type and num is not None and ' start="%s"' % escape_attr(num) or "" 116 self.out("<%s%s%s>" % (tag, style, start)) 117 118 def end_list(self, indent, marker, num): 119 tag, style = self._get_list_tag(marker) 120 self.out("</%s>" % tag) 121 122 def start_listitem(self, indent, marker, space, num): 123 self.out("<li>") 124 125 def end_listitem(self, indent, marker, space, num): 126 self.out("</li>") 127 128 def start_monospace(self): 129 self.out("<tt>") 130 131 def end_monospace(self): 132 self.out("</tt>") 133 134 def start_smaller(self): 135 self.out("<small>") 136 137 def end_smaller(self): 138 self.out("</small>") 139 140 def start_strikethrough(self): 141 self.out("<del>") 142 143 def end_strikethrough(self): 144 self.out("</del>") 145 146 def start_strong(self): 147 self.out("<strong>") 148 149 def end_strong(self): 150 self.out("</strong>") 151 152 def start_subscript(self): 153 self.out("<sub>") 154 155 def end_subscript(self): 156 self.out("</sub>") 157 158 def start_superscript(self): 159 self.out("<sup>") 160 161 def end_superscript(self): 162 self.out("</sup>") 163 164 def start_table(self): 165 self.out("<table>") 166 167 def end_table(self): 168 self.out("</table>") 169 170 def start_table_attrs(self): 171 pass 172 173 def end_table_attrs(self): 174 pass 175 176 def start_table_cell(self, attrs): 177 self.out("<td") 178 if attrs and not attrs.empty(): 179 attrs.to_string(self) 180 self.out(">") 181 182 def end_table_cell(self): 183 self.out("</td>") 184 185 def start_table_row(self): 186 self.out("<tr>") 187 188 def end_table_row(self, trailing): 189 self.out("</tr>") 190 191 def start_underline(self): 192 self.out("<span style='text-decoration: underline'>") 193 194 def end_underline(self): 195 self.out("</span>") 196 197 def break_(self): 198 pass 199 200 def rule(self, length): 201 self.out("<hr style='height: %dpt' />" % min(length, 10)) 202 203 def table_attr(self, name, value, concise, quote): 204 self.out(" %s%s" % (escape_text(name), value is not None and 205 "='%s'" % escape_attr(value) or "")) 206 207 def text(self, s): 208 self.out(escape_text(s)) 209 210 serialiser = HTMLSerialiser 211 212 # vim: tabstop=4 expandtab shiftwidth=4