Changeset 2724


Ignore:
Timestamp:
02/21/05 09:32:45 (8 years ago)
Author:
hauma
Message:
  • Refactoring: Break generate() into filter() and display(). This separates filtering the TOC structure according to the currently displayed page and displaying the result.
  • Display the unfiltered version of the TOC structure, if the current page is not found.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trac/trunk/wiki-macros/JPNav.py

    r2723 r2724  
    9898    if (hdf.getValue('trac.acl.WIKI_MODIFY', '')): 
    9999        html += '<div class="wiki-tocedit"><a href="%s?edit=yes">edit</a></div>' % env.href.wiki(name) 
    100     (_, subhtml) = generate(env, curpage, toc, 0) 
    101     html += subhtml 
     100    (found, filtered) = filter(curpage, toc, 0) 
     101    if found: 
     102        html += display(env, curpage, filtered) 
     103    else: 
     104        html += display(env, curpage, toc) 
    102105    html += '</div>' 
    103     return html; 
     106    return html 
    104107 
    105108 
    106 def generate(env, curpage, toc, level): 
    107     html = '' 
     109def filter(curpage, toc, level): 
    108110    found = 0 
     111    result = [] 
     112    for name, title, sub in toc: 
     113        if sub == None: 
     114            if name == curpage: 
     115                found = 1 
     116            result.append((name, title, None)) 
     117        else: 
     118            (subfound, subtoc) = filter(curpage, sub, level + 1) 
     119            if subfound or (name == None): 
     120                found = 1 
     121                if level == 0 and name != None: 
     122                    prepended = [(name, title, subtoc)] 
     123                    prepended.extend(result) 
     124                    result = prepended 
     125                else: 
     126                    result.append((name, title, subtoc)) 
     127            else: 
     128                result.append((name, title, [])) 
     129    return (found, result) 
     130 
     131 
     132def display(env, curpage, toc): 
     133    html = '<ul>' 
    109134    for name, title, sub in toc: 
    110135        if sub == None: 
    111136            if name == curpage: 
    112137                cls = ' class="active"' 
    113                 found = 1 
    114138            else: 
    115139                cls = '' 
     
    121145            html += '</li>' 
    122146        else: 
    123             (subfound, subhtml) = generate(env, curpage, sub, level + 1) 
    124             if subfound or (name == None): 
    125                 menu = '' 
    126                 menu += '<li>' 
    127                 menu += '<h4>%s</h4>' % title 
    128                 menu += subhtml 
    129                 menu += '</li>' 
    130                 found = 1 
    131                 if level == 0 and name != None: 
    132                     html = menu + html 
    133                 else: 
    134                     html += menu 
     147            html += '<li>' 
     148            if name == None: 
     149                html += '<h4>%s</h4>' % title 
    135150            else: 
    136                 html += '<li>' 
    137151                html += '<h4><a href="%s">%s...</a></h4>' % (env.href.wiki(name), title) 
    138                 html += '</li>' 
    139     html = '<ul>%s</ul>' % html 
    140     return (found, html); 
     152            html += display(env, curpage, sub) 
     153            html += '</li>' 
     154    html += '</ul>' 
     155    return html 
    141156 
Note: See TracChangeset for help on using the changeset viewer.