source: tagsplugin/tags/0.10/tractags/macros.py

Last change on this file was 16924, checked in by Ryan J Ollos, 6 years ago

0.10dev: Make compatible with Trac 1.3.2+ (Jinja2)

Refs #12788.

File size: 17.7 KB
Line 
1# -*- coding: utf-8 -*-
2#
3# Copyright (C) 2006 Alec Thomas <alec@swapoff.org>
4# Copyright (C) 2011 Itamar Ostricher <itamarost@gmail.com>
5# Copyright (C) 2011-2015 Steffen Hoffmann <hoff.st@web.de>
6# Copyright (C) 2015 Ryan J Ollos <ryan.j.ollos@gmail.com>
7#
8# This software is licensed as described in the file COPYING, which
9# you should have received as part of this distribution.
10#
11
12import re
13
14from fnmatch import fnmatchcase
15from pkg_resources import resource_filename
16
17from trac.config import BoolOption, ListOption, Option
18from trac.core import Component, TracError, implements
19from trac.resource import Resource, get_resource_url, render_resource_link
20from trac.ticket.api import TicketSystem
21from trac.ticket.model import Ticket
22from trac.util import as_int, embedded_numbers
23from trac.util.html import html as builder
24from trac.util.presentation import Paginator
25from trac.util.text import shorten_line, to_unicode
26from trac.web.chrome import Chrome, ITemplateProvider, add_link, \
27                            add_stylesheet
28from trac.wiki.api import IWikiMacroProvider, parse_args
29from trac.wiki.formatter import format_to_oneliner, system_message
30
31from tractags.api import Counter, InvalidTagRealm, TagSystem, N_, _, gettext
32from tractags.query import InvalidQuery
33from tractags.util import query_realms
34
35# Check for unsupported pre-tags-0.6 macro keyword arguments.
36_OBSOLETE_ARGS_RE = re.compile(r"""
37    (expression|operation|showheadings|tagspace|tagspaces)=
38    """, re.VERBOSE)
39
40
41class TagTemplateProvider(Component):
42    """Provides templates and static resources for the tags plugin."""
43
44    implements(ITemplateProvider)
45
46    abstract = True
47
48    # ITemplateProvider methods
49    def get_templates_dirs(self):
50        """Return the absolute path of the directory containing the provided
51        Genshi templates.
52        """
53        return [resource_filename(__name__, 'templates')]
54
55    def get_htdocs_dirs(self):
56        """Return the absolute path of a directory containing additional
57        static resources (such as images, style sheets, etc).
58        """
59        return [('tags', resource_filename(__name__, 'htdocs'))]
60
61
62class TagWikiMacros(TagTemplateProvider):
63    """[opt] Provides macros, that utilize the tag system in wiki markup."""
64
65    implements(IWikiMacroProvider)
66
67    caseless_sort = BoolOption('tags', 'cloud_caseless_sort', default=False,
68        doc="Whether the tag cloud should be sorted case-sensitive.")
69    default_cols = Option('tags', 'listtagged_default_table_cols',
70        'id|description|tags',
71        doc="Select columns and column order for table format.\n\n"
72            "See `ListTagged` description (WikiMacros) for supported values.")
73    default_format = Option('tags', 'listtagged_default_format', 'oldlist',
74        doc="Set default format for the handler of the `/tags` domain.\n\n"
75            "See `ListTagged` description (WikiMacros) for supported values.")
76    exclude_realms = ListOption('tags', 'listtagged_exclude_realms', [],
77        doc="Comma-separated list of realms to exclude from tags queries "
78            "by default, unless specifically included using 'realm:<realm>' "
79            "in a query.")
80    items_per_page = Option('tags', 'listtagged_items_per_page', 100,
81        doc="Number of tagged resources displayed per page of tag query "
82            "results requested by `ListTagged` macros and from `/tags`.")
83    items_per_page = as_int(items_per_page, 100)
84    supported_cols = frozenset(['realm', 'id', 'description', 'tags'])
85
86    def __init__(self):
87        # TRANSLATOR: Keep macro doc style formatting here, please.
88        self.doc_cloud = N_("""Display a tag cloud.
89
90    Show a tag cloud for all tags on resources matching query.
91
92    Usage:
93
94    {{{
95    [[TagCloud(<query>[,caseless_sort=<bool>][,mincount=<n>])]]
96    }}}
97    caseless_sort::
98      Whether the tag cloud should be sorted case-sensitive.
99    mincount::
100      Optional integer threshold to hide tags with smaller count.
101
102    See tags documentation for the query syntax.
103    """)
104        self.doc_listtagged = N_("""List tagged resources.
105
106    Usage:
107
108    {{{
109    [[ListTagged(<query>[,exclude=<list>],[[format=<format>],cols=<columns>])]]
110    }}}
111    format::
112      result list presentation; supported values:
113
114    || `compact` || comma-separated inline list of "linked-description" ||
115    || `oldlist` (default) || " * linked-id description (tags)" list ||
116    || `table` || table... (see corresponding column option too) ||
117    || `short` or other value || bulleted list of "linked-description" ||
118    cols::
119      columns for 'table' format using a "|"-separated list of column names
120      (order matters); supported columns: realm, id, description, tags
121    exclude::
122      exclude tagged resources that match a name in the colon-separated list
123      of resource ids, accepts shell-style patterns
124
125    See tags documentation for the query syntax.
126    """)
127
128    # IWikiMacroProvider
129
130    def get_macros(self):
131        yield 'ListTagged'
132        yield 'TagCloud'
133
134    def get_macro_description(self, name):
135        if name == 'ListTagged':
136            return gettext(self.doc_listtagged)
137        if name == 'TagCloud':
138            return gettext(self.doc_cloud)
139
140    def expand_macro(self, formatter, name, content, realms=[]):
141        """Evaluate macro call and render results.
142
143        Calls from web-UI come with pre-processed realm selection.
144        """
145        env = self.env
146        req = formatter.req
147        tag_system = TagSystem(env)
148
149        all_realms = tag_system.get_taggable_realms()
150        if not all_realms:
151            # Tag providers are required, no result without at least one.
152            return ''
153        args, kw = parse_args(content)
154
155        query = args and args[0].strip() or None
156        if not realms:
157            # Check macro arguments for realms (typical wiki macro call).
158            realms = 'realm' in kw and kw['realm'].split('|') or []
159        if query:
160            # Add realms from query expression.
161            realms.extend(query_realms(query, all_realms))
162            # Remove redundant realm selection for performance.
163            if set(realms) == all_realms:
164                query = re.sub('(^|\W)realm:\S+(\W|$)', ' ', query).strip()
165        if name == 'TagCloud':
166            # Set implicit 'all tagged realms' as default.
167            if not realms:
168                realms = all_realms
169            if query:
170                all_tags = Counter()
171                # Require per resource query including view permission checks.
172                for resource, tags in tag_system.query(req, query):
173                    all_tags.update(tags)
174            else:
175                # Allow faster per tag query, side steps permission checks.
176                all_tags = tag_system.get_all_tags(req, realms=realms)
177            mincount = 'mincount' in kw and kw['mincount'] or None
178            return self.render_cloud(req, all_tags,
179                                     caseless_sort=self.caseless_sort,
180                                     mincount=mincount, realms=realms)
181        elif name == 'ListTagged':
182            if content and _OBSOLETE_ARGS_RE.search(content):
183                data = {'warning': 'obsolete_args'}
184            else:
185                data = {'warning': None}
186            context = formatter.context
187            # Use TagsQuery arguments (most likely wiki macro calls).
188            cols = 'cols' in kw and kw['cols'] or self.default_cols
189            format = 'format' in kw and kw['format'] or self.default_format
190            if not realms:
191                # Apply ListTagged defaults to macro call w/o realm.
192                realms = list(set(all_realms)-set(self.exclude_realms))
193                if not realms:
194                    return ''
195            query = '(%s) (%s)' % (query or '', ' or '.join(['realm:%s' % (r)
196                                                             for r in realms]))
197            query_result = tag_system.query(req, query)
198            excludes = [exc.strip()
199                        for exc in kw.get('exclude', '' ).split(':')
200                        if exc.strip()]
201            if excludes and query_result:
202                filtered_result = [(resource, tags)
203                                   for resource, tags in query_result
204                                   if not any(fnmatchcase(resource.id, exc)
205                                              for exc in excludes)]
206                query_result = filtered_result
207            if not query_result:
208                return ''
209
210            def _link(resource):
211                if resource.realm == 'tag':
212                    # Keep realm selection in tag links.
213                    return builder.a(resource.id,
214                                     href=self.get_href(req, realms,
215                                                        tag=resource))
216                elif resource.realm == 'ticket':
217                    # Return resource link including ticket status dependend
218                    #   class to allow for common Trac ticket link style.
219                    ticket = Ticket(env, resource.id)
220                    return builder.a('#%s' % ticket.id,
221                                     class_=ticket['status'],
222                                     href=formatter.href.ticket(ticket.id),
223                                     title=shorten_line(ticket['summary']))
224                return render_resource_link(env, context, resource, 'compact')
225
226            if format == 'table':
227                cols = [col for col in cols.split('|')
228                        if col in self.supported_cols]
229                # Use available translations from Trac core.
230                try:
231                    labels = TicketSystem(env).get_ticket_field_labels()
232                    labels['id'] = _('Id')
233                except AttributeError:
234                    # Trac 0.11 neither has the attribute nor uses i18n.
235                    labels = {'id': 'Id', 'description': 'Description'}
236                labels['realm'] = _('Realm')
237                labels['tags'] = _('Tags')
238                headers = [{'label': labels.get(col)}
239                           for col in cols]
240                data.update({'cols': cols,
241                             'headers': headers})
242
243            try:
244                results = sorted(query_result, key=lambda r:
245                                 embedded_numbers(to_unicode(r[0].id)))
246            except (InvalidQuery, InvalidTagRealm), e:
247                return system_message(_("ListTagged macro error"), e)
248            results = self._paginate(req, results, realms)
249            rows = []
250            for resource, tags in results:
251                desc = tag_system.describe_tagged_resource(req, resource)
252                tags = sorted(tags)
253                wiki_desc = format_to_oneliner(env, context, desc)
254                if tags:
255                    rendered_tags = [_link(Resource('tag', tag))
256                                     for tag in tags]
257                    if 'oldlist' == format:
258                        resource_link = _link(resource)
259                    else:
260                        resource_link = builder.a(wiki_desc,
261                                                  href=get_resource_url(
262                                                  env, resource, context.href))
263                        if 'table' == format:
264                            cells = []
265                            for col in cols:
266                                if col == 'id':
267                                    cells.append(_link(resource))
268                                # Don't duplicate links to resource in both.
269                                elif col == 'description' and 'id' in cols:
270                                    cells.append(wiki_desc)
271                                elif col == 'description':
272                                    cells.append(resource_link)
273                                elif col == 'realm':
274                                    cells.append(resource.realm)
275                                elif col == 'tags':
276                                    cells.append(
277                                        builder([(tag, ' ')
278                                                 for tag in rendered_tags]))
279                            rows.append({'cells': cells})
280                            continue
281                rows.append({'desc': wiki_desc,
282                             'rendered_tags': None,
283                             'resource_link': _link(resource)})
284            data.update({'format': format,
285                         'paginator': results,
286                         'results': rows,
287                         'tags_url': req.href('tags')})
288
289            # Work around a bug in trac/templates/layout.html, that causes a
290            # TypeError for the wiki macro call, if we use add_link() alone.
291            add_stylesheet(req, 'common/css/search.css')
292
293            return Chrome(env).render_template(
294                req, 'listtagged_results.html', data, 'text/html', True)
295
296    def get_href(self, req, realms, query=None, per_page=None, page=None,
297                 tag=None, **kwargs):
298        """Prepare href objects for tag links and pager navigation.
299
300        Generate form-related arguments, strip arguments with default values.
301        """
302        # Prepare realm arguments to keep form data consistent.
303        form_realms = dict((realm, 'on') for realm in realms)
304        if not page and not per_page:
305            # We're not serving pager navigation here.
306            return get_resource_url(self.env, tag, req.href,
307                                    form_realms=form_realms, **kwargs)
308        if page == 1:
309            page = None
310        if per_page == self.items_per_page:
311            per_page = None
312        return req.href(req.path_info, form_realms, q=query,
313                        realms=realms, listtagged_per_page=per_page,
314                        listtagged_page=page, **kwargs)
315
316    def render_cloud(self, req, cloud, renderer=None, caseless_sort=False,
317                     mincount=None, realms=()):
318        """Render a tag cloud.
319
320        :cloud: Dictionary of {object: count} representing the cloud.
321        :param renderer: A callable with signature (tag, count, percent)
322                         used to render the cloud objects.
323        :param caseless_sort: Boolean, whether tag cloud should be sorted
324                              case-sensitive.
325        :param mincount: Integer threshold to hide tags with smaller count.
326        """
327        min_px = 10.0
328        max_px = 30.0
329        scale = 1.0
330
331        if renderer is None:
332            def default_renderer(tag, count, percent):
333                href = self.get_href(req, realms, tag=Resource('tag', tag))
334                return builder.a(tag, rel='tag', title='%i' % count,
335                                 href=href, style='font-size: %ipx'
336                                 % int(min_px + percent * (max_px - min_px)))
337            renderer = default_renderer
338
339        # A LUT from count to n/len(cloud)
340        size_lut = dict([(c, float(i)) for i, c in
341                         enumerate(sorted(set([r for r in cloud.values()])))])
342        if size_lut:
343            scale = 1.0 / len(size_lut)
344
345        if caseless_sort:
346            # Preserve upper-case precedence within similar tags.
347            items = reversed(sorted(cloud.iteritems(),
348                                    key=lambda t: t[0].lower(), reverse=True))
349        else:
350            items = sorted(cloud.iteritems())
351        ul = li = None
352        for i, (tag, count) in enumerate(items):
353            percent = size_lut[count] * scale
354            if mincount and count < as_int(mincount, 1):
355                # Tag count is too low.
356                continue
357            if ul:
358                # Found new tag for cloud; now add previously prepared one.
359                ul('\n', li)
360            else:
361                # Found first tag for cloud; now create the list.
362                ul = builder.ul(class_='tagcloud')
363            # Prepare current tag entry.
364            li = builder.li(renderer(tag, count, percent))
365        if li:
366            # All tags checked; mark latest tag as last one (no tailing colon).
367            li(class_='last')
368            ul('\n', li, '\n')
369        return ul and ul or _("No tags found")
370
371    def _paginate(self, req, results, realms):
372        query = req.args.get('q', None)
373        current_page = as_int(req.args.get('listtagged_page'), 1, min=1)
374        items_per_page = as_int(req.args.get('listtagged_per_page'),
375                                self.items_per_page)
376        if items_per_page < 1:
377            items_per_page = self.items_per_page
378        try:
379            result = Paginator(results, current_page - 1, items_per_page)
380        except (AssertionError, TracError), e:
381            # AssertionError raised in Trac < 1.0.10, TracError otherwise
382            self.log.warn("ListTagged macro: %s", e)
383            current_page = 1
384            result = Paginator(results, current_page - 1, items_per_page)
385
386        pagedata = []
387        shown_pages = result.get_shown_pages(21)
388        for page in shown_pages:
389            page_href = self.get_href(req, realms, query, items_per_page, page)
390            pagedata.append([page_href, None, str(page),
391                             _("Page %(num)d", num=page)])
392
393        attributes = ['href', 'class', 'string', 'title']
394        result.shown_pages = [dict(zip(attributes, p)) for p in pagedata]
395
396        result.current_page = {'href': None, 'class': 'current',
397                               'string': str(result.page + 1), 'title': None}
398
399        if result.has_next_page:
400            next_href = self.get_href(req, realms, query, items_per_page,
401                                      current_page + 1)
402            add_link(req, 'next', next_href, _('Next Page'))
403
404        if result.has_previous_page:
405            prev_href = self.get_href(req, realms, query, items_per_page,
406                                      current_page - 1)
407            add_link(req, 'prev', prev_href, _('Previous Page'))
408        return result
Note: See TracBrowser for help on using the repository browser.