paul@177 | 1 | #!/usr/bin/env python |
paul@177 | 2 | |
paul@177 | 3 | """ |
paul@177 | 4 | Common HTML theme functionality. |
paul@177 | 5 | |
paul@199 | 6 | Copyright (C) 2018, 2019 Paul Boddie <paul@boddie.org.uk> |
paul@177 | 7 | |
paul@177 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@177 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@177 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@177 | 11 | version. |
paul@177 | 12 | |
paul@177 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@177 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@177 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@177 | 16 | details. |
paul@177 | 17 | |
paul@177 | 18 | You should have received a copy of the GNU General Public License along with |
paul@177 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@177 | 20 | """ |
paul@177 | 21 | |
paul@177 | 22 | from moinformat.themes.common import Theme |
paul@177 | 23 | from os import listdir |
paul@177 | 24 | from os.path import splitext |
paul@177 | 25 | |
paul@177 | 26 | class HTMLTheme(Theme): |
paul@177 | 27 | |
paul@177 | 28 | "A common HTML theme abstraction." |
paul@177 | 29 | |
paul@177 | 30 | # Support a collection of links to stylesheets provided by each theme. |
paul@177 | 31 | |
paul@278 | 32 | link = '<link rel="stylesheet" type="text/css" charset="utf-8"' \ |
paul@278 | 33 | ' media="%(media)s" href="%(root)s/_css/%(filename)s" />\n' |
paul@278 | 34 | |
paul@278 | 35 | bundled_link = '<style type="text/css" media="%(media)s">\n%(data)s' \ |
paul@278 | 36 | '</style>\n' |
paul@177 | 37 | |
paul@278 | 38 | def get_links(self, subs, bundle=False): |
paul@177 | 39 | |
paul@278 | 40 | """ |
paul@278 | 41 | Using 'subs', return a string containing markup linking to resources if |
paul@278 | 42 | 'bundle' is a false value or omitted. If 'bundle' is a true value, |
paul@278 | 43 | incorporate resources within the document. |
paul@278 | 44 | """ |
paul@177 | 45 | |
paul@177 | 46 | d = {} |
paul@177 | 47 | d.update(subs) |
paul@177 | 48 | |
paul@278 | 49 | css = self.get_resource("css") |
paul@177 | 50 | links = [] |
paul@177 | 51 | |
paul@278 | 52 | for filename in listdir(css): |
paul@177 | 53 | |
paul@177 | 54 | # Only link to CSS files. |
paul@177 | 55 | |
paul@179 | 56 | basename, ext = splitext(filename) |
paul@179 | 57 | if ext != ".css": |
paul@177 | 58 | continue |
paul@177 | 59 | |
paul@179 | 60 | # Filenames can have the form <media>.css or <media>-<name>.css to |
paul@179 | 61 | # set the media type. |
paul@177 | 62 | |
paul@179 | 63 | t = basename.split("-", 1) |
paul@177 | 64 | |
paul@179 | 65 | d["media"] = t[0] or "all" |
paul@177 | 66 | d["filename"] = filename |
paul@177 | 67 | |
paul@278 | 68 | if bundle: |
paul@278 | 69 | d["data"] = self.load_resource(filename, css) |
paul@278 | 70 | |
paul@278 | 71 | template = bundle and self.bundled_link or self.link |
paul@278 | 72 | links.append(template % d) |
paul@177 | 73 | |
paul@177 | 74 | return "".join(links) |
paul@177 | 75 | |
paul@177 | 76 | # Public methods. |
paul@177 | 77 | |
paul@177 | 78 | def apply(self, text): |
paul@177 | 79 | |
paul@177 | 80 | "Apply this theme to the given 'text', returning a themed version." |
paul@177 | 81 | |
paul@177 | 82 | template = self.load_resource("template.html") |
paul@177 | 83 | subs = { |
paul@177 | 84 | "encoding" : self.output.encoding, |
paul@177 | 85 | "root" : self.linker.get_top_level() or ".", |
paul@177 | 86 | "text" : text, |
paul@199 | 87 | "title" : self.metadata.get("title") or self.metadata.get("pagename"), |
paul@177 | 88 | } |
paul@278 | 89 | subs["links"] = self.get_links(subs, self.bundle) |
paul@177 | 90 | return template % subs |
paul@177 | 91 | |
paul@177 | 92 | def install_resources(self): |
paul@177 | 93 | |
paul@177 | 94 | "Install resources for this theme." |
paul@177 | 95 | |
paul@278 | 96 | if not self.bundle: |
paul@278 | 97 | self.install_resource("css", "_css") |
paul@177 | 98 | |
paul@177 | 99 | # vim: tabstop=4 expandtab shiftwidth=4 |