paul@161 | 1 | #!/usr/bin/env python |
paul@161 | 2 | |
paul@161 | 3 | """ |
paul@161 | 4 | Theming common functionality. |
paul@161 | 5 | |
paul@226 | 6 | Copyright (C) 2018, 2019 Paul Boddie <paul@boddie.org.uk> |
paul@161 | 7 | |
paul@161 | 8 | This program is free software; you can redistribute it and/or modify it under |
paul@161 | 9 | the terms of the GNU General Public License as published by the Free Software |
paul@161 | 10 | Foundation; either version 3 of the License, or (at your option) any later |
paul@161 | 11 | version. |
paul@161 | 12 | |
paul@161 | 13 | This program is distributed in the hope that it will be useful, but WITHOUT |
paul@161 | 14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@161 | 15 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@161 | 16 | details. |
paul@161 | 17 | |
paul@161 | 18 | You should have received a copy of the GNU General Public License along with |
paul@161 | 19 | this program. If not, see <http://www.gnu.org/licenses/>. |
paul@161 | 20 | """ |
paul@161 | 21 | |
paul@278 | 22 | from moinformat.utils.file import readfile |
paul@161 | 23 | from os import listdir, makedirs |
paul@161 | 24 | from os.path import exists, isfile, join, split |
paul@161 | 25 | from shutil import copy |
paul@161 | 26 | |
paul@161 | 27 | class Theme: |
paul@161 | 28 | |
paul@161 | 29 | "A common theme abstraction." |
paul@161 | 30 | |
paul@226 | 31 | default_encoding = "utf-8" |
paul@226 | 32 | |
paul@165 | 33 | def __init__(self, metadata): |
paul@165 | 34 | |
paul@165 | 35 | "Initialise the theme with the given 'metadata'." |
paul@161 | 36 | |
paul@165 | 37 | self.metadata = metadata |
paul@161 | 38 | |
paul@165 | 39 | # Obtain essential metadata. |
paul@165 | 40 | |
paul@165 | 41 | self.linker = metadata.get_linker() |
paul@165 | 42 | self.output = metadata.get_output() |
paul@161 | 43 | |
paul@278 | 44 | # Determine whether to bundle styles within documents. |
paul@278 | 45 | |
paul@278 | 46 | self.bundle = metadata.get("bundle") |
paul@278 | 47 | |
paul@161 | 48 | def apply(self, text): |
paul@161 | 49 | |
paul@161 | 50 | "Apply this theme to the given 'text', returning a themed version." |
paul@161 | 51 | |
paul@161 | 52 | return text |
paul@161 | 53 | |
paul@161 | 54 | def get_resource_base(self): |
paul@161 | 55 | |
paul@161 | 56 | "Return the filesystem base of resources for instances of this class." |
paul@161 | 57 | |
paul@161 | 58 | return split(self.__class__.origin)[0] |
paul@161 | 59 | |
paul@278 | 60 | def get_resource(self, filename, base=None): |
paul@161 | 61 | |
paul@278 | 62 | """ |
paul@278 | 63 | Return the complete path for the resource with the given 'filename'. If |
paul@278 | 64 | the optional 'base' is given, use this as the location of 'filename'. |
paul@278 | 65 | """ |
paul@161 | 66 | |
paul@278 | 67 | base = base or self.get_resource_base() |
paul@161 | 68 | return join(base, filename) |
paul@161 | 69 | |
paul@161 | 70 | def install_resource(self, filename, target=None): |
paul@161 | 71 | |
paul@161 | 72 | """ |
paul@161 | 73 | Install the resource with the given 'filename' into a location having |
paul@161 | 74 | the given 'target' name (or 'filename' if 'target' is omitted). |
paul@161 | 75 | """ |
paul@161 | 76 | |
paul@165 | 77 | if not self.output.can_write(): |
paul@165 | 78 | return |
paul@165 | 79 | |
paul@161 | 80 | pathname = self.get_resource(filename) |
paul@161 | 81 | outpath = self.output.get_filename(target or filename) |
paul@161 | 82 | |
paul@161 | 83 | self.copy(pathname, outpath) |
paul@161 | 84 | |
paul@161 | 85 | def copy(self, pathname, outpath): |
paul@161 | 86 | |
paul@161 | 87 | "Copy 'pathname' to 'outpath'." |
paul@161 | 88 | |
paul@161 | 89 | if isfile(pathname): |
paul@161 | 90 | outdir = split(outpath)[0] |
paul@161 | 91 | if outdir and not exists(outdir): |
paul@161 | 92 | makedirs(outdir) |
paul@161 | 93 | copy(pathname, outpath) |
paul@161 | 94 | else: |
paul@161 | 95 | if not exists(outpath): |
paul@161 | 96 | makedirs(outpath) |
paul@161 | 97 | for filename in listdir(pathname): |
paul@161 | 98 | self.copy(join(pathname, filename), join(outpath, filename)) |
paul@161 | 99 | |
paul@278 | 100 | # NOTE: Also defined in moinformat.input.common. |
paul@278 | 101 | |
paul@278 | 102 | def readpath(self, filename, encoding=None): |
paul@161 | 103 | |
paul@278 | 104 | """ |
paul@278 | 105 | Return the contents of the file having the given 'filename'. If the |
paul@278 | 106 | optional 'encoding' is specified, override the general encoding. |
paul@278 | 107 | """ |
paul@161 | 108 | |
paul@278 | 109 | return readfile(filename, encoding or self.default_encoding) |
paul@278 | 110 | |
paul@278 | 111 | def load_resource(self, filename, base=None): |
paul@278 | 112 | |
paul@278 | 113 | """ |
paul@278 | 114 | Return the textual content of the resource with the given 'filename'. If |
paul@278 | 115 | the optional 'base' is given, use this as the location of 'filename'. |
paul@278 | 116 | """ |
paul@278 | 117 | |
paul@278 | 118 | return self.readpath(self.get_resource(filename, base)) |
paul@161 | 119 | |
paul@161 | 120 | # vim: tabstop=4 expandtab shiftwidth=4 |