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