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 # NOTE: Need to employ list type information, perhaps moving it from the 96 # NOTE: individual items. 97 98 def start_list(self): 99 self.out("<ul>") 100 101 def end_list(self): 102 self.out("</ul>") 103 104 def start_listitem(self, indent, marker, space): 105 self.out("<li>") 106 107 def end_listitem(self, indent, marker): 108 self.out("</li>") 109 110 def start_monospace(self): 111 self.out("<tt>") 112 113 def end_monospace(self): 114 self.out("</tt>") 115 116 def start_smaller(self): 117 self.out("<small>") 118 119 def end_smaller(self): 120 self.out("</small>") 121 122 def start_strong(self): 123 self.out("<strong>") 124 125 def end_strong(self): 126 self.out("</strong>") 127 128 def start_subscript(self): 129 self.out("<sub>") 130 131 def end_subscript(self): 132 self.out("</sub>") 133 134 def start_superscript(self): 135 self.out("<sup>") 136 137 def end_superscript(self): 138 self.out("</sup>") 139 140 def start_table(self): 141 self.out("<table>") 142 143 def end_table(self): 144 self.out("</table>") 145 146 def start_table_attrs(self): 147 pass 148 149 def end_table_attrs(self): 150 pass 151 152 def start_table_cell(self, attrs): 153 self.out("<td") 154 if attrs and not attrs.empty(): 155 attrs.to_string(self) 156 self.out(">") 157 158 def end_table_cell(self): 159 self.out("</td>") 160 161 def start_table_row(self): 162 self.out("<tr>") 163 164 def end_table_row(self, trailing): 165 self.out("</tr>") 166 167 def start_underline(self): 168 self.out("<span style='text-decoration: underline'>") 169 170 def end_underline(self): 171 self.out("</span>") 172 173 def break_(self): 174 pass 175 176 def rule(self, length): 177 self.out("<hr style='height: %dpt' />" % min(length, 10)) 178 179 def table_attr(self, name, value, concise, quote): 180 self.out(" %s%s" % (escape_text(name), value is not None and 181 "='%s'" % escape_attr(value) or "")) 182 183 def text(self, s): 184 self.out(escape_text(s)) 185 186 serialiser = HTMLSerialiser 187 188 # vim: tabstop=4 expandtab shiftwidth=4