1 #!/usr/bin/env python 2 3 """ 4 Common HTML theme functionality. 5 6 Copyright (C) 2018 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 if splitext(filename)[-1] != ".css": 48 continue 49 50 # Filenames can have the form <media>_<name>.css to set the media 51 # type. 52 53 t = filename.split("_") 54 55 d["media"] = len(t) > 1 and t[0] or "all" 56 d["filename"] = filename 57 58 links.append(self.link % d) 59 60 return "".join(links) 61 62 # Public methods. 63 64 def apply(self, text): 65 66 "Apply this theme to the given 'text', returning a themed version." 67 68 template = self.load_resource("template.html") 69 subs = { 70 "encoding" : self.output.encoding, 71 "root" : self.linker.get_top_level() or ".", 72 "text" : text, 73 "title" : self.metadata.get("pagename"), 74 } 75 subs["links"] = self.get_links(subs) 76 return template % subs 77 78 def install_resources(self): 79 80 "Install resources for this theme." 81 82 self.install_resource("css", "_css") 83 84 # vim: tabstop=4 expandtab shiftwidth=4