Add support for templated static files.

This commit is contained in:
Georg Brandl 2009-02-15 07:19:48 +01:00
parent 67242fd375
commit 2f18cc9885
3 changed files with 23 additions and 3 deletions

View File

@ -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')

View File

@ -460,6 +460,15 @@ class StandaloneHTMLBuilder(Builder):
fullname = path.join(staticdirname, filename)
targetname = path.join(self.outdir, '_static', filename)
if path.isfile(fullname):
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:

View File

@ -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'))