1.1 --- a/moinformat/themes/common.py Fri Jul 12 21:54:20 2019 +0200
1.2 +++ b/moinformat/themes/common.py Fri Jul 12 22:04:11 2019 +0200
1.3 @@ -19,10 +19,10 @@
1.4 this program. If not, see <http://www.gnu.org/licenses/>.
1.5 """
1.6
1.7 +from moinformat.utils.file import readfile
1.8 from os import listdir, makedirs
1.9 from os.path import exists, isfile, join, split
1.10 from shutil import copy
1.11 -import codecs
1.12
1.13 class Theme:
1.14
1.15 @@ -41,6 +41,10 @@
1.16 self.linker = metadata.get_linker()
1.17 self.output = metadata.get_output()
1.18
1.19 + # Determine whether to bundle styles within documents.
1.20 +
1.21 + self.bundle = metadata.get("bundle")
1.22 +
1.23 def apply(self, text):
1.24
1.25 "Apply this theme to the given 'text', returning a themed version."
1.26 @@ -53,11 +57,14 @@
1.27
1.28 return split(self.__class__.origin)[0]
1.29
1.30 - def get_resource(self, filename):
1.31 + def get_resource(self, filename, base=None):
1.32
1.33 - "Return the complete path for the resource with the given 'filename'."
1.34 + """
1.35 + Return the complete path for the resource with the given 'filename'. If
1.36 + the optional 'base' is given, use this as the location of 'filename'.
1.37 + """
1.38
1.39 - base = self.get_resource_base()
1.40 + base = base or self.get_resource_base()
1.41 return join(base, filename)
1.42
1.43 def install_resource(self, filename, target=None):
1.44 @@ -90,14 +97,24 @@
1.45 for filename in listdir(pathname):
1.46 self.copy(join(pathname, filename), join(outpath, filename))
1.47
1.48 - def load_resource(self, filename):
1.49 + # NOTE: Also defined in moinformat.input.common.
1.50 +
1.51 + def readpath(self, filename, encoding=None):
1.52
1.53 - "Return the textual content of the resource with the given 'filename'."
1.54 + """
1.55 + Return the contents of the file having the given 'filename'. If the
1.56 + optional 'encoding' is specified, override the general encoding.
1.57 + """
1.58
1.59 - f = codecs.open(self.get_resource(filename), encoding=self.default_encoding)
1.60 - try:
1.61 - return f.read()
1.62 - finally:
1.63 - f.close()
1.64 + return readfile(filename, encoding or self.default_encoding)
1.65 +
1.66 + def load_resource(self, filename, base=None):
1.67 +
1.68 + """
1.69 + Return the textual content of the resource with the given 'filename'. If
1.70 + the optional 'base' is given, use this as the location of 'filename'.
1.71 + """
1.72 +
1.73 + return self.readpath(self.get_resource(filename, base))
1.74
1.75 # vim: tabstop=4 expandtab shiftwidth=4