From 2f18cc98853884a524b40c224d501926a850bba3 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Sun, 15 Feb 2009 07:19:48 +0100 Subject: [PATCH] Add support for templated static files. --- sphinx/application.py | 11 +++++++++-- sphinx/builders/html.py | 11 ++++++++++- sphinx/jinja2glue.py | 4 ++++ 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/sphinx/application.py b/sphinx/application.py index 856f91086..0d87c493f 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -350,7 +350,14 @@ class TemplateBridge(object): def render(self, template, context): """ - Called by the builder to render a *template* with a specified - context (a Python dictionary). + Called by the builder to render a template given as a filename with a + specified context (a Python dictionary). + """ + raise NotImplementedError('must be implemented in subclasses') + + def render_string(self, template, context): + """ + Called by the builder to render a template given as a string with a + specified context (a Python dictionary). """ raise NotImplementedError('must be implemented in subclasses') diff --git a/sphinx/builders/html.py b/sphinx/builders/html.py index a24141641..3332aa144 100644 --- a/sphinx/builders/html.py +++ b/sphinx/builders/html.py @@ -460,7 +460,16 @@ class StandaloneHTMLBuilder(Builder): fullname = path.join(staticdirname, filename) targetname = path.join(self.outdir, '_static', filename) if path.isfile(fullname): - shutil.copyfile(fullname, targetname) + if fullname.lower().endswith('_t'): + # templated! + fsrc = open(fullname, 'rb') + fdst = open(targetname[:-2], 'wb') + fdst.write(self.templates.render_string( + fsrc.read(), self.globalcontext)) + fsrc.close() + fdst.close() + else: + shutil.copyfile(fullname, targetname) elif path.isdir(fullname): if filename in self.config.exclude_dirnames: continue diff --git a/sphinx/jinja2glue.py b/sphinx/jinja2glue.py index 9fc9ba3aa..b7f99e6f9 100644 --- a/sphinx/jinja2glue.py +++ b/sphinx/jinja2glue.py @@ -44,6 +44,7 @@ class BuiltinTemplateLoader(TemplateBridge, jinja2.BaseLoader): chain[0:0] = [path.join(builder.confdir, tp) for tp in builder.config.templates_path] + # store it for use in newest_template_mtime self.pathchain = chain # make the paths into loaders @@ -60,6 +61,9 @@ class BuiltinTemplateLoader(TemplateBridge, jinja2.BaseLoader): def render(self, template, context): return self.environment.get_template(template).render(context) + def render_string(self, source, context): + return self.environment.from_string(source).render(context) + def newest_template_mtime(self): return max(mtimes_of_files(self.pathchain, '.html'))