]> oss.titaniummirror.com Git - webber.git/commitdiff
history: convert all get_XXX() functions from the history plugin so
authorHolger Schurig <holgerschurig@gmx.de>
Thu, 25 Jun 2009 14:14:22 +0000 (16:14 +0200)
committerHolger Schurig <holgerschurig@gmx.de>
Thu, 25 Jun 2009 14:22:02 +0000 (16:22 +0200)
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.

in/functions.md
in/hierarchy.md
in/style/history.tmpl
in/style/sitemap.tmpl
plugins/hierarchy.py

index d45b2c884d637a729d52a677c029db0071ac5730..43d40908a13ae206365d748290aca455f7c05628 100644 (file)
@@ -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`".
index 15b47a9567a2cbababda299883b80729b81a0876..9fe5f89d2cebc44ba12f9c3d1248504f2558253f 100644 (file)
@@ -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:
 
        <ul>\
-       % for linktitle, link in get_breadcrumbs(file.linktitle):
-       <li><a href="${link}">${linktitle}</a></li>\
+       % for page, link in get_breadcrumbs():
+       <li><a href="${link}">${page.linktitle}</a></li>\
        % endfor
        </ul>\
 
+
 = 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:
 
        <ul id="sidebar">
-       % for level, part_of_path, current, title, link in get_sidemenu(file.linktitle):
+       % for level, part_of_path, current, page, link in get_sidemenu():
        <li class="sidebar${level}"\
        %    if current:
-        id="sidebar_current">${title | entity}</li>
+        id="sidebar_current">${page.linktitle | entity}</li>
        %    else:
-       ><a href="${link}">${title | entity}</a></li>
+       ><a href="${link}">${page.linktitle | entity}</a></li>
        %    endif
        % endfor
        </ul>
@@ -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:
        <h2>Recent changed</h2>
-       %   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
-       : <a href="${link}">${title | entity}</a><br />
+       : <a href="${link}">${page.title | entity}</a><br />
        %   endfor
        % endif
index 089f576e0cac3c079119c5996e0e9eb603e2cb61..5a3abb28a367701855f61a000ffd03c781fba8e8 100644 (file)
@@ -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:
 <h2>What's new?</h2>
-%   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
-: <a href="${link}">${title | entity}</a><br />
+: <a href="${link}">${page.title | entity}</a><br />
 %   endfor
 % endif
 </%def>\
index cd136819e66ed3740421b782133a4023065e3e73..3d1927401893d81c3f72033d82d148f08270a686 100644 (file)
@@ -3,11 +3,11 @@
 <%def name="contents()">\
 ${body}
 <%
-  site = get_sitemap(get_current_file())
+  site = get_sitemap("Webber", True)
 %>
 <ul>
-% for level, title, link in site:
-<li class="sitemap${level}"><a href="${link}">${title}</a></li>
+% for level, page, link in site:
+<li class="sitemap${level}"><a href="${link}">${page.title}</a></li>
 % endfor
 </ul>
 </%def>\
index 09fbeaea98132d0832ec0a5d8d2eb97351a1dec3..1a33597cc4710b9759963c04391cc7e0b80d994c 100644 (file)
@@ -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