This page describes a possible new class based macro interface.
Interface
1 class MacroInterface(Interface):
2 # The output of a immutable macro only depends on the arguments and the content
3 immutable = False
4
5 def __init__(request, page_name, alt, context, args=None):
6 """"
7 @arg request: The request object
8 @arg page_name: Absolute name of page
9 @arg alt: Alternative text. Can be used if macro is unapplicable in the given context
10 @arg context: Context of macro, only "block" and "inline" are specified.
11 @arg args: The unparsed arguments string.
12 """
13
14 def __call__(content=()):
15 """
16 @arg content: The macro content if applicable.
17 @return Macro body
18 """
Implementation
1 class MacroBase(object):
2 """
3 Macro base class.
4 Supports argument parsing with wikiutil.invoke_extension_function.
5 """
6
7 # The output of a immutable macro only depends on the arguments
8 immutable = False
9
10 def __init__(self, request, alt, context, args=None):
11 self.request, self.alt, self.context, self._args = request, alt, context, args
12
13 def call_macro(self, content):
14 return wikiutil.invoke_extension_function(request, self.macro, self._args)
15
16 def macro(self):
17 raise NotImplementedError
18
19 class MacroBlockBase(MacroBase):
20 """
21 Macro base class for block element macros.
22 The macro gets only expanded in block context. In inline context the
23 alternative text is used instead.
24 """
25 def __call__(self, content=()):
26 if self.context == 'block':
27 return self.call_macro(content)
28 return self.alt
29
30 class MacroInlineBase(MacroBase):
31 """
32 Macro base class for inline element macros.
33 The macro is wrapped into a paragraph in block context.
34 """
35 def __call__(self, content=()):
36 ret = self.call_macro(content)
37 if self.context == 'inline':
38 return ret
39 return ET.Element(ET.QName('p', namespaces.moin_page), children=[ret])
40
41 class MacroInlineOnlyBase(MacroBase):
42 """
43 Macro base class for strict inline element macros.
44 The macro is expanded to nothing in block context.
45 """
46 def __call__(self, content=()):
47 if self.context == 'inline':
48 return self.call_macro(content)