From: Holger Schurig Date: Thu, 25 Jun 2009 14:14:22 +0000 (+0200) Subject: history: convert all get_XXX() functions from the history plugin so X-Git-Url: https://oss.titaniummirror.com/gitweb?a=commitdiff_plain;h=8a0c0c329455d6bb1ae42b73ad1f3d7351bd4921;p=webber.git history: convert all get_XXX() functions from the history plugin so that they don't return file.title, file.mtime, file.whatever (or a subset of this), but instead the whole file object. Update documentation accordingly. --- diff --git a/in/functions.md b/in/functions.md index d45b2c8..43d4090 100644 --- a/in/functions.md +++ b/in/functions.md @@ -11,7 +11,7 @@ You can call functions only from [[template_mako]]. An example: Here's list of functions defined by webber and it's default plugins: -== format_date == +== format_date(timestamp) == Takes a timestamp (seconds since 1st January 1970) and converts it into a string, using to `cfg.date_format`. @@ -19,48 +19,57 @@ a string, using to `cfg.date_format`. Defined in `webber.py`. -== get_time == +== get_time() == Returns the current date/time as a string according to `cfg.date_format`. Defined in `webber.py`. -== get_breadcrumbs == +== get_breadcrumbs() == -Returns the breadcrumbs as "`(linktitle, link)`" tuples. +Returns the breadcrumbs as "`(page, link)`" tuples, where "`page`" is a "`class +File`"-object and link is a relative link from the current page to "`page`". Defined in [[hierarchy.py|hierarchy]], where you find an example. -== get_current_file == +== get_current_file() == -Returns the current `class File` object. +Returns the current "`class File`" object. Defined in `webber.py`. -== get_recently == +== get_recently(page) == -Returns a list of up to 10 pages below the current page. For each -page, you'll get a "`(mtime, ctime, title, link)`" tuple back. +Returns a list of up to 10 pages below the specified page. If you don't +specify a page, the current page will be used. For each page, you'll get a +"`(page, link)`" tuple back, where "`page`" is a "`class File`"-object and +link is a relative link from the current page to "`page`". Defined in [[hierarchy.py|hierarchy]], where you find an example. == get_sidemenu(root) == -Returns a menu for the current page. For each page in this menu you'll -get back a "`(level, part_of_path, is_current, title, link)`" tuple. +Returns a menu for the current page. For each page in this menu you'll get +back a "`(level, part_of_path, is_current, page, link)`" tuple, where +"`page`" is a "`class File`"-object and link is a relative link from the +current page to "`page`". + +You'll need to specify "`root`" if your top-most page isn't named "`Home`". Defined in [[hierarchy.py|hierarchy]], where you find an example. -== get_sitemap(home, show_orphans) == +== get_sitemap(root, show_orphans) == -Returns all pages as "`(level, title, link)`" tuples. +Returns all pages as "`(level, page, link)`" tuples, where "`page`" is a +"`class File`"-object and link is a relative link from the current page to +"`page`". -You'll need to specify "`root`" if your top-most page is a different one. +You'll need to specify "`root`" if your top-most page isn't named "`Home`". To put pages into the sitemap that are outside the parent/child relationships, specify "`True`" for "`show_orphans`". diff --git a/in/hierarchy.md b/in/hierarchy.md index 15b47a9..9fe5f89 100644 --- a/in/hierarchy.md +++ b/in/hierarchy.md @@ -77,23 +77,23 @@ page itself. = Generation of breadcrumbs = -This is done via a suitable [[template_mako]]. The -template uses the function "`get_breadcrumbs(linktitle)`" and returns -(linktitle, link) tuples. As a bonus: all the links are always relative to -the calling page. +This is done via a suitable [[template_mako]]. The template uses the +function "`get_breadcrumbs()`" and returns (linktitle, link) tuples. As a +bonus: all the links are always relative to the calling page. Here's a sample Mako template excerpt: \ + = Generation of a side-menu = This again is done via a suitable [[template_mako]]. The -template uses the function "`get_sidemenu(linktitle)`" and returns (level, +template uses the function "`get_sidemenu()`" and returns (level, part_of_path, is_current, title, link) tuples. Again all links are relative to the calling page. @@ -108,12 +108,12 @@ to the calling page. Here's a sample Mako template excerpt that converts this into a HTML menu: @@ -123,16 +123,16 @@ Here's a sample Mako template excerpt that converts this into a HTML menu: To get a list of recently changed pages, do this: <% - history = get_recently(get_current_file()) + history = get_recently()) %> % if len(history)>1:

