paul@38 | 1 | #!/usr/bin/env python |
paul@38 | 2 | |
paul@38 | 3 | """ |
paul@38 | 4 | HTML serialiser. |
paul@38 | 5 | |
paul@38 | 6 | Copyright (C) 2017 Paul Boddie <paul@boddie.org.uk> |
paul@38 | 7 | |
paul@38 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@38 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@38 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@38 | 11 | version. |
paul@38 | 12 | |
paul@38 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@38 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@38 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@38 | 16 | details. |
paul@38 | 17 | |
paul@38 | 18 | You should have received a copy of the GNU General Public License along with |
paul@38 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@38 | 20 | """ |
paul@38 | 21 | |
paul@38 | 22 | from moinformat.serialisers.common import escape_attr, escape_text, Serialiser |
paul@38 | 23 | |
paul@38 | 24 | class HTMLSerialiser(Serialiser): |
paul@38 | 25 | |
paul@38 | 26 | "Serialisation of the page." |
paul@38 | 27 | |
paul@38 | 28 | def start_region(self, level, indent, type): |
paul@38 | 29 | l = [] |
paul@38 | 30 | out = l.append |
paul@38 | 31 | if level: |
paul@38 | 32 | out("level-%d" % level) |
paul@38 | 33 | |
paul@38 | 34 | if indent: |
paul@38 | 35 | out("indent-%d" % indent) |
paul@38 | 36 | |
paul@38 | 37 | # NOTE: Encode type details for CSS. |
paul@38 | 38 | |
paul@38 | 39 | if type: |
paul@38 | 40 | out("type-%s" % escape_attr(type)) |
paul@38 | 41 | |
paul@38 | 42 | self.out("<span class='%s'>" % " ".join(l)) |
paul@38 | 43 | |
paul@38 | 44 | def end_region(self, level, indent, type): |
paul@38 | 45 | self.out("</span>") |
paul@38 | 46 | |
paul@38 | 47 | def start_block(self): |
paul@38 | 48 | self.out("<p>") |
paul@38 | 49 | |
paul@38 | 50 | def end_block(self): |
paul@38 | 51 | self.out("</p>") |
paul@38 | 52 | |
paul@38 | 53 | def start_defitem(self, pad, extra): |
paul@38 | 54 | self.out("<dd>") |
paul@38 | 55 | |
paul@38 | 56 | def end_defitem(self, pad, extra): |
paul@38 | 57 | self.out("</dd>") |
paul@38 | 58 | |
paul@38 | 59 | def start_defterm(self, pad): |
paul@38 | 60 | self.out("<dt>") |
paul@38 | 61 | |
paul@38 | 62 | def end_defterm(self, pad): |
paul@38 | 63 | self.out("</dt>") |
paul@38 | 64 | |
paul@38 | 65 | def start_emphasis(self): |
paul@38 | 66 | self.out("<em>") |
paul@38 | 67 | |
paul@38 | 68 | def end_emphasis(self): |
paul@38 | 69 | self.out("</em>") |
paul@38 | 70 | |
paul@38 | 71 | def start_heading(self, level, extra, pad): |
paul@38 | 72 | self.out("<h%d>" % level) |
paul@38 | 73 | |
paul@38 | 74 | def end_heading(self, level, pad, extra): |
paul@38 | 75 | self.out("</h%d>" % level) |
paul@38 | 76 | |
paul@38 | 77 | def start_larger(self): |
paul@38 | 78 | self.out("<big>") |
paul@38 | 79 | |
paul@38 | 80 | def end_larger(self): |
paul@38 | 81 | self.out("</big>") |
paul@38 | 82 | |
paul@43 | 83 | # NOTE: Need to employ list type information, perhaps moving it from the |
paul@43 | 84 | # NOTE: individual items. |
paul@43 | 85 | |
paul@43 | 86 | def start_list(self): |
paul@43 | 87 | self.out("<ul>") |
paul@43 | 88 | |
paul@43 | 89 | def end_list(self): |
paul@43 | 90 | self.out("</ul>") |
paul@43 | 91 | |
paul@38 | 92 | def start_listitem(self, indent, marker, space): |
paul@38 | 93 | self.out("<li>") |
paul@38 | 94 | |
paul@38 | 95 | def end_listitem(self, indent, marker): |
paul@38 | 96 | self.out("</li>") |
paul@38 | 97 | |
paul@38 | 98 | def start_monospace(self): |
paul@38 | 99 | self.out("<tt>") |
paul@38 | 100 | |
paul@38 | 101 | def end_monospace(self): |
paul@38 | 102 | self.out("</tt>") |
paul@38 | 103 | |
paul@38 | 104 | def start_smaller(self): |
paul@38 | 105 | self.out("<small>") |
paul@38 | 106 | |
paul@38 | 107 | def end_smaller(self): |
paul@38 | 108 | self.out("</small>") |
paul@38 | 109 | |
paul@38 | 110 | def start_strong(self): |
paul@38 | 111 | self.out("<strong>") |
paul@38 | 112 | |
paul@38 | 113 | def end_strong(self): |
paul@38 | 114 | self.out("</strong>") |
paul@38 | 115 | |
paul@38 | 116 | def start_subscript(self): |
paul@38 | 117 | self.out("<sub>") |
paul@38 | 118 | |
paul@38 | 119 | def end_subscript(self): |
paul@38 | 120 | self.out("</sub>") |
paul@38 | 121 | |
paul@38 | 122 | def start_superscript(self): |
paul@38 | 123 | self.out("<sup>") |
paul@38 | 124 | |
paul@38 | 125 | def end_superscript(self): |
paul@38 | 126 | self.out("</sup>") |
paul@38 | 127 | |
paul@38 | 128 | def start_table(self): |
paul@38 | 129 | self.out("<table>") |
paul@38 | 130 | |
paul@38 | 131 | def end_table(self): |
paul@38 | 132 | self.out("</table>") |
paul@38 | 133 | |
paul@38 | 134 | def start_table_attrs(self): |
paul@38 | 135 | pass |
paul@38 | 136 | |
paul@38 | 137 | def end_table_attrs(self): |
paul@38 | 138 | pass |
paul@38 | 139 | |
paul@38 | 140 | def start_table_cell(self, attrs): |
paul@38 | 141 | self.out("<td") |
paul@38 | 142 | if attrs and not attrs.empty(): |
paul@38 | 143 | attrs.to_string(self) |
paul@38 | 144 | self.out(">") |
paul@38 | 145 | |
paul@38 | 146 | def end_table_cell(self): |
paul@38 | 147 | self.out("</td>") |
paul@38 | 148 | |
paul@38 | 149 | def start_table_row(self): |
paul@38 | 150 | self.out("<tr>") |
paul@38 | 151 | |
paul@38 | 152 | def end_table_row(self, trailing): |
paul@38 | 153 | self.out("</tr>") |
paul@38 | 154 | |
paul@38 | 155 | def start_underline(self): |
paul@38 | 156 | self.out("<span style='text-decoration: underline'>") |
paul@38 | 157 | |
paul@38 | 158 | def end_underline(self): |
paul@38 | 159 | self.out("</span>") |
paul@38 | 160 | |
paul@38 | 161 | def break_(self): |
paul@38 | 162 | pass |
paul@38 | 163 | |
paul@38 | 164 | def rule(self, length): |
paul@38 | 165 | self.out("<hr style='height: %dpt' />" % min(length, 10)) |
paul@38 | 166 | |
paul@38 | 167 | def table_attr(self, name, value, concise, quote): |
paul@38 | 168 | self.out(" %s%s" % (escape_text(name), value is not None and |
paul@38 | 169 | "='%s'" % escape_attr(value) or "")) |
paul@38 | 170 | |
paul@38 | 171 | def text(self, s): |
paul@38 | 172 | self.out(escape_text(s)) |
paul@38 | 173 | |
paul@39 | 174 | serialiser = HTMLSerialiser |
paul@39 | 175 | |
paul@38 | 176 | # vim: tabstop=4 expandtab shiftwidth=4 |