Edgewall Software

Ticket #8204: 8204-processor-args-macro-r8371.patch

File 8204-processor-args-macro-r8371.patch, 2.7 KB (added by rblank, 3 years ago)

Pass wiki processor arguments to macros.

  • trac/wiki/api.py

    diff --git a/trac/wiki/api.py b/trac/wiki/api.py
    a b  
    8282    def render_macro(req, name, content): 
    8383        """Return the HTML output of the macro (deprecated)""" 
    8484 
    85     def expand_macro(formatter, name, content): 
     85    def expand_macro(formatter, name, content, args={}): 
    8686        """Called by the formatter when rendering the parsed wiki text. 
    8787 
     88        `content` is the content of the macro call. When called using macro 
     89        syntax (`[[Macro(content)]]`), this is the string contained between 
     90        parentheses, usually containing macro arguments. When called using wiki 
     91        processor syntax (`{{{!#Macro ...}}}`), it is the content of the 
     92        processor block, that is, the text starting on the line following the 
     93        macro name. In this case, `args` contains the named arguments passed on 
     94        the same line as the macro name. 
     95 
    8896        (since 0.11) 
    8997        """ 
    9098 
  • trac/wiki/formatter.py

    diff --git a/trac/wiki/formatter.py b/trac/wiki/formatter.py
    a b  
    3434from trac.resource import get_relative_resource, get_resource_url 
    3535from trac.wiki.api import WikiSystem, parse_args 
    3636from trac.wiki.parser import WikiParser 
     37from trac.util import arity 
    3738from trac.util.text import exception_to_unicode, shorten_line, to_unicode, \ 
    3839                           unicode_quote, unicode_quote_plus 
    3940from trac.util.html import TracHTMLSanitizer 
     
    175176    def _macro_processor(self, text): 
    176177        self.env.log.debug('Executing Wiki macro %s by provider %s' 
    177178                           % (self.name, self.macro_provider)) 
    178         return self.macro_provider.expand_macro(self.formatter, self.name, 
    179                                                 text) 
     179        if arity(self.macro_provider.expand_macro) == 5: 
     180            return self.macro_provider.expand_macro(self.formatter, self.name, 
     181                                                    text, self.args) 
     182        else: 
     183            return self.macro_provider.expand_macro(self.formatter, self.name, 
     184                                                    text) 
    180185 
    181186    def _mimeview_processor(self, text): 
    182187        return Mimeview(self.env).render(self.formatter.context, 
     
    796801            match = WikiParser._processor_re.match(line) 
    797802            if match: 
    798803                name = match.group(1) 
    799                 args = WikiParser._processor_param_re.split(line[len(name):]) 
     804                args = WikiParser._processor_param_re.split(line[2+len(name):]) 
    800805                del args[::3] 
    801806                keys = [str(k) for k in args[::2]] # used as keyword parameters 
    802807                values = [(v and v[0] in '"\'' and [v[1:-1]] or [v])[0]