#158: Allow '..' in template names, and absolute template paths;

Jinja 2 by default disables both.
This commit is contained in:
Georg Brandl 2009-06-04 18:39:47 +02:00
parent 137db91d77
commit be49adef8c
2 changed files with 31 additions and 1 deletions

View File

@ -1,6 +1,9 @@
Release 0.6.2 (in development) Release 0.6.2 (in development)
============================== ==============================
* #158: Allow '..' in template names, and absolute template paths;
Jinja 2 by default disables both.
* When highlighting Python code, ignore extra indentation before * When highlighting Python code, ignore extra indentation before
trying to parse it as Python. trying to parse it as Python.

View File

@ -15,6 +15,7 @@ from pprint import pformat
from jinja2 import FileSystemLoader, BaseLoader, TemplateNotFound, \ from jinja2 import FileSystemLoader, BaseLoader, TemplateNotFound, \
contextfunction contextfunction
from jinja2.utils import open_if_exists
from jinja2.sandbox import SandboxedEnvironment from jinja2.sandbox import SandboxedEnvironment
from sphinx.util import mtimes_of_files from sphinx.util import mtimes_of_files
@ -36,6 +37,32 @@ def accesskey(context, key):
return '' 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): class BuiltinTemplateLoader(TemplateBridge, BaseLoader):
""" """
Interfaces the rendering environment of jinja2 for use in Sphinx. Interfaces the rendering environment of jinja2 for use in Sphinx.
@ -65,7 +92,7 @@ class BuiltinTemplateLoader(TemplateBridge, BaseLoader):
self.pathchain = chain self.pathchain = chain
# make the paths into loaders # make the paths into loaders
self.loaders = map(FileSystemLoader, chain) self.loaders = map(SphinxFileSystemLoader, chain)
use_i18n = builder.translator is not None use_i18n = builder.translator is not None
extensions = use_i18n and ['jinja2.ext.i18n'] or [] extensions = use_i18n and ['jinja2.ext.i18n'] or []