]> oss.titaniummirror.com Git - webber.git/commitdiff
plugins/hierarchy.py: index _parents{} and _childs{} by File objects
authorHolger Schurig <holgerschurig@gmail.com>
Thu, 13 Jan 2011 11:17:06 +0000 (12:17 +0100)
committerHolger Schurig <holgerschurig@gmail.com>
Thu, 13 Jan 2011 11:17:06 +0000 (12:17 +0100)
Previously the keys for _parents{} and _childs{} where linktitles.
This made problems when several pages had the same linktitle. Having
two pages with the same linktitle however was perfectly legal, as the
linktitle is supposed to be displayed in sidemenus or breadcrumbs,
when the normal page title is too long.

plugins/hierarchy.py

index 427de0fdfb8d481b28a60cecc9339523622afad7..184d66b926b7ee703612f906f978d9966125ebf3 100644 (file)
@@ -5,32 +5,35 @@ import re
 
 reSPLIT = re.compile(r',\s*')
 
+# This hashes store webber.File instances
 _childs = {}
 _parent = {}
 
-def memorize_links(title, links):
+def memorize_links(thisfile, links):
        global _childs
        if not links:
                return
        order = 100
        for link in reSPLIT.split(links):
-               #print title, link
-               if not _childs.has_key(title):
-                       _childs[title] = []
-               _childs[title].append( (order,link))
+               linked = get_file_for(link)
+               if not _childs.has_key(thisfile):
+                       _childs[thisfile] = []
+               _childs[thisfile].append( (order, linked))
                order += 100
-               _parent[link] = title
+               #print "memorize_links:", thisfile, "->", linked
+               _parent[linked] = thisfile
 
 
-def memorize_parent(title, parent, order=100):
-       #print "memorize_parent:", title, parent
-       #print "  parent:", _parent
-       #print "  childs:", _childs
-       #print "order:", title, order
+def memorize_parent(thisfile, parent, order=100):
+       # Convert titles or linktitle to entries of webber.File
+       if not isinstance(parent, webber.File):
+               parent = get_file_for(parent)
+
        if not _childs.has_key(parent):
                _childs[parent] = []
-       _childs[parent].append( (order, title) )
-       _parent[title] = parent
+       _childs[parent].append( (order, thisfile) )
+       #print "memorize_parent:", thisfile, "->", parent
+       _parent[thisfile] = parent
 
 
 #
@@ -49,13 +52,13 @@ def scan(params):
                return
 
        if file.has_key("links"):
-               memorize_links(file.title, file.links)
+               memorize_links(file, file.links)
        if file.has_key("parent"):
                if file.has_key("order"):
                        order = int(file.order)
                else:
                        order = 100
-               memorize_parent(file.title, file.parent, order)
+               memorize_parent(file, file.parent, order)
 
 
 @set_hook("scan_done")
@@ -64,14 +67,19 @@ def scan_done(params):
        in ascending order."""
 
        for c in _childs:
-               _childs[c].sort()
+               # Sort by linktitle
+               _childs[c].sort(key = lambda x: x[1].linktitle)
+               # And now sort by priority. Since Python 2.2 and upwards has stable-sort,
+               # this effectively makes a two-dimensional sort.
+               _childs[c].sort(key = lambda x: x[0])
        return
 
        print "_parent:"
        for c in _parent:
                print " ", c, _parent[c]
        print "_childs:"
-       for c in _childs: print " ", c,_childs[c]
+       for c in _childs:
+               print " ", c,_childs[c]
 
 
 @set_function("get_breadcrumbs")
@@ -80,33 +88,30 @@ def get_breadcrumbs(orig_page=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.title):
-               page = get_file_for(_parent[page.title])
+       while _parent.has_key(page):
+               page = _parent[page]
                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(root="Home", level=1):
+       """Returns (level, part_of_path, is_current, page, link) tuples, where
+    page is a class File object and link is a relative link from the current
+    page to page."""
        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
-
        res = [(0, 1, int(root==page), root, get_link_from(page, root))]
 
+       bread = get_breadcrumbs()
+
        def do_menu(pg, level):
-               #print "pg, has_key:", pg, _childs.has_key(pg)
-               if _childs.has_key(pg.title):
-                       for p in _childs[pg.title]:
-                               subpage = get_file_for(p[1])
+               if _childs.has_key(pg):
+                       for p in _childs[pg]:
+                               subpage = p[1]
                                in_bread = False
                                for b in bread:
                                        if b[0] == subpage:
@@ -114,7 +119,6 @@ def get_sidemenu(root="Home", level=1):
                                                break
 
                                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, in_bread, int(subpage==page), subpage, link))
                                if go_deeper:
@@ -123,6 +127,11 @@ def get_sidemenu(root="Home", 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, level)
+
+       # print "-" * 77
+       # import pprint
+       # pprint.pprint(res)
+       # print "-" * 77
        return res
 
 
@@ -136,9 +145,9 @@ def get_hierarchical_sitemap(root="Home", show_orphans=False):
        visited = {root: True}
        def do_menu(pg):
                res = []
-               if _childs.has_key(pg.title):
-                       for p in _childs[pg.title]:
-                               subpage = get_file_for(p[1])
+               if _childs.has_key(pg):
+                       for p in _childs[pg]:
+                               subpage = p[1]
                                visited[subpage] = True
                                res.append( do_menu(subpage) )
                return (pg, get_link_from(root, pg), res)
@@ -177,9 +186,9 @@ def get_linear_sitemap(root="Home", show_orphans=False, level=1):
        def do_menu(pg, level):
                #print "pg:", pg
                #, _childs.has_key(pg.title)
-               if _childs.has_key(pg.title):
-                       for p in _childs[pg.title]:
-                               subpage = get_file_for(p[1])
+               if _childs.has_key(pg):
+                       for p in _childs[pg]:
+                               subpage = p[1]
 
                                #print "subpage:", subpage
                                link = get_link_from(page, subpage)