1 #!/usr/bin/env python 2 3 """ 4 HTML serialiser. 5 6 Copyright (C) 2017 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_listitem(self, indent, marker, space): 84 self.out("<li>") 85 86 def end_listitem(self, indent, marker): 87 self.out("</li>") 88 89 def start_monospace(self): 90 self.out("<tt>") 91 92 def end_monospace(self): 93 self.out("</tt>") 94 95 def start_smaller(self): 96 self.out("<small>") 97 98 def end_smaller(self): 99 self.out("</small>") 100 101 def start_strong(self): 102 self.out("<strong>") 103 104 def end_strong(self): 105 self.out("</strong>") 106 107 def start_subscript(self): 108 self.out("<sub>") 109 110 def end_subscript(self): 111 self.out("</sub>") 112 113 def start_superscript(self): 114 self.out("<sup>") 115 116 def end_superscript(self): 117 self.out("</sup>") 118 119 def start_table(self): 120 self.out("<table>") 121 122 def end_table(self): 123 self.out("</table>") 124 125 def start_table_attrs(self): 126 pass 127 128 def end_table_attrs(self): 129 pass 130 131 def start_table_cell(self, attrs): 132 self.out("<td") 133 if attrs and not attrs.empty(): 134 attrs.to_string(self) 135 self.out(">") 136 137 def end_table_cell(self): 138 self.out("</td>") 139 140 def start_table_row(self): 141 self.out("<tr>") 142 143 def end_table_row(self, trailing): 144 self.out("</tr>") 145 146 def start_underline(self): 147 self.out("<span style='text-decoration: underline'>") 148 149 def end_underline(self): 150 self.out("</span>") 151 152 def break_(self): 153 pass 154 155 def rule(self, length): 156 self.out("<hr style='height: %dpt' />" % min(length, 10)) 157 158 def table_attr(self, name, value, concise, quote): 159 self.out(" %s%s" % (escape_text(name), value is not None and 160 "='%s'" % escape_attr(value) or "")) 161 162 def text(self, s): 163 self.out(escape_text(s)) 164 165 # vim: tabstop=4 expandtab shiftwidth=4