move layout.tmpl to sites/www.qmsk.net sites
authorTero Marttila <terom@fixme.fi>
Sat, 07 Feb 2009 16:55:23 +0200
branchsites
changeset 32 be954df4f0e8
parent 31 107062ebb6f9
child 33 19ea04f4b0cd
move layout.tmpl to sites/www.qmsk.net
lib/filesystem/map.py
lib/filesystem/page.py
lib/map.py
lib/template.py
sites/irclogs.qmsk.net/__init__.py
sites/www.qmsk.net/__init__.py
sites/www.qmsk.net/templates/layout.tmpl
templates/layout.tmpl
--- a/lib/filesystem/map.py	Sat Feb 07 16:33:27 2009 +0200
+++ b/lib/filesystem/map.py	Sat Feb 07 16:55:23 2009 +0200
@@ -16,13 +16,14 @@
         (template.TEMPLATE_EXT,     page.TemplatePage       ),
     ]
 
-    def __init__ (self, path) :
+    def __init__ (self, path, template) :
         """
             Create, path is where the pages are stored. The list of pages is loaded from $path/list
         """
         
         # store
-        self.path = path
+        self.path = path 
+        self.template = template
 
         # load the page tree
         self.tree = page_tree.PageTree(path + '/list')
@@ -118,7 +119,7 @@
         p.bind_request(request)
 
         # render the template
