diff -r 0ce1f471e9d7 -r 2a47b00f60b0 lib/page.py --- 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 """