paul@38 | 1 | #!/usr/bin/env python |
paul@38 | 2 | |
paul@38 | 3 | """ |
paul@38 | 4 | HTML serialiser. |
paul@38 | 5 | |
paul@47 | 6 | Copyright (C) 2017, 2018 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@85 | 28 | format = "html" |
paul@85 | 29 | |
paul@52 | 30 | def _region_tag(self, type): |
paul@52 | 31 | |
paul@52 | 32 | # NOTE: Need to support types in general. |
paul@52 | 33 | |
paul@56 | 34 | type = type and type.split()[0] |
paul@52 | 35 | |
paul@52 | 36 | if type == "inline": |
paul@52 | 37 | return "tt" |
paul@52 | 38 | elif type in (None, "python"): |
paul@52 | 39 | return "pre" |
paul@52 | 40 | else: |
paul@52 | 41 | return "span" |
paul@52 | 42 | |
paul@67 | 43 | def start_region(self, level, indent, type, extra): |
paul@38 | 44 | l = [] |
paul@38 | 45 | out = l.append |
paul@38 | 46 | if level: |
paul@38 | 47 | out("level-%d" % level) |
paul@38 | 48 | |
paul@38 | 49 | if indent: |
paul@38 | 50 | out("indent-%d" % indent) |
paul@38 | 51 | |
paul@38 | 52 | # NOTE: Encode type details for CSS. |
paul@38 | 53 | |
paul@52 | 54 | out("type-%s" % escape_attr(type or "opaque")) |
paul@38 | 55 | |
paul@52 | 56 | tag = self._region_tag(type) |
paul@52 | 57 | self.out("<%s class='%s'>" % (tag, " ".join(l))) |
paul@38 | 58 | |
paul@67 | 59 | def end_region(self, level, indent, type, extra): |
paul@52 | 60 | tag = self._region_tag(type) |
paul@52 | 61 | self.out("</%s>" % tag) |
paul@38 | 62 | |
paul@38 | 63 | def start_block(self): |
paul@38 | 64 | self.out("<p>") |
paul@38 | 65 | |
paul@38 | 66 | def end_block(self): |
paul@38 | 67 | self.out("</p>") |
paul@38 | 68 | |
paul@38 | 69 | def start_defitem(self, pad, extra): |
paul@38 | 70 | self.out("<dd>") |
paul@38 | 71 | |
paul@38 | 72 | def end_defitem(self, pad, extra): |
paul@38 | 73 | self.out("</dd>") |
paul@38 | 74 | |
paul@38 | 75 | def start_defterm(self, pad): |
paul@38 | 76 | self.out("<dt>") |
paul@38 | 77 | |
paul@38 | 78 | def end_defterm(self, pad): |
paul@38 | 79 | self.out("</dt>") |
paul@38 | 80 | |
paul@38 | 81 | def start_emphasis(self): |
paul@38 | 82 | self.out("<em>") |
paul@38 | 83 | |
paul@38 | 84 | def end_emphasis(self): |
paul@38 | 85 | self.out("</em>") |
paul@38 | 86 | |
paul@38 | 87 | def start_heading(self, level, extra, pad): |
paul@38 | 88 | self.out("<h%d>" % level) |
paul@38 | 89 | |
paul@38 | 90 | def end_heading(self, level, pad, extra): |
paul@38 | 91 | self.out("</h%d>" % level) |
paul@38 | 92 | |
paul@38 | 93 | def start_larger(self): |
paul@38 | 94 | self.out("<big>") |
paul@38 | 95 | |
paul@38 | 96 | def end_larger(self): |
paul@38 | 97 | self.out("</big>") |
paul@38 | 98 | |
paul@47 | 99 | def start_link(self, target): |
paul@47 | 100 | self.out('<a href="%s">' % escape_attr(target)) |
paul@47 | 101 | |
paul@47 | 102 | def end_link(self): |
paul@47 | 103 | self.out("</a>") |
paul@47 | 104 | |
paul@47 | 105 | def start_linktext(self): |
paul@47 | 106 | pass |
paul@47 | 107 | |
paul@47 | 108 | def end_linktext(self): |
paul@47 | 109 | pass |
paul@47 | 110 | |
paul@50 | 111 | list_tags = { |
paul@50 | 112 | "i" : "lower-roman", |
paul@50 | 113 | "I" : "upper-roman", |
paul@50 | 114 | "a" : "lower-latin", |
paul@50 | 115 | "A" : "upper-latin", |
paul@50 | 116 | } |
paul@43 | 117 | |
paul@50 | 118 | def _get_list_tag(self, marker): |
paul@50 | 119 | if marker: |
paul@50 | 120 | if marker[0].isdigit(): |
paul@50 | 121 | return "ol", "decimal" |
paul@50 | 122 | style_type = self.list_tags.get(marker[0]) |
paul@50 | 123 | if style_type: |
paul@50 | 124 | return "ol", style_type |
paul@43 | 125 | |
paul@50 | 126 | return "ul", None |
paul@50 | 127 | |
paul@51 | 128 | def start_list(self, indent, marker, num): |
paul@50 | 129 | tag, style_type = self._get_list_tag(marker) |
paul@50 | 130 | style = style_type and ' style="list-style-type: %s"' % escape_attr(style_type) or "" |
paul@51 | 131 | start = style_type and num is not None and ' start="%s"' % escape_attr(num) or "" |
paul@51 | 132 | self.out("<%s%s%s>" % (tag, style, start)) |
paul@50 | 133 | |
paul@51 | 134 | def end_list(self, indent, marker, num): |
paul@50 | 135 | tag, style = self._get_list_tag(marker) |
paul@50 | 136 | self.out("</%s>" % tag) |
paul@43 | 137 | |
paul@51 | 138 | def start_listitem(self, indent, marker, space, num): |
paul@38 | 139 | self.out("<li>") |
paul@38 | 140 | |
paul@51 | 141 | def end_listitem(self, indent, marker, space, num): |
paul@38 | 142 | self.out("</li>") |
paul@38 | 143 | |
paul@38 | 144 | def start_monospace(self): |
paul@38 | 145 | self.out("<tt>") |
paul@38 | 146 | |
paul@38 | 147 | def end_monospace(self): |
paul@38 | 148 | self.out("</tt>") |
paul@38 | 149 | |
paul@38 | 150 | def start_smaller(self): |
paul@38 | 151 | self.out("<small>") |
paul@38 | 152 | |
paul@38 | 153 | def end_smaller(self): |
paul@38 | 154 | self.out("</small>") |
paul@38 | 155 | |
paul@48 | 156 | def start_strikethrough(self): |
paul@48 | 157 | self.out("<del>") |
paul@48 | 158 | |
paul@48 | 159 | def end_strikethrough(self): |
paul@48 | 160 | self.out("</del>") |
paul@48 | 161 | |
paul@38 | 162 | def start_strong(self): |
paul@38 | 163 | self.out("<strong>") |
paul@38 | 164 | |
paul@38 | 165 | def end_strong(self): |
paul@38 | 166 | self.out("</strong>") |
paul@38 | 167 | |
paul@38 | 168 | def start_subscript(self): |
paul@38 | 169 | self.out("<sub>") |
paul@38 | 170 | |
paul@38 | 171 | def end_subscript(self): |
paul@38 | 172 | self.out("</sub>") |
paul@38 | 173 | |
paul@38 | 174 | def start_superscript(self): |
paul@38 | 175 | self.out("<sup>") |
paul@38 | 176 | |
paul@38 | 177 | def end_superscript(self): |
paul@38 | 178 | self.out("</sup>") |
paul@38 | 179 | |
paul@38 | 180 | def start_table(self): |
paul@38 | 181 | self.out("<table>") |
paul@38 | 182 | |
paul@38 | 183 | def end_table(self): |
paul@38 | 184 | self.out("</table>") |
paul@38 | 185 | |
paul@38 | 186 | def start_table_attrs(self): |
paul@38 | 187 | pass |
paul@38 | 188 | |
paul@38 | 189 | def end_table_attrs(self): |
paul@38 | 190 | pass |
paul@38 | 191 | |
paul@38 | 192 | def start_table_cell(self, attrs): |
paul@38 | 193 | self.out("<td") |
paul@38 | 194 | if attrs and not attrs.empty(): |
paul@38 | 195 | attrs.to_string(self) |
paul@38 | 196 | self.out(">") |
paul@38 | 197 | |
paul@38 | 198 | def end_table_cell(self): |
paul@38 | 199 | self.out("</td>") |
paul@38 | 200 | |
paul@38 | 201 | def start_table_row(self): |
paul@38 | 202 | self.out("<tr>") |
paul@38 | 203 | |
paul@38 | 204 | def end_table_row(self, trailing): |
paul@38 | 205 | self.out("</tr>") |
paul@38 | 206 | |
paul@38 | 207 | def start_underline(self): |
paul@38 | 208 | self.out("<span style='text-decoration: underline'>") |
paul@38 | 209 | |
paul@38 | 210 | def end_underline(self): |
paul@38 | 211 | self.out("</span>") |
paul@38 | 212 | |
paul@38 | 213 | def break_(self): |
paul@38 | 214 | pass |
paul@38 | 215 | |
paul@77 | 216 | def macro(self, name, args): |
paul@77 | 217 | |
paul@77 | 218 | # NOTE: Special case. |
paul@77 | 219 | |
paul@77 | 220 | if name == "BR": |
paul@77 | 221 | self.out("<br />") |
paul@77 | 222 | return |
paul@77 | 223 | |
paul@77 | 224 | # Fallback case. |
paul@77 | 225 | |
paul@77 | 226 | self.out("<span class='macro'>") |
paul@77 | 227 | self.out(escape_text("<<")) |
paul@77 | 228 | self.out("<span class='name'>%s</span>" % escape_text(name)) |
paul@77 | 229 | if args: |
paul@77 | 230 | self.out("(") |
paul@77 | 231 | first = True |
paul@77 | 232 | for arg in args: |
paul@77 | 233 | if not first: |
paul@77 | 234 | self.out(",") |
paul@77 | 235 | self.out("<span class='arg'>%s</span>" % escape_text(arg)) |
paul@77 | 236 | first = False |
paul@77 | 237 | if args: |
paul@77 | 238 | self.out(")") |
paul@77 | 239 | self.out(escape_text(">>")) |
paul@77 | 240 | self.out("</span>") |
paul@77 | 241 | |
paul@38 | 242 | def rule(self, length): |
paul@38 | 243 | self.out("<hr style='height: %dpt' />" % min(length, 10)) |
paul@38 | 244 | |
paul@38 | 245 | def table_attr(self, name, value, concise, quote): |
paul@38 | 246 | self.out(" %s%s" % (escape_text(name), value is not None and |
paul@38 | 247 | "='%s'" % escape_attr(value) or "")) |
paul@38 | 248 | |
paul@38 | 249 | def text(self, s): |
paul@38 | 250 | self.out(escape_text(s)) |
paul@38 | 251 | |
paul@39 | 252 | serialiser = HTMLSerialiser |
paul@39 | 253 | |
paul@38 | 254 | # vim: tabstop=4 expandtab shiftwidth=4 |