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