-        response_data = template.render("layout",
+        response_data = template.render(self.template,
             site_root_url   = request.get_script_dir(),
             site_page_url   = request.get_page_prefix(),
             page            = p,
--- a/lib/filesystem/page.py	Sat Feb 07 16:33:27 2009 +0200
+++ b/lib/filesystem/page.py	Sat Feb 07 16:55:23 2009 +0200
@@ -125,7 +125,7 @@
             Loads the .tmpl file, and renders it
         """
 
-        return template.render_file(self.path,
+        return template.TemplateLoader.render_file(self.path,
             request     = self.request,
             page_tree   = self.fs.tree
         )
--- a/lib/map.py	Sat Feb 07 16:33:27 2009 +0200
+++ b/lib/map.py	Sat Feb 07 16:55:23 2009 +0200
@@ -68,7 +68,7 @@
         # store
         self.mappings = mappings
 
-    def map_request (self, request) :
+    def handle_request (self, request) :
         """
             Returns the appropriate handler
         """
--- a/lib/template.py	Sat Feb 07 16:33:27 2009 +0200
+++ b/lib/template.py	Sat Feb 07 16:55:23 2009 +0200
@@ -5,7 +5,7 @@
 # use Mako
 from mako import exceptions
 from mako.template import Template
-from mako.lookup import TemplateLookup
+import mako.lookup
 
 # for http.ResponseError
 import http
@@ -21,11 +21,6 @@
 # template file extension
 TEMPLATE_EXT = "tmpl"
 
-
-# our Mako template lookup handler
-_lookup = TemplateLookup(directories=[TEMPLATE_DIR], module_directory=CACHE_DIR)
-
-
 class TemplateError (http.ResponseError) :
     """
         Raised by the template module functions
@@ -33,29 +28,7 @@
 
     pass
 
-def lookup (name) :
-    """
-        Looks up a template based on the bare "name", which does not include the path or file extension
-    """
-    
-    try :
-        return _lookup.get_template("%s.%s" % (name, TEMPLATE_EXT))
-
-    except :
-        raise TemplateError("Template broken: %r" % (name, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
-
-def load (path) :
-    """
-        Loads a template from a specific file
-    """
-
-    try :
-        return Template(filename=path, module_directory=CACHE_DIR)
-
-    except :
-        raise TemplateError("Template broken: %r" % (path, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
-
-def render_template (tpl, **params) :
+def render (tpl, **params) :
     """
         Render the given template, returning the output as a unicode string, or raising a TemplateError
     """
@@ -78,17 +51,59 @@
 
         raise TemplateError("Template render failed", status='500 Internal Server Error', details=details)
 
-def render (name, **params) :
+class TemplateLoader (mako.lookup.TemplateLookup) :
     """
-        Render a template, using lookup() on the given name
+        Our own specialization of mako's TemplateLookup
     """
 
-    return render_template(lookup(name), **params)
+    def __init__ (self, path, fileext=TEMPLATE_EXT) :
+        """
+            Initialize to load templates located at path, with the given file extension
+        """
 
-def render_file (path, **params) :
-    """
-        Render a template, using load() on the given path
-    """
+        # store
+        self.path = path
+        self.fileext = fileext
+        
+        # XXX: separate cache?
+        super(TemplateLoader, self).__init__(directories=[path], module_directory=CACHE_DIR)
 
-    return render_template(load(path), **params)
+    def lookup (self, name) :
+        """
+            Looks up a template based on the bare "name", which does not include the path or file extension
+        """
+        
+        try :
+            return self.get_template("%s.%s" % (name, self.fileext))
 
+        except :
+            raise TemplateError("Template broken: %r" % (name, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
+    
+    def render (name, **params) :
+        """
+            Render a template, using lookup() on the given name
+        """
+
+        return render(self.lookup(name), **params)
+
+    @classmethod
+    def load (cls, path) :
+        """
+            Loads a template from a specific file
+        """
+
+        try :
+            return Template(filename=path, module_directory=CACHE_DIR)
+
+        except :
+            raise TemplateError("Template broken: %r" % (path, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
+    
+    @classmethod
+    def render_file (cls, path, **params) :
+        """
+            Render a template, using load() on the given path
+        """
+
+        return render(cls.load(path), **params)
+
+
--- a/sites/irclogs.qmsk.net/__init__.py	Sat Feb 07 16:33:27 2009 +0200
+++ b/sites/irclogs.qmsk.net/__init__.py	Sat Feb 07 16:55:23 2009 +0200
@@ -3,5 +3,8 @@
 """
 
 # the URL mapper
-from urls import build_mapper
+import urls
 
+# our RequestHandler
+handler = urls.build_mapper()
+
--- a/sites/www.qmsk.net/__init__.py	Sat Feb 07 16:33:27 2009 +0200
+++ b/sites/www.qmsk.net/__init__.py	Sat Feb 07 16:55:23 2009 +0200
@@ -2,8 +2,9 @@
     The www.qmsk.net site is just a simple site with a filesystem-based URL mapping
 """
 
+from lib import template
 from lib.filesystem.map import FilesystemMapper as _fstree
 
 # global mapper attribute
-handler = _fstree("pages")
+handler = _fstree("pages", template=template.TemplateLoader.load("sites/www.qmsk.net/templates/layout.tmpl"))
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sites/www.qmsk.net/templates/layout.tmpl	Sat Feb 07 16:55:23 2009 +0200
@@ -0,0 +1,56 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+
+<%def name="render_menu(open_page, page, items, ancestry)">
+<ul>
+% for pi in items :
+    <li>
+        <a href="${site_page_url}/${pi.url}"${' class="selected-page"' if pi == open_page else ''}>${pi.title} ${'&raquo;' if pi.children and pi.parent else ''}</a>
+    % if pi in ancestry and pi.children and pi.parent :
+        ${render_menu(page, pi, pi.children, ancestry)}
+    % endif
+    </li>
+% endfor
+</ul>
+</%def>
+
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
+    <head>
+        <title>qmsk.net ${' :: ' + h.breadcrumb(menu.ancestry, links=False) if menu.ancestry else ''}</title>
+        <link rel="Stylesheet" type="text/css" href="${site_root_url}/static/style.css" />
+    </head>
+    <body>
+            <div id="header">
+                <a href="${site_page_url}/">QMSK.NET</a>
+            </div>
+            
+            <div id="container">
+                <div id="nav">
+                    ${render_menu(menu.page, menu.page, menu.items, menu.ancestry)}
+                </div>
+
+                <div id="content">
+                    <div id="breadcrumb">
+                        <!-- ${h.breadcrumb(menu.ancestry)} -->
+                    </div>
+                    ${page.content}
+                </div>
+                
+            </div>
+
+            <div id="footer">
+                <div id="footer-right">
+                    Page Modified ${page.modified} <br/>
+                    Current time ${h.now()}
+                </div>
+               
+                <div id="footer-left">
+                    &copy; ${h.copyright_year()} Tero Marttila
+                </div>
+                
+                <div id="footer-center">
+                    Validated <a href="http://validator.w3.org/check?uri=www.qmsk.net">XHTML 1.0 Strict</a> &amp; <a href="http://jigsaw.w3.org/css-validator/validator?uri=www.qmsk.net">CSS 2.1</a>
+                </div>
+            </div>
+    </body>
+</html>
+
--- a/templates/layout.tmpl	Sat Feb 07 16:33:27 2009 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
-<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-
-<%def name="render_menu(open_page, page, items, ancestry)">
-<ul>
-% for pi in items :
-    <li>
-        <a href="${site_page_url}/${pi.url}"${' class="selected-page"' if pi == open_page else ''}>${pi.title} ${'&raquo;' if pi.children and pi.parent else ''}</a>
-    % if pi in ancestry and pi.children and pi.parent :
-        ${render_menu(page, pi, pi.children, ancestry)}
-    % endif
-    </li>
-% endfor
-</ul>
-</%def>
-
-<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
-    <head>
-        <title>qmsk.net ${' :: ' + h.breadcrumb(menu.ancestry, links=False) if menu.ancestry else ''}</title>
-        <link rel="Stylesheet" type="text/css" href="${site_root_url}/static/style.css" />
-    </head>
-    <body>
-            <div id="header">
-                <a href="${site_page_url}/">QMSK.NET</a>
-            </div>
-            
-            <div id="container">
-                <div id="nav">
-                    ${render_menu(menu.page, menu.page, menu.items, menu.ancestry)}
-                </div>
-
-                <div id="content">
-                    <div id="breadcrumb">
-                        <!-- ${h.breadcrumb(menu.ancestry)} -->
-                    </div>
-                    ${page.content}
-                </div>
-                
-            </div>
-
-            <div id="footer">
-                <div id="footer-right">
-                    Page Modified ${page.modified} <br/>
-                    Current time ${h.now()}
-                </div>
-               
-                <div id="footer-left">
-                    &copy; ${h.copyright_year()} Tero Marttila
-                </div>
-                
-                <div id="footer-center">
-                    Validated <a href="http://validator.w3.org/check?uri=www.qmsk.net">XHTML 1.0 Strict</a> &amp; <a href="http://jigsaw.w3.org/css-validator/validator?uri=www.qmsk.net">CSS 2.1</a>
-                </div>
-            </div>
-    </body>
-</html>
-