Commits

Georg Brandl committed 2a5a8e0

#158: Allow '..' in template names, and absolute template paths;
Jinja 2 by default disables both.

Comments (0)

Files changed (2)

 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
   trying to parse it as Python.
 
 
 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
     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.
         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 []