Attachment 'do_nearestsiblings.py'

Download

Toggle line numbers
   1     def do_nearestsiblings(self, root=None):
   2         """ Navigate from a subpage to its nearest siblings and its parent
   3 
   4             gsg 2005-11-15 - This is a modification of the do_siblings function existing in 1.5beta3.
   5         """
   6         _ = self._
   7         # get parent page name
   8         parent = root or _getParent(self.pagename)
   9         if not parent:
  10             return (self.macro.formatter.sysmsg(1) +
  11                     self.macro.formatter.text(_('No parent page found!'))+
  12                     self.macro.formatter.sysmsg(0))
  13 
  14         try:
  15             depth = int(self.args[1])
  16         except (IndexError, TypeError, ValueError):
  17             depth = 0
  18 
  19         # iterate over children, adding links to all of them
  20         result = []
  21 
  22         # gsg 2005-11-15 - start by adding parent for link upwards
  23         result.append('^')
  24         result.append(Page(self.macro.request, parent).link_to(self.macro.request, text=parent, querystr=self.querystr))
  25         result.append('^   ')
  26 
  27         children = _getPages(self.macro.request, '^%s/' % parent)
  28 
  29         # gsg 2005-11-15
  30         # - find current page and siblings either side (if they exist)
  31         # childrensubset will take 1,2 or 3 siblings including current page (one or both sides may not exist)
  32         children.sort()
  33         currentdayindex = children.index(self.pagename) 
  34         childrensubset = []
  35         if currentdayindex > 0:
  36             childrensubset.append(children[currentdayindex-1])
  37         childrensubset.append(children[currentdayindex])
  38         if currentdayindex+1 < len(children):
  39             childrensubset.append(children[currentdayindex+1])
  40         childindex = -1
  41 
  42         for child in childrensubset:
  43             # display short page name, leaving out the parent path
  44             # (and make sure the name doesn't get wrapped)
  45             shortname = child[len(parent):]
  46 
  47             # possibly limit depth
  48             if depth and shortname.count('/') > depth:
  49                 continue
  50 
  51             # gsg 2005-11-15 - now remove leading / in shortname because it doesn't look pretty
  52             shortname = shortname[1:]
  53 
  54             childindex += 1
  55 
  56             if child == self.pagename:
  57                 # do not link to focus
  58                 result.append(self.macro.formatter.text(shortname))
  59             else:
  60                 # gsg 2005-11-15 - if index == 0 then this must be a left link, else must be right link
  61                 if childindex == 0:
  62                     result.append('<')
  63                     # link to sibling / child
  64                     result.append(Page(self.macro.request, child).link_to(self.macro.request, text=shortname, querystr=self.querystr))
  65                     result.append('<&nbsp;&nbsp;&nbsp;')
  66                 else:
  67                     result.append('&nbsp;&nbsp;&nbsp;>')
  68                     # link to sibling / child
  69                     result.append(Page(self.macro.request, child).link_to(self.macro.request, text=shortname, querystr=self.querystr))
  70                     result.append('>')
  71 
  72         return ''.join(result)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2005-11-15 10:57:21, 3.0 KB) [[attachment:do_nearestsiblings.py]]
  • [get | view] (2005-11-15 10:55:10, 23.8 KB) [[attachment:nearestsiblings.jpg]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.