source: trac/trunk/wiki-macros/JPNav.py @ 2705

Revision 2705, 3.9 KB checked in by hauma, 8 years ago (diff)
  • Fancyfication: Sort the currently active top-level category to the first position in the navigation bar.
Line 
1# -*- coding: iso8859-1 -*-
2"""
3= TracNav: The navigation bar for Trac =
4
5This macro implements a fully customizable navigation bar for the Trac
6wiki engine. The contents of the navigation bar is a wiki page itself
7and can be edited like any other wiki page through the web
8interface. The navigation bar supports hierarchical ordering of
9topics. The design of TracNav mimics the design of the TracGuideToc
10that was originally supplied with Trac. The drawback of TracGuideToc
11is that is is not customizable without editing its source code and
12that it does not support hierarchical ordering.
13"""
14
15import re
16
17listRule = re.compile(r"""(?P<indent>^\s+)\*\s+\[wiki:(?P<link>(&#34;(.*?)&#34;|'(.*?)')|([^ ]*[^'~_\., \)]))\s+(?P<label>.*)\]""", re.M)
18
19def getToc(db, name):
20    cursor = db.cursor()
21    cursor.execute('SELECT text FROM wiki WHERE name=%s', name)
22    row = cursor.fetchone()
23    if not row:
24        return None
25
26    tocText = row[0]
27
28    stack = [(1, [])]
29    nextPos = 0
30    while 1:
31        match = listRule.search(tocText, nextPos)
32        if not match:
33            break
34
35        indent = len(match.group('indent'))
36        link = match.group('link')
37        label = match.group('label')
38
39        (lastIndent, list) = stack[len(stack) - 1]
40
41        if indent > lastIndent:
42            stack.append((indent, [(link, label, None)]))
43        elif indent == lastIndent:
44            list.append((link, label, None))
45        else:
46            while indent < lastIndent:
47                (_, list) = stack.pop()
48                (lastIndent, topList) = stack[len(stack) - 1]
49                (topLink, topLabel, _) = topList[len(topList) - 1]
50                topList[len(topList) - 1] = (topLink, topLabel, list)
51
52            (lastIndent, list) = stack[len(stack) - 1]
53            list.append((link, label, None))
54               
55        nextPos = match.end()
56
57    while len(stack) > 1:
58        (_, list) = stack.pop()
59        (_, topList) = stack[len(stack) - 1]
60        (topLink, topLabel, _) = topList[len(topList) - 1]
61        topList[len(topList) - 1] = (topLink, topLabel, list)
62
63    (_, list) = stack.pop()
64    return list
65
66
67def execute(hdf, args, env):
68    curpage =  '%s' % hdf.getValue('args.page', '')
69    name = args
70    if not name:
71        name = 'TOC'
72
73    db = env.get_db_cnx()
74    toc = getToc(db, name)
75    if not toc:
76        msg = ''
77        msg += '<div class="system-message"><strong>Error: Table of contents does not exist.'
78        if (hdf.getValue('trac.acl.WIKI_MODIFY', '')):
79            msg += ' Click here to <a href="%s?edit=yes">edit</a>.' % env.href.wiki(name)
80        msg += '</strong></div>'
81        return msg
82
83    html = ''
84    html += '<div class="wiki-toc">'
85    if (hdf.getValue('trac.acl.WIKI_MODIFY', '')):
86        html += '<div class="wiki-tocedit"><a href="%s?edit=yes">edit</a></div>' % env.href.wiki(name)
87    (_, subhtml) = generate(env, curpage, toc, 0)
88    html += subhtml
89    html += '</div>'
90    return html;
91
92
93def generate(env, curpage, toc, level):
94    html = ''
95    found = 0
96    for name, title, sub in toc:
97        if sub == None:
98            if name == curpage:
99                cls = ' class="active"'
100                found = 1
101            else:
102                cls = ''
103            html += '<li%s>' % ( cls )
104            html += '<a href="%s">%s</a>' % (env.href.wiki(name), title)
105            html += '</li>'
106        else:
107            (subfound, subhtml) = generate(env, curpage, sub, level + 1)
108            if subfound:
109                menu = ''
110                menu += '<li>'
111                menu += '<h4>%s</h4>' % title
112                menu += subhtml
113                menu += '</li>'
114                found = 1
115                if level == 0:
116                    html = menu + html
117                else:
118                    html += menu
119            else:
120                html += '<li>'
121                html += '<h4><a href="%s">%s...</a></h4>' % (env.href.wiki(name), title)
122                html += '</li>'
123    html = '<ul>%s</ul>' % html
124    return (found, html);
125
Note: See TracBrowser for help on using the repository browser.