Edgewall Software

Ticket #5227: resource_macro.patch

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

Simple resource macro

  • 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 hte resulting link can also be specified: default, 
     721    compact or complete. 
     722    """ 
     723 
     724    def expand_macro(self, formatter, name, args): 
     725        args, kw = parse_args(args) 
     726 
     727        args = dict( 
     728            zip(('realm', 'id', 'version', 'format'), args)) 
     729        args.update(kw) 
     730 
     731        realm = args.get('realm', '').strip() or formatter.resource.realm 
     732        res_id = args.get('id', '').strip() 
     733 
     734        if not res_id: 
     735            raise TracError('Resource Macro requires a resource ID') 
     736 
     737        format = args.get('format', 'default').strip() 
     738        version = args.get('version', False) 
     739        if version: 
     740            version.strip() 
     741 
     742        res = Resource(realm, res_id, version) 
     743 
     744        return  render_resource_link(self.env, formatter, res, format)