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@94 | 100 | if self.linker: |
paul@94 | 101 | target = self.linker.translate(target) |
paul@47 | 102 | self.out('<a href="%s">' % escape_attr(target)) |
paul@47 | 103 | |
paul@47 | 104 | def end_link(self): |
paul@47 | 105 | self.out("</a>") |
paul@47 | 106 | |
paul@47 | 107 | def start_linktext(self): |
paul@47 | 108 | pass |
paul@47 | 109 | |
paul@47 | 110 | def end_linktext(self): |
paul@47 | 111 | pass |
paul@47 | 112 | |
paul@50 | 113 | list_tags = { |
paul@50 | 114 | "i" : "lower-roman", |
paul@50 | 115 | "I" : "upper-roman", |
paul@50 | 116 | "a" : "lower-latin", |
paul@50 | 117 | "A" : "upper-latin", |
paul@50 | 118 | } |
paul@43 | 119 | |
paul@50 | 120 | def _get_list_tag(self, marker): |
paul@50 | 121 | if marker: |
paul@50 | 122 | if marker[0].isdigit(): |
paul@50 | 123 | return "ol", "decimal" |
paul@50 | 124 | style_type = self.list_tags.get(marker[0]) |
paul@50 | 125 | if style_type: |
paul@50 | 126 | return "ol", style_type |
paul@43 | 127 | |
paul@50 | 128 | return "ul", None |
paul@50 | 129 | |
paul@51 | 130 | def start_list(self, indent, marker, num): |
paul@50 | 131 | tag, style_type = self._get_list_tag(marker) |
paul@50 | 132 | style = style_type and ' style="list-style-type: %s"' % escape_attr(style_type) or "" |
paul@51 | 133 | start = style_type and num is not None and ' start="%s"' % escape_attr(num) or "" |
paul@51 | 134 | self.out("<%s%s%s>" % (tag, style, start)) |
paul@50 | 135 | |
paul@51 | 136 | def end_list(self, indent, marker, num): |
paul@50 | 137 | tag, style = self._get_list_tag(marker) |
paul@50 | 138 | self.out("</%s>" % tag) |
paul@43 | 139 | |
paul@51 | 140 | def start_listitem(self, indent, marker, space, num): |
paul@38 | 141 | self.out("<li>") |
paul@38 | 142 | |
paul@51 | 143 | def end_listitem(self, indent, marker, space, num): |
paul@38 | 144 | self.out("</li>") |
paul@38 | 145 | |
paul@89 | 146 | def start_macro(self, name, args, nodes): |
paul@89 | 147 | self.out("<span class='macro'>") |
paul@87 | 148 | |
paul@87 | 149 | # Fallback case for when macros are not replaced. |
paul@87 | 150 | |
paul@89 | 151 | if not nodes: |
paul@89 | 152 | self.out(escape_text("<<")) |
paul@89 | 153 | self.out("<span class='name'>%s</span>" % escape_text(name)) |
paul@89 | 154 | if args: |
paul@89 | 155 | self.out("(") |
paul@89 | 156 | first = True |
paul@89 | 157 | for arg in args: |
paul@89 | 158 | if not first: |
paul@89 | 159 | self.out(",") |
paul@89 | 160 | self.out("<span class='arg'>%s</span>" % escape_text(arg)) |
paul@89 | 161 | first = False |
paul@89 | 162 | if args: |
paul@89 | 163 | self.out(")") |
paul@89 | 164 | self.out(escape_text(">>")) |
paul@87 | 165 | |
paul@87 | 166 | def end_macro(self): |
paul@89 | 167 | self.out("</span>") |
paul@87 | 168 | |
paul@38 | 169 | def start_monospace(self): |
paul@38 | 170 | self.out("<tt>") |
paul@38 | 171 | |
paul@38 | 172 | def end_monospace(self): |
paul@38 | 173 | self.out("</tt>") |
paul@38 | 174 | |
paul@38 | 175 | def start_smaller(self): |
paul@38 | 176 | self.out("<small>") |
paul@38 | 177 | |
paul@38 | 178 | def end_smaller(self): |
paul@38 | 179 | self.out("</small>") |
paul@38 | 180 | |
paul@48 | 181 | def start_strikethrough(self): |
paul@48 | 182 | self.out("<del>") |
paul@48 | 183 | |
paul@48 | 184 | def end_strikethrough(self): |
paul@48 | 185 | self.out("</del>") |
paul@48 | 186 | |
paul@38 | 187 | def start_strong(self): |
paul@38 | 188 | self.out("<strong>") |
paul@38 | 189 | |
paul@38 | 190 | def end_strong(self): |
paul@38 | 191 | self.out("</strong>") |
paul@38 | 192 | |
paul@38 | 193 | def start_subscript(self): |
paul@38 | 194 | self.out("<sub>") |
paul@38 | 195 | |
paul@38 | 196 | def end_subscript(self): |
paul@38 | 197 | self.out("</sub>") |
paul@38 | 198 | |
paul@38 | 199 | def start_superscript(self): |
paul@38 | 200 | self.out("<sup>") |
paul@38 | 201 | |
paul@38 | 202 | def end_superscript(self): |
paul@38 | 203 | self.out("</sup>") |
paul@38 | 204 | |
paul@38 | 205 | def start_table(self): |
paul@38 | 206 | self.out("<table>") |
paul@38 | 207 | |
paul@38 | 208 | def end_table(self): |
paul@38 | 209 | self.out("</table>") |
paul@38 | 210 | |
paul@38 | 211 | def start_table_attrs(self): |
paul@38 | 212 | pass |
paul@38 | 213 | |
paul@38 | 214 | def end_table_attrs(self): |
paul@38 | 215 | pass |
paul@38 | 216 | |
paul@38 | 217 | def start_table_cell(self, attrs): |
paul@38 | 218 | self.out("<td") |
paul@38 | 219 | if attrs and not attrs.empty(): |
paul@38 | 220 | attrs.to_string(self) |
paul@38 | 221 | self.out(">") |
paul@38 | 222 | |
paul@38 | 223 | def end_table_cell(self): |
paul@38 | 224 | self.out("</td>") |
paul@38 | 225 | |
paul@38 | 226 | def start_table_row(self): |
paul@38 | 227 | self.out("<tr>") |
paul@38 | 228 | |
paul@38 | 229 | def end_table_row(self, trailing): |
paul@38 | 230 | self.out("</tr>") |
paul@38 | 231 | |
paul@38 | 232 | def start_underline(self): |
paul@38 | 233 | self.out("<span style='text-decoration: underline'>") |
paul@38 | 234 | |
paul@38 | 235 | def end_underline(self): |
paul@38 | 236 | self.out("</span>") |
paul@38 | 237 | |
paul@38 | 238 | def break_(self): |
paul@38 | 239 | pass |
paul@38 | 240 | |
paul@38 | 241 | def rule(self, length): |
paul@38 | 242 | self.out("<hr style='height: %dpt' />" % min(length, 10)) |
paul@38 | 243 | |
paul@38 | 244 | def table_attr(self, name, value, concise, quote): |
paul@38 | 245 | self.out(" %s%s" % (escape_text(name), value is not None and |
paul@38 | 246 | "='%s'" % escape_attr(value) or "")) |
paul@38 | 247 | |
paul@38 | 248 | def text(self, s): |
paul@38 | 249 | self.out(escape_text(s)) |
paul@38 | 250 | |
paul@39 | 251 | serialiser = HTMLSerialiser |
paul@39 | 252 | |
paul@38 | 253 | # vim: tabstop=4 expandtab shiftwidth=4 |