source: foldedrepositoryindexplugin/folded_repository_index/web_ui.py @ 1:34f4cf9dcb95

tip
Last change on this file since 1:34f4cf9dcb95 was 1:34f4cf9dcb95, checked in by Tobias Föhst <foehst@…>, 6 years ago

Makes XPath in genshi stream filter aware of possible prefix

File size: 1.9 KB
Line 
1from trac.core import *
2from trac.web import IRequestFilter
3from trac.web.api import ITemplateStreamFilter
4from trac.web.chrome import ITemplateProvider, \
5                            add_script, add_script_data
6
7from genshi.filters import Transformer
8
9import os
10import re
11
12class FoldedRepositoryIndex(Component):
13    """Adds folding by prefix to repository index.
14
15    Repositories are collected into folded groups by mapping their names via
16    regular expressions to custom labels. This mapping is defined in the
17    `[folded-repository-index]` section of the [wiki:TracIni trac.ini] configuration
18    file.
19
20    Example:
21    {{{
22    [folded-repository-index]
23    ^finroc_ = Finroc
24    ^paper_(\d+)_ = Papers $1
25    }}}
26    """
27
28    implements(IRequestFilter, ITemplateProvider, ITemplateStreamFilter)
29
30    ### IRequestFilter methods
31    def pre_process_request(self, req, handler):
32        return handler
33
34    def post_process_request(self, req, template, data, content_type):
35        match = re.match(r'^/browser/?$', req.path_info)
36        if match:
37            config = self.config.options('folded-repository-index')
38            add_script_data(req, { 'fri_config' : dict(config) })
39            add_script(req, 'fri/folded_repository_index.js')
40        return template, data, content_type
41
42    ### ITemplateProvider methods
43    def get_templates_dirs(self):
44        return []
45
46    def get_htdocs_dirs(self):
47        from pkg_resources import resource_filename
48        return [('fri', resource_filename(__name__, 'htdocs'))]
49
50    ### ITemplateStreamFilter methods
51    def filter_stream(self, req, method, filename, stream, data):
52        match = re.match(r'^/browser/?$', req.path_info)
53        if not match:
54            return stream
55
56        script_url = req.href('/chrome/common/js/expand_dir.js')
57        filter = Transformer('//script[@src="' + script_url + '"]')
58        return stream | filter.remove()
Note: See TracBrowser for help on using the repository browser.