Changeset 735

Show
Ignore:
Timestamp:
05/02/06 01:11:46 (3 years ago)
Author:
athomas
Message:

TocMacro:

Current pages should now be highlighted, closes #350

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • tocmacro/0.9/tractoc/macro.py

    r655 r735  
    1717class MyOutlineFormatter(OutlineFormatter): 
    1818 
    19     def format(self, page, *args, **kwords): 
     19    def format(self, active_page, page, text, out, min_depth, max_depth): 
    2020        self.__page = page 
    21         super(MyOutlineFormatter,self).format(*args, **kwords) 
     21        # XXX Code copied straight out of OutlineFormatter 
     22        self.outline = [] 
     23        class NullOut(object): 
     24            def write(self, data): pass 
     25        Formatter.format(self, text, NullOut()) 
     26 
     27        if min_depth > max_depth: 
     28            min_depth, max_depth = max_depth, min_depth 
     29        max_depth = min(6, max_depth) 
     30        min_depth = max(1, min_depth) 
     31 
     32        curr_depth = min_depth - 1 
     33        for depth, link in self.outline: 
     34            active = '' 
     35            if '/%s' % active_page in link: 
     36                active = ' class="active"' 
     37            if depth < min_depth or depth > max_depth: 
     38                continue 
     39            if depth < curr_depth: 
     40                out.write('</li></ol><li%s>' % active * (curr_depth - depth)) 
     41            elif depth > curr_depth: 
     42                out.write('<ol><li%s>' % active * (depth - curr_depth)) 
     43            else: 
     44                out.write("</li><li%s>\n" % active) 
     45            curr_depth = depth 
     46            out.write(link) 
     47        out.write('</li></ol>' * curr_depth) 
    2248 
    2349    def _heading_formatter(self, match, fullmatch): 
     
    136162        for pagename in pagenames: 
    137163            if params['title_index']: 
     164                li_class = pagename.startswith(current_page) and ' class="active"' or '' 
    138165                prefix = (pagename.split('/'))[0] 
    139166                prefix = prefix.replace('\'', '\'\'') 
     
    145172                        page_text, _ = get_page_text(page) 
    146173     
    147                         formatter.format(page, page_text, NullOut(), 1, 1) 
     174                        formatter.format(current_page, page, page_text, NullOut(), 1, 1) 
    148175                        header = '' 
    149176                        if formatter.outline: 
     
    151178                            title = re.sub('<[^>]*>','', title) # Strip all tags 
    152179                            header = ': ' + wiki_to_oneliner(title, self.env) 
    153                         out.write('<li> <a href="%s">%s</a> %s</li>\n' % (self.env.href.wiki(page), page, header)) 
     180                        out.write('<li%s> <a href="%s">%s</a> %s</li>\n' % (li_class, self.env.href.wiki(page), page, header)) 
    154181                    out.write('</ol>')         
    155182                else : 
     
    159186                page_text, page_exists = get_page_text(page) 
    160187                if page_exists: 
    161                     formatter.format(page, page_text, out, params['min_depth'], params['max_depth']) 
     188                    formatter.format(current_page, page, page_text, out, params['min_depth'], params['max_depth']) 
    162189                else: 
    163190                    out.write('<div class="system-message"><strong>Error: Page %s does not exist</strong></div>' % pagename)