changeset 2028:8383e7156ef5

ImageLink, text_gedit: refactored
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Thu, 26 Apr 2007 12:56:05 +0200
parents 2203d6f9885e
children 6f9e71e8bf26
files MoinMoin/formatter/text_gedit.py MoinMoin/macro/ImageLink.py
diffstat 2 files changed, 54 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/formatter/text_gedit.py	Thu Apr 26 12:55:30 2007 +0200
+++ b/MoinMoin/formatter/text_gedit.py	Thu Apr 26 12:56:05 2007 +0200
@@ -97,34 +97,28 @@
     def macro(self, macro_obj, name, args):
         #use ImageLink for resized images
         if name == "ImageLink" and args is not None:
+
+            from MoinMoin.macro import ImageLink
             pagename = self.page.page_name
-            if args:
-                args = [arg.strip() for arg in args.split(',')]
-            else:
-                args = []
-            url = None
-            kw = {}
+
+            kwAllowed = ['width', 'height', 'alt']
+            pp, pp_count, kw, kw_count = ImageLink.explore_args(args, kwAllowed)
+
             kw['src'] = None
-            pos = 0
-            for arg in args:
-                if '=' in arg:
-                    key, value = arg.split('=')
-                    if key == 'width' and value:
-                        kw['width'] = value
-                    elif key == 'height' and value:
-                        kw['height'] = value
-                    elif key == 'alt' and value:
-                        kw['alt'] = value
-                else:
-                    if pos == 0 and arg:
-                        url = arg
-                        if url.startswith('http:'):
-                            kw['src'] = url
-                        else:
-                            kw['title'] = "attachment:%s" % wikiutil.quoteWikinameURL(url)
-                    elif pos == 1 and arg:
-                        kw['target'] = arg
-                    pos += 1
+            url = None
+
+            if pp_count >= 1:
+                url = pp[0]
+
+            if pp_count == 2:
+                kw['target'] = pp[1]
+
+            if ImageLink._is_URL(url):
+                kw['src'] = url
+                kw['title'] = url
+            else:
+                kw['title'] = "attachment:%s" % wikiutil.quoteWikinameURL(url)
+
             if kw['src'] is None:
                 if '/' in url:
                     pagename, target = AttachFile.absoluteName(url, pagename)
--- a/MoinMoin/macro/ImageLink.py	Thu Apr 26 12:55:30 2007 +0200
+++ b/MoinMoin/macro/ImageLink.py	Thu Apr 26 12:56:05 2007 +0200
@@ -30,6 +30,7 @@
         [[ImageLink(mnchen.png,http://www.muenchen.de,width=50)]]
         [[ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg)]]
         [[ImageLink(example.png,alt=whateveryouwant())]]
+        [[ImageLink(http://moinmoin.wikiwikiweb.de/OliverSiemoneit?action=AttachFile&do=get&target=screenshot.png,width=647,height=517,alt=OliverSiemoneit?action=AttachFile&do=get&target=screenshot,FrontPage)]] 
 
     History:
         Jeff Kunce: 
@@ -87,52 +88,69 @@
 
     @copyright: 2001 by Jeff Kunce,
                 2004 by Marcin Zalewski,
-                2004-2006 by MoinMoin:ReimarBauer,
-                2006 by Thomas Waldmann
+                2006 by MoinMoin:ThomasWaldmann,
+                2004-2007 by MoinMoin:ReimarBauer
+                
     @license: GNU GPL, see COPYING for details.
 """
 
 from MoinMoin import wikiutil
 from MoinMoin.action import AttachFile
 
-kwAllowed = ['width', 'height', 'alt']
-
 def _is_URL(text):
     """ Answer true if text is an URL.
         The method used here is pretty dumb. Improvements are welcome.
     """
     return '://' in text
 
-def execute(macro, args):
-    request = macro.request
-    _ = request.getText
-    formatter = macro.formatter
+def explore_args(args, kwAllowed):
+    """ 
+    explore args for positional and keyword parameters
+    """
     if args:
         args = args.split(',')
         args = [arg.strip() for arg in args]
     else:
         args = []
 
-    argc = len(args)
     kw_count = 0
     kw = {} # create a dictionary for the formatter.image call
+    pp = [] # positional parameter
+
+    if not kwAllowed:
+        return pp, 0, kw, 0
+
     for arg in args:
         if '=' in arg:
             key, value = arg.split('=', 1)
             # avoid that urls with "=" are interpreted as keyword
             if key.lower() not in kwAllowed:
+                if not kw_count and _is_URL(arg):
+                    # assuming that this is the image
+                    pp.append(wikiutil.escape(arg, quote=1))
                 continue
             kw_count += 1
             kw[str(key.lower())] = wikiutil.escape(value, quote=1)
+        else:
+            pp.append(wikiutil.escape(arg, quote=1))
 
-    argc -= kw_count
-    if not argc or argc and not args[0]:
+    return pp, len(pp), kw, len(kw)
+
+def execute(macro, args):
+    request = macro.request
+    _ = request.getText
+    formatter = macro.formatter
+
+    kwAllowed = ['width', 'height', 'alt']
+    pp, pp_count, kw, kw_count = explore_args(args, kwAllowed)
+
+    if not pp_count or pp_count and not pp[0]:
         msg = 'Not enough arguments to ImageLink macro! e.g. [[ImageLink(example.png, WikiName, width=200)]].'
         return "%s%s%s" % (formatter.sysmsg(1), formatter.text(msg), formatter.sysmsg(0))
 
-    image = args[0]
-    if argc >= 2 and args[1]:
-        target = args[1]
+    image = pp[0]
+    if pp_count >= 2 and pp[1]:
+        target = pp[1]
         if target.startswith('attachment:') or target.startswith('inline:'):
             if target.startswith('attachment:'):
                 target = (target.split('attachment:'))[1]
@@ -153,7 +171,7 @@
 
             kw['src'] = AttachFile.getAttachUrl(pagename, image, request)
 
-    elif argc == 1:
+    elif pp_count == 1:
         pagename, attname = AttachFile.absoluteName(image, formatter.page.page_name)
         target = AttachFile.getAttachUrl(pagename, image, request)
     else:
@@ -185,7 +203,7 @@
     if target is None:
         target = kw['src']
 
-    if argc == 1:
+    if pp_count == 1:
         return "%s%s%s" % (formatter.url(1, kw['src']),
                            formatter.image(**kw),
                            formatter.url(0))