1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/moinformat/themes/html.py Mon Nov 26 16:52:43 2018 +0100
1.3 @@ -0,0 +1,84 @@
1.4 +#!/usr/bin/env python
1.5 +
1.6 +"""
1.7 +Common HTML theme functionality.
1.8 +
1.9 +Copyright (C) 2018 Paul Boddie <paul@boddie.org.uk>
1.10 +
1.11 +This program is free software; you can redistribute it and/or modify it under
1.12 +the terms of the GNU General Public License as published by the Free Software
1.13 +Foundation; either version 3 of the License, or (at your option) any later
1.14 +version.
1.15 +
1.16 +This program is distributed in the hope that it will be useful, but WITHOUT
1.17 +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.18 +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.19 +details.
1.20 +
1.21 +You should have received a copy of the GNU General Public License along with
1.22 +this program. If not, see <http://www.gnu.org/licenses/>.
1.23 +"""
1.24 +
1.25 +from moinformat.themes.common import Theme
1.26 +from os import listdir
1.27 +from os.path import splitext
1.28 +
1.29 +class HTMLTheme(Theme):
1.30 +
1.31 + "A common HTML theme abstraction."
1.32 +
1.33 + # Support a collection of links to stylesheets provided by each theme.
1.34 +
1.35 + link = '<link rel="stylesheet" type="text/css" charset="utf-8" media="%(media)s" href="%(root)s/_css/%(filename)s" />\n'
1.36 +
1.37 + def get_links(self, subs):
1.38 +
1.39 + "Using 'subs', return a string containing markup linking to resources."
1.40 +
1.41 + d = {}
1.42 + d.update(subs)
1.43 +
1.44 + links = []
1.45 +
1.46 + for filename in listdir(self.get_resource("css")):
1.47 +
1.48 + # Only link to CSS files.
1.49 +
1.50 + if splitext(filename)[-1] != ".css":
1.51 + continue
1.52 +
1.53 + # Filenames can have the form <media>_<name>.css to set the media
1.54 + # type.
1.55 +
1.56 + t = filename.split("_")
1.57 +
1.58 + d["media"] = len(t) > 1 and t[0] or "all"
1.59 + d["filename"] = filename
1.60 +
1.61 + links.append(self.link % d)
1.62 +
1.63 + return "".join(links)
1.64 +
1.65 + # Public methods.
1.66 +
1.67 + def apply(self, text):
1.68 +
1.69 + "Apply this theme to the given 'text', returning a themed version."
1.70 +
1.71 + template = self.load_resource("template.html")
1.72 + subs = {
1.73 + "encoding" : self.output.encoding,
1.74 + "root" : self.linker.get_top_level() or ".",
1.75 + "text" : text,
1.76 + "title" : self.metadata.get("pagename"),
1.77 + }
1.78 + subs["links"] = self.get_links(subs)
1.79 + return template % subs
1.80 +
1.81 + def install_resources(self):
1.82 +
1.83 + "Install resources for this theme."
1.84 +
1.85 + self.install_resource("css", "_css")
1.86 +
1.87 +# vim: tabstop=4 expandtab shiftwidth=4