ChildNavPlugin: ChildNav.py

File ChildNav.py, 1.5 kB (added by mbertheau, 10 months ago)
Line 
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008 Markus Bertheau <mbertheau@gmail.com>
4 #
5 # Based on SiblingNav plugin by Wouter Bolsterlee <uws@xs4all.nl>
6 #
7 # This macro is put in the public domain by the author. Do whatever you want
8 # with it, but it would be nice if your improvements somehow come back to me.
9 #
10
11 from trac.wiki.api import WikiSystem
12 from trac.util.html import html, escape
13 from trac.wiki.macros import WikiMacroBase
14
15
16 class ChildNavMacro(WikiMacroBase):
17     '''
18     Show a navigation TOC of the current page hiearchy with a list of child
19     pages of the current page, excluding grand children.
20     '''
21
22     def render_macro(self, req, name, content):
23
24         current_page = req.args.get('page', 'WikiStart')
25         prefix = content or current_page
26
27         wiki = WikiSystem(self.env)
28
29         elements = []
30
31         # Build items for sibling pages (if any)
32         child_items = []
33         for page in sorted(wiki.get_pages(prefix)):
34             # skip grand children and self
35             child_part = page[len(prefix)+1:]
36             if "/" in child_part or child_part == '':
37                 continue
38
39             page_name = wiki.format_page_name(page[page.rfind('/')+1:])
40             link = html.A(page_name, href=req.href.wiki(page))
41             if page == current_page:
42                 li = html.LI(link, class_='active')
43             else:
44                 li = html.LI(link)
45             child_items.append(li)
46
47         if not child_items:
48             return None
49    
50         return html.UL(child_items)