SVN: r25709 - trunk/quixote

Neil Schemenauer <nascheme-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]> Tue, 7 Dec 2004 17:10:24 -0500
Newsgroups gmane.comp.web.quixote.cvs
Message-ID <[email protected]>
Author: nascheme
Date: 2004-12-07 17:10:09 -0500 (Tue, 07 Dec 2004)
New Revision: 25709

Modified:
   trunk/quixote/util.py
Log:
Generate a valid HTML document when listing static directories.  Also, use
get_path() instead of REQUEST_URI.


Modified: trunk/quixote/util.py
===================================================================
--- trunk/quixote/util.py	2004-12-07 21:51:51 UTC (rev 25708)
+++ trunk/quixote/util.py	2004-12-07 22:10:09 UTC (rev 25709)
@@ -76,7 +76,7 @@
         __import__(module_name)
         return getattr(sys.modules[module_name], object_name)
     else:
-        __import__(name)        
+        __import__(name)
         return sys.modules[name]
 
 def xmlrpc(request, func):
@@ -247,6 +247,20 @@
             self.file_class = self.FILE_CLASS
         self.index_filenames = index_filenames
 
+    def _render_header(self, title):
+        r = TemplateIO(html=True)
+        r += htmltext('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 '
+                      'Transitional//EN" '
+                      '"http://www.w3.org/TR/REC-html40/loose.dtd">')
+        r += htmltext('<html>')
+        r += htmltext('<head><title>%s</title></head>') % title
+        r += htmltext('<body>')
+        r += htmltext("<h1>%s</h1>") % title
+        return r.getvalue()
+
+    def _render_footer(self):
+        return htmltext('</body></html>')
+
     def _q_index(self):
         """
         If directory listings are allowed, generate a simple HTML
@@ -262,27 +276,26 @@
                     continue
                 if not isinstance(obj, StaticDirectory) and callable(obj):
                     return obj()
-        # FIXME: this is not a valid HTML document!
-        out = StringIO()
+        r = TemplateIO(html=True)
         if self.list_directory:
-            template = htmltext('<a href="%s">%s</a>%s')
-            print >>out, (htmltext("<h1>%s</h1>")
-                          % quixote.get_request().get_environ('REQUEST_URI'))
-            print >>out, "<pre>"
-            print >>out, template % ('..', '..', '')
+            r += self._render_header('Index of %s' % quixote.get_path())
+            template = htmltext('<a href="%s">%s</a>%s\n')
+            r += htmltext('<pre>')
+            r += template % ('..', '..', '')
             files = os.listdir(self.path)
             files.sort()
             for filename in files:
                 filepath = os.path.join(self.path, filename)
                 marker = os.path.isdir(filepath) and "/" or ""
-                print >>out, \
-                        template % (urllib.quote(filename), filename, marker)
-            print >>out, "</pre>"
+                r += template % (urllib.quote(filename), filename, marker)
+            r += htmltext('</pre>')
+            r += self._render_footer()
         else:
-            print >>out, "<h1>Directory listing denied</h1>"
-            print >>out, \
-                "<p>This directory does not allow its contents to be listed.</p>"
-        return out.getvalue()
+            r += self._render_header('Directory listing denied')
+            r += htmltext('<p>This directory does not allow its contents '
+                          'to be listed.</p>')
+            r += self._render_footer()
+        return r.getvalue()
 
     def _q_lookup(self, name):
         """