changeset 300:6ad1e2221d55 trunk

Reenable includes to work without an search path. Closes #63.
author cmlenz
date Mon, 16 Oct 2006 08:08:13 +0000
parents 5b1d5460fe25
children 53d502c7c874
files genshi/plugin.py genshi/template.py genshi/tests/template.py
diffstat 3 files changed, 45 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/genshi/plugin.py
+++ b/genshi/plugin.py
@@ -51,7 +51,8 @@
             raise ConfigurationError('Invalid value for max_cache_size: "%s"' %
                                      max_cache_size)
 
-        self.loader = TemplateLoader(search_path, auto_reload=auto_reload,
+        self.loader = TemplateLoader(filter(None, search_path),
+                                     auto_reload=auto_reload,
                                      max_cache_size=max_cache_size)
 
     def load_template(self, templatename, template_string=None):
--- a/genshi/template.py
+++ b/genshi/template.py
@@ -1354,17 +1354,20 @@
                 pass
 
             search_path = self.search_path
+            isabs = False
 
             if os.path.isabs(filename):
                 # Bypass the search path if the requested filename is absolute
                 search_path = [os.path.dirname(filename)]
+                isabs = True
 
             elif relative_to and os.path.isabs(relative_to):
                 # Make sure that the directory containing the including
                 # template is on the search path
                 dirname = os.path.dirname(relative_to)
                 if dirname not in search_path:
-                    search_path = search_path[:] + [dirname]
+                    search_path = search_path + [dirname]
+                isabs = True
 
             elif not search_path:
                 # Uh oh, don't know where to look for the template
@@ -1375,6 +1378,14 @@
                 try:
                     fileobj = open(filepath, 'U')
                     try:
+                        if isabs:
+                            # If the filename of either the included or the 
+                            # including template is absolute, make sure the
+                            # included template gets an absolute path, too,
+                            # so that nested include work properly without a
+                            # search path
+                            filename = os.path.join(dirname, filename)
+                            dirname = ''
                         tmpl = cls(fileobj, basedir=dirname, filename=filename,
                                    loader=self)
                     finally:
--- a/genshi/tests/template.py
+++ b/genshi/tests/template.py
@@ -1235,6 +1235,37 @@
               <div>Included</div>
             </html>""", tmpl.generate().render())
 
+    def test_relative_include_without_search_path_nested(self):
+        file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
+        try:
+            file1.write("""<div>Included</div>""")
+        finally:
+            file1.close()
+
+        file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w')
+        try:
+            file2.write("""<div xmlns:xi="http://www.w3.org/2001/XInclude">
+              <xi:include href="tmpl1.html" />
+            </div>""")
+        finally:
+            file2.close()
+
+        file3 = open(os.path.join(self.dirname, 'tmpl3.html'), 'w')
+        try:
+            file3.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
+              <xi:include href="tmpl2.html" />
+            </html>""")
+        finally:
+            file3.close()
+
+        loader = TemplateLoader()
+        tmpl = loader.load(os.path.join(self.dirname, 'tmpl3.html'))
+        self.assertEqual("""<html>
+              <div>
+              <div>Included</div>
+            </div>
+            </html>""", tmpl.generate().render())
+
     def test_relative_include_from_inmemory_template(self):
         file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
         try:
Copyright (C) 2012-2017 Edgewall Software