| 1 | # -*- coding: iso8859-1 -*- |
|---|
| 2 | """ |
|---|
| 3 | = TracNav: The navigation bar for Trac = |
|---|
| 4 | |
|---|
| 5 | This macro implements a fully customizable navigation bar for the Trac |
|---|
| 6 | wiki engine. The contents of the navigation bar is a wiki page itself |
|---|
| 7 | and can be edited like any other wiki page through the web |
|---|
| 8 | interface. The navigation bar supports hierarchical ordering of |
|---|
| 9 | topics. The design of TracNav mimics the design of the TracGuideToc |
|---|
| 10 | that was originally supplied with Trac. The drawback of TracGuideToc |
|---|
| 11 | is that is is not customizable without editing its source code and |
|---|
| 12 | that it does not support hierarchical ordering. |
|---|
| 13 | """ |
|---|
| 14 | |
|---|
| 15 | import re |
|---|
| 16 | |
|---|
| 17 | listRule = re.compile(r"""(?P<indent>^\s+)\*\s+\[wiki:(?P<link>("(.*?)"|'(.*?)')|([^ ]*[^'~_\., \)]))\s+(?P<label>.*)\]""", re.M) |
|---|
| 18 | |
|---|
| 19 | def 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 | |
|---|
| 67 | def 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 | |
|---|
| 93 | def 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 | |
|---|