Edgewall Software

Ticket #5227: resource_macro_multiple_formats.patch

File resource_macro_multiple_formats.patch, 2.1 KB (added by Mark Mc Mahon <mark.mcmahon@…>, 21 months ago)

Allow multiple formats separated with | character

  • macros.py

     
    2525 
    2626from trac.core import * 
    2727from trac.resource import Resource, ResourceNotFound, get_resource_name, \ 
    28                           get_resource_summary, get_resource_url 
     28                          get_resource_summary, get_resource_url, \ 
     29                          render_resource_link 
    2930from trac.util.compat import any, rpartition 
    3031from trac.util.datefmt import format_date, from_utimestamp 
    3132from trac.util.html import escape 
     
    710711                           class_=(prefix+ref == curpage and 'active')) 
    711712                    for ref, title in self.TOC]), 
    712713            class_='wiki-toc') 
     714 
     715 
     716class ResourceMacro(WikiMacroBase): 
     717    """Display a link to the specified resource 
     718 
     719    A resource is specified using `realm`, `id` and optionally `version`. 
     720    The `format` of the resulting link can also be specified: default, 
     721    compact or summary (or a combination of formats separated by the |  
     722    character. 
     723    """ 
     724 
     725    def expand_macro(self, formatter, name, args): 
     726        args, kw = parse_args(args) 
     727         
     728        args = dict( 
     729            zip(('realm', 'id', 'version', 'format'), args)) 
     730        args.update(kw) 
     731         
     732        realm = args.get('realm', '').strip() or formatter.resource.realm 
     733        res_id = args.get('id', '').strip() 
     734         
     735        if not res_id: 
     736            raise TracError('Resource Macro requires a resource ID') 
     737         
     738        formats = [f.strip() for f in args.get('format', 'default').split('|')] 
     739 
     740        version = args.get('version', False) 
     741        if version: 
     742            version.strip() 
     743         
     744        res = Resource(realm, res_id, version) 
     745         
     746        links = [] 
     747        for format in formats: 
     748            # insert a space before every part except the first 
     749            if links: 
     750                links.append(" ") 
     751            links.append( 
     752                render_resource_link(self.env, formatter, res, format)) 
     753 
     754        return  tag.span(links)