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): |
| 67 | 67 | _block_elem_re = re.compile(r'^\s*<(?:div|table)(?:\s+[^>]+)?>', |
| 68 | 68 | re.I | re.M) |
| 69 | 69 | |
| 70 | | def __init__(self, formatter, name, args={}): |
| | 70 | def __init__(self, formatter, name, args=None): |
| 71 | 71 | """Find the processor by name |
| 72 | 72 | |
| 73 | 73 | :param formatter: the formatter embedding a call for this processor |
| … |
… |
class WikiProcessor(object): |
| 155 | 155 | |
| 156 | 156 | def _elt_processor(self, eltname, format_to, text, args): |
| 157 | 157 | # 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 {})) |
| 159 | 159 | if not WikiSystem(self.env).render_unsafe_content: |
| 160 | 160 | sanitized_elt = getattr(tag, eltname) |
| 161 | 161 | for (k, data, pos) in (Stream(elt) | self._sanitizer): |
| … |
… |
class WikiProcessor(object): |
| 166 | 166 | return elt |
| 167 | 167 | |
| 168 | 168 | def _div_processor(self, text): |
| | 169 | if not self.args: |
| | 170 | self.args = {} |
| 169 | 171 | if 'class' not in self.args: |
| 170 | 172 | self.args['class'] = 'wikipage' |
| 171 | 173 | return self._elt_processor('div', format_to_html, text, self.args) |
| … |
… |
class WikiProcessor(object): |
| 190 | 192 | return system_message(e) |
| 191 | 193 | |
| 192 | 194 | def _table_processor(self, text): |
| | 195 | if not self.args: |
| | 196 | self.args = {} |
| 193 | 197 | if 'class' not in self.args: |
| 194 | 198 | self.args['class'] = 'wiki' |
| 195 | 199 | try: |
diff --git a/trac/wiki/tests/formatter.py b/trac/wiki/tests/formatter.py
|
a
|
b
|
class NoneMacro(WikiMacroBase): |
| 66 | 66 | def expand_macro(self, formatter, name, content): |
| 67 | 67 | return None |
| 68 | 68 | |
| | 69 | class 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 | |
| 69 | 78 | class SampleResolver(Component): |
| 70 | 79 | """A dummy macro returning a div block, used by the unit test.""" |
| 71 | 80 | |
diff --git a/trac/wiki/tests/wiki-tests.txt b/trac/wiki/tests/wiki-tests.txt
|
a
|
b
|
After |
| 687 | 687 | Before |
| 688 | 688 | […] |
| 689 | 689 | After |
| | 690 | ============================== WikiProcessor default arguments |
| | 691 | [[div(Div has 'wikipage' class.)]] |
| | 692 | [[table()]] |
| | 693 | Table must have 'wiki' class. |
| | 694 | ------------------------------ |
| | 695 | <p> |
| | 696 | </p><div class="wikipage"><p> |
| | 697 | Div has 'wikipage' class. |
| | 698 | </p> |
| | 699 | </div><p> |
| | 700 | </p><table class="wiki"></table><p> |
| | 701 | Table must have 'wiki' class. |
| | 702 | </p> |
| | 703 | ------------------------------ |
| 690 | 704 | ============================== div and span wiki processors |
| 691 | 705 | And now it's [[span('''TIME FOR BED!,class=important)]]. Really. |
| 692 | 706 | {{{ |
| … |
… |
Hello World, args = hej hopp Hello World |
| 870 | 884 | </p> |
| 871 | 885 | ------------------------------ |
| 872 | 886 | [[None(...)]] nada |
| | 887 | ============================== WikiProcessor defined as a macro |
| | 888 | [[WikiProcessorSample(inlined content)]] |
| | 889 | {{{#!WikiProcessorSample arg1=this arg2="the second argument" |
| | 890 | multiline |
| | 891 | content |
| | 892 | }}} |
| | 893 | ------------------------------ |
| | 894 | <p> |
| | 895 | Called as a macro: inlined content |
| | 896 | </p> |
| | 897 | Called as a processor with params: <dl><dt>arg1</dt><dd>this</dd><dt>arg2</dt><dd>the second argument</dd></dl>multiline |
| | 898 | content |
| | 899 | ------------------------------ |
| | 900 | [[WikiProcessorSample(...)]] |
| | 901 | […] |
| 873 | 902 | ============================== Inlined HTML wiki processor |
| 874 | 903 | Inline [[html(<B> Test </B>)]] text |
| 875 | 904 | ------------------------------ |