Recent changed

- % for mtime,ctime,title,link in history: - % if mtime > ctime: - Modified ${format_date(mtime)}\ + % for page, link in history: + % if page.mtime > page.ctime: + Modified ${format_date(page.mtime)}\ % else: - Created ${format_date(ctime)}\ + Created ${format_date(page.ctime)}\ % endif - : ${title | entity}
+ : ${page.title | entity}
% endfor % endif diff --git a/in/style/history.tmpl b/in/style/history.tmpl index 089f576..5a3abb2 100644 --- a/in/style/history.tmpl +++ b/in/style/history.tmpl @@ -3,17 +3,17 @@ <%def name="contents()">\ ${body} <% - history = get_recently(get_current_file()) + recently = get_recently() %> -% if len(history)>1: +% if len(recently)>1:

What's new?

-% for mtime,ctime,title,link in history: -% if mtime > ctime: - Modified ${format_date(mtime)}\ +% for page, link in recently: +% if page.mtime > page.ctime: + Modified ${format_date(page.mtime)}\ % else: - Created ${format_date(ctime)}\ + Created ${format_date(page.ctime)}\ % endif -: ${title | entity}
+: ${page.title | entity}
% endfor % endif \ diff --git a/in/style/sitemap.tmpl b/in/style/sitemap.tmpl index cd13681..3d19274 100644 --- a/in/style/sitemap.tmpl +++ b/in/style/sitemap.tmpl @@ -3,11 +3,11 @@ <%def name="contents()">\ ${body} <% - site = get_sitemap(get_current_file()) + site = get_sitemap("Webber", True) %> \ diff --git a/plugins/hierarchy.py b/plugins/hierarchy.py index 09fbeae..1a33597 100644 --- a/plugins/hierarchy.py +++ b/plugins/hierarchy.py @@ -1,4 +1,5 @@ # -*- coding: iso-8859-1 -*- +import webber from webber import * import re @@ -69,50 +70,38 @@ def scan_done(params): @set_function("get_breadcrumbs") -def get_breadcrumbs(orig_page): - """Returns something like ['Home', 'Beruf', 'Werdegang']. This can - be easyly used to generate breadcrumbs HTML code.""" +def get_breadcrumbs(orig_page=None): + if orig_page is None: + orig_page = get_current_file() res = [(orig_page, get_link_from(orig_page, orig_page))] page = orig_page #print "orig_page:", orig_page - while _parent.has_key(page): - page = _parent[page] + while _parent.has_key(page.linktitle): + page = get_file_for(_parent[page.linktitle]) link = get_link_from(orig_page, page) #print " page, link:", page, link res.insert(0, (page, link)) + #print res return res - @set_function("get_sidemenu") -def get_sidemenu(page): - """Returns an array with a side-menu. Everything from the current - page upwards is shown, as well as one level below the current - position. The array has the following items: - - level part-of-path current-page title - - Example: - 0 1 0 Home - 1 1 0 Beruf - 2 0 0 Kenntnisse - 2 1 0 Werdegang - 3 0 1 Alte - 1 0 0 Haus - """ - # Determine root page: - bread = get_breadcrumbs(page) +def get_sidemenu(root="Home", level=1): + page = get_current_file() + if not isinstance(root, webber.File): + root = get_file_for(root) + + bread = get_breadcrumbs() #print "Menu for:", page #print "Bread:", bread - root = "Home" #TODO res = [(0, 1, int(root==page), root, get_link_from(page, root))] def do_menu(pg, level): #print "pg, has_key:", pg, _childs.has_key(pg) - if _childs.has_key(pg): - for p in _childs[pg]: - subpage = p[1] + if _childs.has_key(pg.linktitle): + for p in _childs[pg.linktitle]: + subpage = get_file_for(p[1]) in_bread = False for b in bread: if b[0] == subpage: @@ -122,101 +111,77 @@ def get_sidemenu(page): go_deeper = in_bread or (subpage==page) #print "subpage:", subpage, "in bread:", in_bread, "go deeper:", go_deeper link = get_link_from(page, subpage) - res.append((level, int(subpage in bread), int(subpage==page), subpage, link)) + res.append((level, in_bread, int(subpage==page), subpage, link)) if go_deeper: do_menu(subpage, level+1) # TODO: make this configurable, e.g. cfg.rootpage, otherwise a page # that is outside of the menu won't show a menu - do_menu(root, 1) + do_menu(root, level) return res @set_function("get_sitemap") -def get_sitemap(page): - # Determine root page: - root = "Home" #TODO - - res = [(0, get_file_for(root).title, get_link_from(page, root))] +def get_sitemap(root="Home", show_orphans=False, level=1): + page = get_current_file() + if not isinstance(root, webber.File): + root = get_file_for(root) + + res = [(0, root, get_link_from(page, root))] visited = {root: None} def do_menu(pg, level): - #print "pg, has_key:", pg, _childs.has_key(pg) - if _childs.has_key(pg): - for p in _childs[pg]: - subpage = p[1] + #print "pg:", pg + #, _childs.has_key(pg.linktitle) + if _childs.has_key(pg.linktitle): + for p in _childs[pg.linktitle]: + subpage = get_file_for(p[1]) - #print "subpage:", subpage, "in bread:", in_bread, "go deeper:", go_deeper + #print "subpage:", subpage link = get_link_from(page, subpage) res.append((level, subpage, link)) visited[subpage] = None do_menu(subpage, level+1) - do_menu(root, 1) - #print visited - for f in files: - #print f - file = files[f] - try: - if file.linktitle in visited: - #print "found", file.linktitle + do_menu(root, level) + + #print "visited:", visited + if show_orphans: + for f in files: + #print f + file = files[f] + if not file.has_key("linktitle"): continue - except KeyError: - continue - #print "not found:", file.linktitle - res.append( (0, file.title, get_link_from(page, file.linktitle))) + try: + if file in visited: + #print "found", file.linktitle + continue + except KeyError: + continue + #print "not found:", file.linktitle + res.append( (0, file, get_link_from(page, file.linktitle))) #for t in res: print t return res @set_function("get_recently") -def get_recently(file): - #file = get_current_file() - #print "XXXXXX:", file.linktitle - pg = [] - - max_n = 10 # TODO: configurable? - orig_page = file.linktitle - - def addPage(pg, title): - #print "addPage", title - for f in files: - file = files[f] - #print file - if file.has_key("linktitle") and file.linktitle == title: - pg.append( (file.mtime, file.ctime, file.title, get_link_from(orig_page, file.linktitle)) ) - if _childs.has_key(file.linktitle): - for c in _childs[file.linktitle]: - #print "c:", c - addPage(pg, c[1]) - if len(pg) == max_n: - return - addPage(pg, orig_page) - pg.sort(reverse=True) - #for p in pg: print p - return pg - - - - - -if __name__ == "__main__": - # You can call this test-code this way: - # - # PYTHONPATH=`pwd` python plugins/hierarchy.py - # - memorize_parent("Impressum", "Home", 99999) - memorize_parent("Beruf", "Home", 100) - memorize_parent("Werdegang", "Beruf", 100) - memorize_parent("Kenntnisse", "Beruf", 200) - scan_done(None) - - #print get_breadcrumbs("Home") - #print get_breadcrumbs("Beruf") - #print get_breadcrumbs("Werdegang") - #print get_breadcrumbs("Kenntnisse") - #for t in get_sidemenu("Home"): print t - #for t in get_sidemenu("Beruf"): print t - #for t in get_sidemenu("Kenntnisse"): print t - for t in get_sitemap("Home"): print t +def get_recently(page=None, max_items=10): + if page is None: + page = get_current_file() + elif not isinstance(page, webber.File): + page = get_file_for(page) + + res = [] + orig_page = page + + def addPage(res, page): + res.append( (page, get_link_from(orig_page, page)) ) + if _childs.has_key(page.linktitle): + for c in _childs[page.linktitle]: + if len(res) < max_items: + addPage(res, get_file_for(c[1])) + addPage(res, orig_page) + res.sort(cmp = lambda x,y: x[0].mtime < y[0].mtime) + #for p in res: print p + return res