changeset 2579:9f933413518b

rewrite action macro. this breaks some pages that'll need to be fixed
author Johannes Berg <johannes AT sipsolutions DOT net>
date Fri, 27 Jul 2007 11:43:18 +0200
parents 1e5ac92670e6
children c3741ed41050
files MoinMoin/macro/Action.py MoinMoin/macro/_tests/test_Action.py
diffstat 2 files changed, 33 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/macro/Action.py	Fri Jul 27 11:42:09 2007 +0200
+++ b/MoinMoin/macro/Action.py	Fri Jul 27 11:43:18 2007 +0200
@@ -2,17 +2,7 @@
 """
     MoinMoin - Create an action link
 
-    Usage:
-
-        [[Action(action)]]
-
-        Create a link to page with ?action=action and the text action
-
-        [[Action(action, text)]]
-
-        Same with custom text.
-
-    @copyright: 2004 Johannes Berg <johannes@sipsolutions.de>
+    @copyright: 2004, 2007 Johannes Berg <johannes@sipsolutions.net>
                 2007 by MoinMoin:ReimarBauer
     @license: GNU GPL, see COPYING for details.
 """
@@ -22,69 +12,36 @@
 Dependencies = ["language"]
 
 
-class ActionLink:
-    """ ActionLink - link to page with valid action """
-
-    arguments = ['action', 'text']
-
-    def __init__(self, macro, args):
-        self.macro = macro
-        self.request = macro.request
-        self.args = self.getArgs(args)
-
-    def getValidActions(self):
-        """ lists all valid actions """
-        from MoinMoin import action
-        # builtin
-        actions_builtin = action.names
-        # global
-        actions_global = ([x for x in action.modules
-                           if not x in self.macro.request.cfg.actions_excluded])
-        # local
-        actions_local = ([x for x in wikiutil.wikiPlugins('action', self.macro.cfg)
-                          if not x in self.macro.request.cfg.actions_excluded])
-
-        return actions_builtin + actions_global + actions_local
-
-    def getArgs(self, argstr):
-        """ Temporary function until Oliver Graf args parser is finished
+def _get_valid_actions(macro):
+    """ lists all valid actions """
+    from MoinMoin import action
+    # builtin
+    actions_builtin = action.names
+    # global
+    actions_global = ([x for x in action.modules
+                       if not x in macro.request.cfg.actions_excluded])
+    # local
+    actions_local = ([x for x in wikiutil.wikiPlugins('action', macro.cfg)
+                      if not x in macro.request.cfg.actions_excluded])
 
-        @param string: string from the wiki markup [[NewPage(string)]]
-        @rtype: dict
-        @return: dictionary with macro options
-        """
-        if not argstr:
-            return {}
-        args = [s.strip() for s in argstr.split(',')]
-        args = dict(zip(self.arguments, args))
-        return args
-
-    def renderInText(self):
-        """ Render macro in text context
-
-        The parser should decide what to do if this macro is placed in a
-        paragraph context.
-        """
-        _ = self.request.getText
+    return actions_builtin + actions_global + actions_local
 
-        # Default to show page instead of an error message (too lazy to
-        # do an error message now).
-        action = self.args.get('action', 'show')
+def macro_Action(macro, action=u'show', text=None, _kwargs=None):
+    _ = macro.request.getText
+    if text is None:
+        text = action
+    if not _kwargs:
+        _kwargs = {}
 
-        # Use translated text or action name
-        text = self.args.get('text', action)
-        text = _(text, formatted=False)
-        text = wikiutil.escape(text, 1)
-        action, args = (action.split('&') + [None] * 2)[:2]
-        if action in self.getValidActions():
-            # Create link
-            page = self.macro.formatter.page
-            link = page.link_to(self.request, text, querystr='action=%s&%s' % (action, args))
-            return link
-        else:
-            return text
-
-def execute(macro, args):
-    """ Temporary glue code to use with moin current macro system """
-    return ActionLink(macro, args).renderInText()
-
+    text = _(text, formatted=False)
+    if action in _get_valid_actions(macro):
+        page = macro.formatter.page
+        _kwargs['action'] = action
+        url = page.url(macro.request, querystr=_kwargs)
+        return ''.join([
+            macro.formatter.url(1, url),
+            macro.formatter.text(text),
+            macro.formatter.url(0),
+        ])
+    else:
+        return macro.formatter.text(text)
--- a/MoinMoin/macro/_tests/test_Action.py	Fri Jul 27 11:42:09 2007 +0200
+++ b/MoinMoin/macro/_tests/test_Action.py	Fri Jul 27 11:43:18 2007 +0200
@@ -53,11 +53,11 @@
     def testActionCallingRaw(self):
         """ module_tested: executes raw by macro Action on existing page"""
 
-        expected = '<a href="./AutoCreatedMoinMoinTemporaryTestPageForAction?action=raw">raw</a>'
+        expected = '<a href="AutoCreatedMoinMoinTemporaryTestPageForAction?action=raw">raw</a>'
         text = '= title1 =\n||A||B||\n'
         self._createTestPage(text)
         m = self._make_macro()
-        result = Action.execute(m, 'raw')
+        result = Action.macro_Action(m, 'raw')
 
         assert result == expected