Changeset 3056


Ignore:
Timestamp:
11/16/05 16:37:58 (8 years ago)
Author:
moschny
Message:
  • Bugfix: Uncaught StopIteration, in case the toc is empty.
  • Fix many pylint warnings (long lines, conventions for variable and method names, unused variables and imports, docstrings).
File:
1 edited

Legend:

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

    • Property svn:keywords set to Id
    r3054 r3056  
    6868""" 
    6969 
     70__revision__ = "$Id$" 
     71 
    7072import re 
    71 import sys 
    7273from trac.wiki.api import WikiSystem 
    7374from trac.wiki.model import WikiPage 
    7475 
    75 listRule = re.compile(r"""^(?P<indent> *)\* +(?:(?P<wikilink>\[wiki:(?P<link>(&#34;([^&#34;]*)&#34;|'([^']*)')|([^ \]]+)) +(?P<label>[^\]]*)\])|(?P<text>.*))""", re.M) 
    76  
    77 def getToc(hdf, env, curpage, name): 
    78     tocText = "* Table of contents" 
     76LISTRULE = re.compile(r"""^(?P<indent> *)\* +""" 
     77                      r"""(?:(?P<wikilink>\[wiki:""" 
     78                      r"""(?P<link>(&#34;([^&#34;]*)&#34;|""" 
     79                      r"""'([^']*)')|([^ \]]+))""" 
     80                      r"""+(?P<label>[^\]]*)\])|(?P<text>.*))""", 
     81                      re.M) 
     82 
     83TRACNAVHOME = "http://svn.ipd.uka.de/trac/javaparty/wiki/TracNav" 
     84 
     85def get_toc(hdf, env, curpage, name): 
     86    """Fetch the wiki page containing the toc, if available.""" 
     87     
     88    toc_text = "* Table of contents" 
    7989 
    8090    preview = hdf.getValue('args.preview', "") 
    8191    if preview and (name == curpage): 
    82         tocText = hdf.getValue('wiki.page_source', tocText); 
     92        toc_text = hdf.getValue('wiki.page_source', toc_text); 
    8393    else: 
    8494        if WikiSystem(env).has_page(name): 
    85             tocText = WikiPage(env, name).text 
     95            toc_text = WikiPage(env, name).text 
    8696 
    8797    # env.log.debug(tocText) 
    88     return tocText 
    89  
    90  
    91 def getTocEntry(tocText): 
    92     nextPos = 0 
     98    return toc_text 
     99 
     100 
     101def get_toc_entry(toc_text): 
     102    """Generator: returns the next toc entry. Each toc entry consists 
     103    of it's indentation level, label and link.""" 
     104    next_pos = 0 
    93105    while 1: 
    94         match = listRule.search(tocText, nextPos) 
     106        match = LISTRULE.search(toc_text, next_pos) 
    95107        if not match: 
    96108            # env.log.debug("No more matches") 
     
    111123     
    112124        yield indent, link, label 
    113         nextPos = match.end() 
    114  
    115  
    116 def getTocEntryAndNextIndent(g): 
    117     indent, link, label = g.next() 
     125        next_pos = match.end() 
     126 
     127 
     128def get_toc_entry_and_indent(gen): 
     129    """Generator, to be used as a filter for get_toc_entry().  Returns 
     130    link and label of the current toc entry and the indentation level 
     131    of the next entry or -1 if there are no more entries. The first 
     132    call to next() returns the indentation of the first entry.""" 
     133     
     134    try: 
     135        indent, link, label = gen.next() 
     136    except StopIteration: 
     137        indent = -1 
    118138    yield indent 
    119139 
     
    122142        yield link, label 
    123143        try: 
    124             indent, link, label = g.next() 
     144            indent, link, label = gen.next() 
    125145        except StopIteration: 
    126146            indent, ready = -1, True 
     
    128148 
    129149 
    130 def _parseToc(g, nextIndent, level = 0): 
    131     list = [] 
    132     if nextIndent > level: 
    133         subList, nextIndent = _parseToc(g, nextIndent, level + 1) 
    134         if nextIndent < level: 
     150def _parse_toc(gen, next_indent, level = 0): 
     151    toclist = [] 
     152    if next_indent > level: 
     153        sublist, next_indent = _parse_toc(gen, next_indent, level + 1) 
     154        if next_indent < level: 
    135155            # level is empty 
    136             return subList, nextIndent 
     156            return sublist, next_indent 
    137157        else: 
    138158            # broken indentation structure 
    139             list.append((None, None, subList)) 
     159            toclist.append((None, None, sublist)) 
    140160    while 1: 
    141         if nextIndent == level: 
    142             (link, label), nextIndent = g.next(), g.next() 
    143             if nextIndent > level: 
    144                 subList, nextIndent = _parseToc(g, nextIndent, level + 1) 
    145                 list.append((link, label, subList)) 
    146             else: 
    147                 list.append((link, label, None)) 
    148         else: 
    149             assert nextIndent < level 
    150             return list, nextIndent 
    151  
    152  
    153 def parseToc(tocText): 
    154     g = getTocEntryAndNextIndent(getTocEntry(tocText)) 
    155     list, _ = _parseToc(g, g.next()) 
    156     return list 
     161        if next_indent == level: 
     162            (link, label), next_indent = gen.next(), gen.next() 
     163            if next_indent > level: 
     164                sublist, next_indent = _parse_toc(gen, next_indent, level + 1) 
     165                toclist.append((link, label, sublist)) 
     166            else: 
     167                toclist.append((link, label, None)) 
     168        else: 
     169            assert next_indent < level 
     170            return toclist, next_indent 
     171 
     172 
     173def parse_toc(toc_text): 
     174    gen = get_toc_entry_and_indent(get_toc_entry(toc_text)) 
     175    toclist, _ = _parse_toc(gen, gen.next()) 
     176    return toclist 
    157177    
    158178 
     
    164184        name = 'TOC' 
    165185 
    166     db = env.get_db_cnx() 
    167     toc = parseToc(getToc(hdf, env, curpage, name)) 
     186    toc = parse_toc(get_toc(hdf, env, curpage, name)) 
    168187    if not toc: 
    169         msg = '' 
    170         msg += '<div class="system-message"><strong>Error: Table of contents does not exist.' 
     188        msg = '<div class="system-message">' \ 
     189              "<strong>Error: Table of contents does not exist or is empty." 
    171190        if (not preview) and (hdf.getValue('trac.acl.WIKI_MODIFY', '')): 
    172             msg += ' Click here to <a href="%s?edit=yes">edit</a>.' % env.href.wiki(name) 
     191            msg += ' Click here to <a href="%s?edit=yes">edit</a>.' % \ 
     192                   env.href.wiki(name) 
    173193        msg += '</strong></div>\n' 
    174194        return msg 
    175195 
    176     (found, filtered) = filter(curpage, toc, 0) 
     196    (found, filtered) = filter_toc(curpage, toc, 0) 
    177197    if found: 
    178         return displayAll(hdf, env, name, curpage, filtered, 0) 
     198        return display_all(hdf, env, name, curpage, filtered, 0) 
    179199    else: 
    180         return displayAll(hdf, env, name, curpage, toc, 0) 
    181  
    182  
    183 def filter(curpage, toc, level): 
     200        return display_all(hdf, env, name, curpage, toc, 0) 
     201 
     202 
     203def filter_toc(curpage, toc, level): 
    184204    found = 0 
    185205    result = [] 
     
    190210            result.append((name, title, None)) 
    191211        else: 
    192             (subfound, subtoc) = filter(curpage, sub, level + 1) 
     212            (subfound, subtoc) = filter_toc(curpage, sub, level + 1) 
    193213            if subfound: 
    194214                found = 1 
     
    207227    return ' ' * col 
    208228 
    209 def displayAll(hdf, env, name, curpage, toc, col): 
     229def display_all(hdf, env, name, curpage, toc, col): 
    210230    preview = hdf.getValue('args.preview', "") 
    211     html = '' 
    212     html += '%s<div class="wiki-toc trac-nav">\n' % indentation(col) 
     231    html = '%s<div class="wiki-toc trac-nav">\n' % indentation(col) 
    213232    col += 1 
    214  
    215     html += '%s<h2><a href="http://svn.ipd.uka.de/trac/javaparty/wiki/TracNav">TracNav</a> menu</h2>' % indentation(col) 
     233    html += '%s<h2><a href="%s">TracNav</a> menu</h2>' % \ 
     234            (indentation(col), TRACNAVHOME) 
    216235 
    217236    if (not preview) and hdf.getValue('trac.acl.WIKI_MODIFY', ''): 
    218         html += '%s<div class="edit"><a href="%s?edit=yes">edit</a></div>\n' % (indentation(col), env.href.wiki(name)) 
     237        html += '%s<div class="edit"><a href="%s?edit=yes">edit</a></div>\n' % \ 
     238                (indentation(col), env.href.wiki(name)) 
    219239    html += '%s<ul>\n' % indentation(col) 
    220240    col += 1 
     
    229249    html = '' 
    230250    for name, title, sub in toc: 
    231         liStyle = ' style="padding-left: %dem;"' % (depth + 1) 
     251        li_style = ' style="padding-left: %dem;"' % (depth + 1) 
    232252        if sub == None: 
    233253            if name == curpage: 
     
    235255            else: 
    236256                cls = '' 
    237             html += '%s<li%s%s>' % (indentation(col), liStyle, cls) 
     257            html += '%s<li%s%s>' % (indentation(col), li_style, cls) 
    238258            if name == None: 
    239259                html += title 
     
    242262            html += '</li>\n' 
    243263        else: 
    244             html += '%s<li%s>\n' % (indentation(col), liStyle) 
     264            html += '%s<li%s>\n' % (indentation(col), li_style) 
    245265            col += 1 
    246266            if name == None or len(sub) > 0: 
    247267                html += '%s<h4>%s</h4>\n' % (indentation(col), title) 
    248268            else: 
    249                 html += '%s<h4><a href="%s">%s</a>...</h4>\n' % (indentation(col), env.href.wiki(name), title) 
     269                html += '%s<h4><a href="%s">%s</a>...</h4>\n' % \ 
     270                        (indentation(col), env.href.wiki(name), title) 
    250271            col -= 1 
    251272            html += '%s</li>\n' % indentation(col) 
Note: See TracChangeset for help on using the changeset viewer.