page list + menu
authorTero Marttila <terom@fixme.fi>
Fri, 06 Feb 2009 23:21:24 +0200
changeset 9 2a47b00f60b0
parent 8 0ce1f471e9d7
child 10 d83b10c210e3
page list + menu
lib/handler.py
lib/http.py
lib/menu.py
lib/page.py
pages/list
static/style.css
templates/layout.tmpl
--- a/lib/handler.py	Fri Feb 06 22:48:00 2009 +0200
+++ b/lib/handler.py	Fri Feb 06 23:21:24 2009 +0200
@@ -2,7 +2,7 @@
     The actual application behaviour, i.e. generating a Response from a Request :)
 """
 
-import http, page, template
+import http, page, menu, template
 
 def handle_request (request) :
     """
@@ -18,8 +18,8 @@
     # render the template
     response_data = template.render("layout",
         site_root_url   = request.get_script_dir(),
-        page_title      = p.get_title(),
-        page_content    = p.get_content(),
+        page            = p,
+        menu            = menu.Menu(p),
     )
     
     # return the response
--- a/lib/http.py	Fri Feb 06 22:48:00 2009 +0200
+++ b/lib/http.py	Fri Feb 06 23:21:24 2009 +0200
@@ -51,7 +51,15 @@
             /foo.cgi/quux/  -> quux/
         """
         
-        return os.path.normpath(self.env['PATH_INFO']).lstrip('/')
+        # the raw PATH_INFO
+        path_info = self.env.get('PATH_INFO')
+        
+        # avoid nasty '.' paths
+        if path_info :
+            return os.path.normpath(path_info).lstrip('/')
+
+        else :
+            return ''
 
 class Response (object) :
     """
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/menu.py	Fri Feb 06 23:21:24 2009 +0200
@@ -0,0 +1,23 @@
+"""
+    Handling the list of available pages
+"""
+
+# for page_list
+from page import page_list as _page_list
+
+class Menu (object) :
+    """
+        Contains info needed to render the menu
+    """
+
+    def __init__ (self, page) :
+        """
+            Gather the menu information for the given page
+        """
+
+        # the selected page
+        self.page = page
+        
+        # list of siblings
+        self.siblings = _page_list.get_siblings(page)
+    
--- a/lib/page.py	Fri Feb 06 22:48:00 2009 +0200
+++ b/lib/page.py	Fri Feb 06 23:21:24 2009 +0200
@@ -15,6 +15,9 @@
 # path to directory containing the page heirarcy
 PAGE_DIR = "pages"
 
+# path to directory containing the list of visible pages
+PAGE_LIST_FILE = os.path.join(PAGE_DIR, "list")
+
 class PageError (http.ResponseError) :
     """
         Error looking up/handling a page
@@ -22,6 +25,80 @@
 
     pass
 
+class PageList (object) :
+    """
+        The list of pages
+    """
+
+    def __init__ (self) :
+        """
+            Loads the page list from the list file
+        """
+
+        # initialize list of pages
+        self.pages = []
+
+        # load from file
+        self._load(PAGE_LIST_FILE)
+
+    def _load (self, path) :
+        """
+            Processes the lines in the given file
+        """
+
+        for line in open(path, 'rb') :
+            # ignore whitespace
+            line = line.strip()
+            
+            # ignore empty lines
+            if not line :
+                continue
+
+            # parse line
+            url, title = line.split(':')
+
+            # add
+            self._add_item(url.strip(), title.strip())
+    
+    def _add_item (self, url, title) :
+        """
+            Add item to pages list
+        """
+
+        self.pages.append((url, title))
+    
+    def get_title (self, page) :
+        """
+            Gets the title for the given page, or None if not found
+        """
+
+        return dict(self.pages).get(page)
+
+    def get_siblings (self, page) :
+        """
+            Gets the (url, title) tuple list of siblings (including the given page itself) for the given page
+        """
+    
+        siblings = []
+
+        # parent url
+        parent = os.path.split(page.url)[0]
+
+        # how many segments in the page name
+        segment_count = len(page.url.split('/'))
+        
+        # go through all pages
+        for url, title in self.pages :
+            # it's a sibling if the parent is the same, and the number of segments it the same
+            if url.startswith(parent) and len(url.split('/')) == segment_count :
+                siblings.append((url, title))
+        
+        # return
+        return siblings
+
+# global singleton instance
+page_list = PageList()
+
 class Page (object) :
     """
         This object represents the information about our attempt to render some specific page
@@ -53,16 +130,25 @@
         
         pass
 
-    def get_title (self) :
+    @property
+    def title (self) :
         """
             Return the page's title
 
-            Defaults to the Titlecase'd file basename
+            Defaults to the retreiving the page title from page_list, or basename in Titlecase.
         """
+        
+        # lookup in page_list
+        title = page_list.get_title(self.url)
+        
+        # fallback to titlecase
+        if not title :
+            title = self.basename.title()
 
-        return self.basename.title()
-
-    def get_content (self) :
+        return title
+    
+    @property
+    def content (self) :
         """
             Return the page content as a string
         """
@@ -74,7 +160,8 @@
         A simple .html page that's just passed through directly
     """
 
-    def get_content (self) :
+    @property
+    def content (self) :
         """
             Opens the .html file, reads and returns contents
         """
@@ -85,8 +172,9 @@
     """
         A template that's rendered using our template library
     """
-
-    def get_content (self) :
+    
+    @property
+    def content (self) :
         """
             Loads the .tmpl file, and renders it
         """
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pages/list	Fri Feb 06 23:21:24 2009 +0200
@@ -0,0 +1,5 @@
+
+index       : Index
+foo         : Foo
+about       : About page
+
--- a/static/style.css	Fri Feb 06 22:48:00 2009 +0200
+++ b/static/style.css	Fri Feb 06 23:21:24 2009 +0200
@@ -70,7 +70,7 @@
     text-decoration: none;
 }
 
-ul#nav li a#selected-page {
+ul#nav li a.selected-page {
     border-left: 5px solid black;
     padding-left: 15px;
 }
--- a/templates/layout.tmpl	Fri Feb 06 22:48:00 2009 +0200
+++ b/templates/layout.tmpl	Fri Feb 06 23:21:24 2009 +0200
@@ -2,7 +2,7 @@
 
 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
     <head>
-        <title>qmsk.net :: ${page_title}</title>
+        <title>qmsk.net :: ${page.title}</title>
         <link rel="Stylesheet" type="text/css" href="${site_root_url}/static/style.css" />
     </head>
     <body>
@@ -11,11 +11,13 @@
             </div>
  
             <ul id="nav">
-                <li><a href="#foo">Foo</a></li>
+            % for (url, title) in menu.siblings :
+                <li><a href="${url}"${' class="selected-page"' if url == page.url else ''}>${title}</a></li>
+            % endfor
             </ul>
 
             <div id="content">
-                ${page_content}
+                ${page.content}
             </div>
     </body>
 </html>