diff options
author | Georg Brandl <georg@python.org> | 2009-06-04 18:39:47 +0200 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2009-06-04 18:39:47 +0200 |
commit | 69d6f5617e2be49789905f7ea3067fa903547500 (patch) | |
tree | a6e871271917b440289965099785f33302c95be6 /sphinx/jinja2glue.py | |
parent | c335e3655f03dde6482a7634823761aa9d800c81 (diff) | |
download | sphinx-69d6f5617e2be49789905f7ea3067fa903547500.tar.gz |
#158: Allow '..' in template names, and absolute template paths;
Jinja 2 by default disables both.
Diffstat (limited to 'sphinx/jinja2glue.py')
-rw-r--r-- | sphinx/jinja2glue.py | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/sphinx/jinja2glue.py b/sphinx/jinja2glue.py index 679447a8..c584a0cd 100644 --- a/sphinx/jinja2glue.py +++ b/sphinx/jinja2glue.py @@ -15,6 +15,7 @@ from pprint import pformat from jinja2 import FileSystemLoader, BaseLoader, TemplateNotFound, \ contextfunction +from jinja2.utils import open_if_exists from jinja2.sandbox import SandboxedEnvironment from sphinx.util import mtimes_of_files @@ -36,6 +37,32 @@ def accesskey(context, key): return '' +class SphinxFileSystemLoader(FileSystemLoader): + """FileSystemLoader subclass that is not so strict about '..' + entries in template names.""" + + def get_source(self, environment, template): + for searchpath in self.searchpath: + filename = path.join(searchpath, template) + f = open_if_exists(filename) + if f is None: + continue + try: + contents = f.read().decode(self.encoding) + finally: + f.close() + + mtime = path.getmtime(filename) + def uptodate(): + try: + return path.getmtime(filename) == mtime + except OSError: + return False + return contents, filename, uptodate + raise TemplateNotFound(template) + + + class BuiltinTemplateLoader(TemplateBridge, BaseLoader): """ Interfaces the rendering environment of jinja2 for use in Sphinx. @@ -65,7 +92,7 @@ class BuiltinTemplateLoader(TemplateBridge, BaseLoader): self.pathchain = chain # make the paths into loaders - self.loaders = map(FileSystemLoader, chain) + self.loaders = map(SphinxFileSystemLoader, chain) use_i18n = builder.translator is not None extensions = use_i18n and ['jinja2.ext.i18n'] or [] |