Edgewall Software

Ticket #8204: wikiprocessors-or-macros-r9193.patch

File wikiprocessors-or-macros-r9193.patch, 4.0 KB (added by cboos, 2 years ago)

WikiProcessors: fix a potential problem with processor arguments.

  • trac/wiki/formatter.py

    WikiProcessors: fix a potential problem with processor arguments.
    
    At the same time, this makes it now possible to differentiate between being callled as a macro (`self.args is None`) and via a code block.
    
    Related to #8204.
    
    diff --git a/trac/wiki/formatter.py b/trac/wiki/formatter.py
    a b class WikiProcessor(object): 
    6767    _block_elem_re = re.compile(r'^\s*<(?:div|table)(?:\s+[^>]+)?>', 
    6868                                re.I | re.M) 
    6969 
    70     def __init__(self, formatter, name, args={}): 
     70    def __init__(self, formatter, name, args=None): 
    7171        """Find the processor by name 
    7272         
    7373        :param formatter: the formatter embedding a call for this processor  
    class WikiProcessor(object): 
    155155         
    156156    def _elt_processor(self, eltname, format_to, text, args): 
    157157        # Note: as long as _processor_param_re is not re.UNICODE, **args is OK 
    158         elt = getattr(tag, eltname)(**args) 
     158        elt = getattr(tag, eltname)(**(args or {})) 
    159159        if not WikiSystem(self.env).render_unsafe_content: 
    160160            sanitized_elt = getattr(tag, eltname) 
    161161            for (k, data, pos) in (Stream(elt) | self._sanitizer): 
    class WikiProcessor(object): 
    166166        return elt 
    167167 
    168168    def _div_processor(self, text): 
     169        if not self.args: 
     170            self.args = {} 
    169171        if 'class' not in self.args: 
    170172            self.args['class'] = 'wikipage' 
    171173        return self._elt_processor('div', format_to_html, text, self.args) 
    class WikiProcessor(object): 
    190192            return system_message(e) 
    191193     
    192194    def _table_processor(self, text): 
     195        if not self.args: 
     196            self.args = {} 
    193197        if 'class' not in self.args: 
    194198            self.args['class'] = 'wiki' 
    195199        try: 
  • trac/wiki/tests/formatter.py

    diff --git a/trac/wiki/tests/formatter.py b/trac/wiki/tests/formatter.py
    a b class NoneMacro(WikiMacroBase): 
    6666    def expand_macro(self, formatter, name, content): 
    6767        return None 
    6868 
     69class WikiProcessorSampleMacro(WikiMacroBase): 
     70    def expand_macro(self, formatter, name, content, args): 
     71        if args is None: 
     72            return 'Called as a macro: ' + content 
     73        else: 
     74            return 'Called as a processor with params: <dl>%s</dl>' % \ 
     75                ''.join('<dt>%s</dt><dd>%s</dd>' % kv for kv in args.items()) \ 
     76                + content 
     77 
    6978class SampleResolver(Component): 
    7079    """A dummy macro returning a div block, used by the unit test.""" 
    7180 
  • trac/wiki/tests/wiki-tests.txt

    diff --git a/trac/wiki/tests/wiki-tests.txt b/trac/wiki/tests/wiki-tests.txt
    a b After 
    687687Before 
    688688 […] 
    689689After 
     690============================== WikiProcessor default arguments 
     691[[div(Div has 'wikipage' class.)]] 
     692[[table()]] 
     693Table must have 'wiki' class. 
     694------------------------------ 
     695<p> 
     696</p><div class="wikipage"><p> 
     697Div has 'wikipage' class. 
     698</p> 
     699</div><p> 
     700</p><table class="wiki"></table><p> 
     701Table must have 'wiki' class. 
     702</p> 
     703------------------------------ 
    690704============================== div and span wiki processors 
    691705And now it's [[span('''TIME FOR BED!,class=important)]]. Really. 
    692706{{{ 
    Hello World, args = hej hopp Hello World 
    870884</p> 
    871885------------------------------ 
    872886[[None(...)]] nada 
     887============================== WikiProcessor defined as a macro 
     888[[WikiProcessorSample(inlined content)]] 
     889{{{#!WikiProcessorSample arg1=this arg2="the second argument" 
     890multiline 
     891content 
     892}}} 
     893------------------------------ 
     894<p> 
     895Called as a macro: inlined content 
     896</p> 
     897Called as a processor with params: <dl><dt>arg1</dt><dd>this</dd><dt>arg2</dt><dd>the second argument</dd></dl>multiline 
     898content 
     899------------------------------ 
     900[[WikiProcessorSample(...)]] 
     901 […] 
    873902============================== Inlined HTML wiki processor 
    874903Inline [[html(<B> Test </B>)]] text 
    875904------------------------------