Edgewall Software

Ticket #7124: trac-0.13.dev-wiki-macros.patch

File trac-0.13.dev-wiki-macros.patch, 4.9 KB (added by mark.m.mcmahon@…, 21 months ago)

Patch to allow extra filtering in the TitleIndex macro

  • macros.py

     
    1919import os 
    2020import re 
    2121from StringIO import StringIO 
     22import fnmatch 
    2223 
    2324from genshi.builder import Element, tag 
    2425from genshi.core import Markup 
     
    7677    parameter is omitted, all pages are listed. 
    7778    If the prefix is specified, a second argument of value 'hideprefix' 
    7879    can be given as well, in order to remove that prefix from the output. 
     80    Any includes provided are processed before excludes. 
    7981 
    8082    Alternate `format` and `depth` named parameters can be specified: 
    8183     - `format=compact`: The pages are displayed as comma-separated links. 
     
    8991       only toplevel pages will be shown, if set to 1, only immediate 
    9092       children pages will be shown, etc. If not set, or set to -1, 
    9193       all pages in the hierarchy will be shown. 
     94     - `exclude=page1:page*2`: exclude pages which match any item in the colon 
     95        separated list of pages. 
     96     - `include=page1:page*2`: include pages wihch match any item in the colon 
     97        separated list of pages 
    9298    """ 
    9399 
    94100    SPLIT_RE = re.compile(r"([/ 0-9.]+)") 
     
    102108        start = prefix and prefix.count('/') or 0 
    103109        format = kw.get('format', '') 
    104110 
     111        def parse_include_exclude(in_or_exclude): 
     112            return [inc.strip() 
     113                    for inc in in_or_exclude.split(':') 
     114                    if inc.strip()] 
     115        includes = parse_include_exclude(kw.get('include', '')) 
     116        excludes = parse_include_exclude(kw.get('exclude', '')) 
     117 
    105118        if hideprefix: 
    106119            omitprefix = lambda page: page[len(prefix):] 
    107120        else: 
     
    109122 
    110123        wiki = formatter.wiki 
    111124 
    112         pages = sorted(page for page in wiki.get_pages(prefix) \ 
    113                        if (depth < 0 or depth >= page.count('/') - start) 
    114                        and 'WIKI_VIEW' in formatter.perm('wiki', page)) 
     125        pages = [page for page in wiki.get_pages(prefix) 
     126                 if (depth < 0 or depth >= page.count('/') - start) 
     127                 and 'WIKI_VIEW' in formatter.perm('wiki', page)] 
    115128 
     129        filtered_pages = set() 
     130 
     131        if includes: 
     132            for include in includes: 
     133                filtered_pages.update(fnmatch.filter(pages, include)) 
     134        else: 
     135            filtered_pages = set(pages) 
     136 
     137        for exclude in excludes: 
     138            filtered_pages.difference_update( 
     139                fnmatch.filter(filtered_pages, exclude)) 
     140 
     141        pages = sorted(filtered_pages) 
     142 
    116143        if format == 'compact': 
    117144            return tag( 
    118145                separated((tag.a(wiki.format_page_name(omitprefix(p)), 
  • tests/macros.py

     
    213213</p><div class="titleindex"><ul><li><strong>0.11</strong><ul><li><strong>Group</strong><ul><li><a href="/wiki/0.11/GroupOne">0.11/GroupOne</a></li><li><a href="/wiki/0.11/GroupTwo">0.11/GroupTwo</a></li></ul></li><li><a href="/wiki/0.11/Test">0.11/Test</a></li></ul></li><li><strong>Test</strong><ul><li><strong>0.11Abc</strong><ul><li><a href="/wiki/Test0.11/Abc">Test0.11/Abc</a></li><li><a href="/wiki/Test0.11Abc">Test0.11Abc</a></li></ul></li><li><strong>0.12</strong><ul><li><a href="/wiki/Test0.12Def">Test0.12Def</a></li><li><a href="/wiki/Test0.12Ijk">Test0.12Ijk</a></li></ul></li><li><strong>0.13</strong><ul><li><a href="/wiki/Test0.13alpha">Test0.13alpha</a></li><li><a href="/wiki/Test0.13beta">Test0.13beta</a></li></ul></li><li><a href="/wiki/Test0.131">Test0.131</a></li><li><a href="/wiki/Test2">Test2</a></li><li><a href="/wiki/TestTest">TestTest</a></li><li><a href="/wiki/TestThing">TestThing</a></li></ul></li><li><a href="/wiki/WikiStart">WikiStart</a></li></ul></div><p> 
    214214</p> 
    215215------------------------------ 
     216============================== TitleIndex, compact format with prefix hidden, including Test0.13* 
     217[[TitleIndex(Test,format=compact,include=*0.13*)]] 
     218------------------------------ 
     219<p> 
     220<a href="/wiki/Test0.131">Test0.131</a>, <a href="/wiki/Test0.13alpha">Test0.13alpha</a>, <a href="/wiki/Test0.13beta">Test0.13beta</a> 
     221</p> 
     222------------------------------ 
     223============================== TitleIndex, compact format, excluding various topics 
     224[[TitleIndex(Test,format=compact,exclude=Test0.13*:*0.11*:Test2:Test*i*)]] 
     225------------------------------ 
     226<p> 
     227<a href="/wiki/Test0.12Def">Test0.12Def</a>, <a href="/wiki/TestTest">TestTest</a> 
     228</p> 
     229------------------------------ 
     230============================== TitleIndex, compact format, including and excluding various topics 
     231[[TitleIndex(format=compact,include=*Group*:test2,exclude=*one)]] 
     232------------------------------ 
     233<p> 
     234<a href="/wiki/0.11/GroupTwo">0.11/GroupTwo</a>, <a href="/wiki/Test2">Test2</a> 
     235</p> 
     236------------------------------ 
    216237""" 
    217238 
    218239def titleindex4_setup(tc):