# HG changeset patch # User Paul Boddie # Date 1562961851 -7200 # Node ID 3e6a19d2e0668cbf5e5d3df1d0a022b8ae0156b3 # Parent 2ddf8fb7e6f7f6bff4191b05648f584df368750f Added support for bundling stylesheets within documents for easier distribution of individual documents. diff -r 2ddf8fb7e6f7 -r 3e6a19d2e066 moinconvert --- a/moinconvert Fri Jul 12 21:54:20 2019 +0200 +++ b/moinconvert Fri Jul 12 22:04:11 2019 +0200 @@ -107,6 +107,7 @@ # Flags. all = False + bundle = False fragment = False macros = False tree = False @@ -120,6 +121,11 @@ if arg == "--all": all = True + # Detect resource bundling. + + elif arg == "--bundle": + bundle = True + # Detect fragment output (if serialising). elif arg == "--fragment": @@ -237,6 +243,7 @@ metadata = Metadata({ "attachments" : getvalue(attachments_dir, "attachments"), + "bundle" : bundle, "document_index" : getvalue(document_indexes), "input_context" : input_dir and \ getvalue(input_dir_types, "directory") or \ @@ -367,6 +374,9 @@ Output options: +--bundle Bundle resources such as stylesheets within every document, + useful for publishing documents that need to be copied or + distributed individually. --document-index Provide a "DocumentIndex" filename to be used in links in HTML format output, useful for local file browsing instead of Web-published content diff -r 2ddf8fb7e6f7 -r 3e6a19d2e066 moinformat/themes/common.py --- a/moinformat/themes/common.py Fri Jul 12 21:54:20 2019 +0200 +++ b/moinformat/themes/common.py Fri Jul 12 22:04:11 2019 +0200 @@ -19,10 +19,10 @@ this program. If not, see . """ +from moinformat.utils.file import readfile from os import listdir, makedirs from os.path import exists, isfile, join, split from shutil import copy -import codecs class Theme: @@ -41,6 +41,10 @@ self.linker = metadata.get_linker() self.output = metadata.get_output() + # Determine whether to bundle styles within documents. + + self.bundle = metadata.get("bundle") + def apply(self, text): "Apply this theme to the given 'text', returning a themed version." @@ -53,11 +57,14 @@ return split(self.__class__.origin)[0] - def get_resource(self, filename): + def get_resource(self, filename, base=None): - "Return the complete path for the resource with the given 'filename'." + """ + Return the complete path for the resource with the given 'filename'. If + the optional 'base' is given, use this as the location of 'filename'. + """ - base = self.get_resource_base() + base = base or self.get_resource_base() return join(base, filename) def install_resource(self, filename, target=None): @@ -90,14 +97,24 @@ for filename in listdir(pathname): self.copy(join(pathname, filename), join(outpath, filename)) - def load_resource(self, filename): + # NOTE: Also defined in moinformat.input.common. + + def readpath(self, filename, encoding=None): - "Return the textual content of the resource with the given 'filename'." + """ + Return the contents of the file having the given 'filename'. If the + optional 'encoding' is specified, override the general encoding. + """ - f = codecs.open(self.get_resource(filename), encoding=self.default_encoding) - try: - return f.read() - finally: - f.close() + return readfile(filename, encoding or self.default_encoding) + + def load_resource(self, filename, base=None): + + """ + Return the textual content of the resource with the given 'filename'. If + the optional 'base' is given, use this as the location of 'filename'. + """ + + return self.readpath(self.get_resource(filename, base)) # vim: tabstop=4 expandtab shiftwidth=4 diff -r 2ddf8fb7e6f7 -r 3e6a19d2e066 moinformat/themes/html.py --- a/moinformat/themes/html.py Fri Jul 12 21:54:20 2019 +0200 +++ b/moinformat/themes/html.py Fri Jul 12 22:04:11 2019 +0200 @@ -29,18 +29,27 @@ # Support a collection of links to stylesheets provided by each theme. - link = '\n' + link = '\n' + + bundled_link = '\n' - def get_links(self, subs): + def get_links(self, subs, bundle=False): - "Using 'subs', return a string containing markup linking to resources." + """ + Using 'subs', return a string containing markup linking to resources if + 'bundle' is a false value or omitted. If 'bundle' is a true value, + incorporate resources within the document. + """ d = {} d.update(subs) + css = self.get_resource("css") links = [] - for filename in listdir(self.get_resource("css")): + for filename in listdir(css): # Only link to CSS files. @@ -56,7 +65,11 @@ d["media"] = t[0] or "all" d["filename"] = filename - links.append(self.link % d) + if bundle: + d["data"] = self.load_resource(filename, css) + + template = bundle and self.bundled_link or self.link + links.append(template % d) return "".join(links) @@ -73,13 +86,14 @@ "text" : text, "title" : self.metadata.get("title") or self.metadata.get("pagename"), } - subs["links"] = self.get_links(subs) + subs["links"] = self.get_links(subs, self.bundle) return template % subs def install_resources(self): "Install resources for this theme." - self.install_resource("css", "_css") + if not self.bundle: + self.install_resource("css", "_css") # vim: tabstop=4 expandtab shiftwidth=4