Attachment 'ImageLink-1.6.0.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - ImageLink Macro
   4 
   5     This macro is used to make a link that displays an image (can be given
   6     as either attachment or URL) and links to either an URL or a wiki page.
   7     Optionally the size of the image can be adjusted.
   8     If no target is given the link will point to the image itself.
   9    
  10     Since MoinMoin 1.6.0 this macro is replaced by builin markup, see HelpOnLinking 
  11  
  12     Syntax:
  13         <<ImageLink(image, [target,] [width=width, [height=height>>)>>
  14 
  15     Parameters:
  16         image: image attachment file name or the URL of an image
  17         target: link target wiki page or URL (optional)
  18 
  19     Keyword Parameters:
  20         width: rendered image width (optional)
  21         height: rendered image heigth (optional)
  22         alt: text for img tag "alt" attribute
  23 
  24     Examples:
  25         <<ImageLink(München.png,München,height=100)>>
  26         <<ImageLink(Images/München.png,München,height=100)>>
  27         <<ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg,München Marienplatz)>>
  28         <<ImageLink(plot.png,width=200)>>
  29         <<ImageLink(plot.png,height=200)>>
  30         <<ImageLink(plot.png)>>
  31         <<ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg,http://www.muenchen.de,width=150)>>
  32         <<ImageLink(münchen.png,http://www.muenchen.de,width=50)>>
  33         <<ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg)>>
  34         <<ImageLink(example.png,alt=whateveryouwant(üöä))>>
  35         <<ImageLink(http://moinmo.in/OliverSiemoneit?action=AttachFile&do=get&target=screenshot.png,width=647,height=517,alt=OliverSiemoneit?action=AttachFile&do=get&target=screenshot,FrontPage)>>
  36 
  37     History:
  38         Jeff Kunce: 
  39             wrote the first published version of this macro in 2001.
  40         
  41         Reimar Bauer:
  42             2004 intitial new version for MoinMoin 1.2
  43         
  44         Marcin Zalewski:   
  45             Implemented wikiutil.link_tag and macro.formatter.pagelink.
  46             Added title attribute to the created link. One could generalize that to
  47             add arbitrary attributes.
  48 
  49             One could also add class attributes to <a> and/or <img> elements.
  50             I do not see the need for such modifications. If however this is
  51             to be done in the future one would need to add 'html_class' key to the kw dictionary
  52             with a corresponding value to add class to <img> element. To add class to <a> element
  53             one needs to add 'css_class' key with a corresponding value to the dictionary passed to
  54             pagelink call.
  55         
  56        Reimar Bauer:    
  57             2004-12-23 Adapted to MoinMoin Version 1.3.1-1
  58             2004-12-23 Syntax change Version 1.3.1-2
  59                    width and height and probably other keywords must be given as keywords (e.g. height=20)
  60             2004-12-31 Version 1.3.1-3 code clean up
  61             2005-01-16 Bug fixed in the errorhandler - found and patched by Malte Helmert
  62             2005-03-05 Version 1.3.3-5 Bug fixed found by cypress ("If I put <<ImageLink(moinmoin.png)>> it bombs")
  63             2005-03-28 Version 1.3.3-6 feature request added by CDPark:
  64                        "Can we use an external image? And an external target?"
  65             2005-04-16 Version 1.3.3-7 no default alt tag definition as requested by CDPark and AlexanderSchremmer
  66        
  67        Chong-Dae Park:
  68             2005-04-17 Version 1.3.3-8 code refactored
  69                        IMG with no alt tag is not recommended in the HTML standard.
  70                        It keeps a user specified alt tag. If there is none, it tries to make on using the WikiName
  71                        or image name instead.
  72        
  73        Reimar Bauer:
  74             2005-04-21 Version 1.3.3-9 bug fixed
  75                        When the image does not exist yet, it gives you a "Upload Image" link, this link does not
  76                        work. I suspect that this only is a problem on sub-pages, caused by incorrect escaping of
  77                         "/". -- CraigJohnson
  78  
  79             2005-12-19 Versiom 1.5.0-10 feature added to link to images on different wiki pages
  80             2006-02-14 Version 1.5.2-11 bug fixed for filename of attached image is Chinese (encode added)
  81             2006-02-22 Version 1.5.2-12 code refactored
  82 
  83       Thomas Waldmann
  84             2006-03-10 code refactored
  85             
  86       Reimar Bauer
  87              2006-09-22 bug fix of image linked to attachment and inline
  88              2006-10-08 patch of DavidLinke added and keys now only lowercase used
  89              2006-12-16 interwikilink for pages added
  90 
  91     @copyright: 2001 by Jeff Kunce,
  92                 2004 by Marcin Zalewski,
  93                 2006 by MoinMoin:ThomasWaldmann,
  94                 2004-2007 by MoinMoin:ReimarBauer
  95                 
  96     @license: GNU GPL, see COPYING for details.
  97 """
  98 
  99 from MoinMoin import wikiutil
 100 from MoinMoin.action import AttachFile
 101 
 102 def _is_URL(text):
 103     """ Answer true if text is an URL.
 104         The method used here is pretty dumb. Improvements are welcome.
 105     """
 106     return '://' in text
 107 
 108 def explore_args(args, kwAllowed):
 109     """ 
 110     explore args for positional and keyword parameters
 111     """
 112     if args:
 113         args = args.split(',')
 114         args = [arg.strip() for arg in args]
 115     else:
 116         args = []
 117 
 118     kw_count = 0
 119     kw = {} # create a dictionary for the formatter.image call
 120     pp = [] # positional parameter
 121 
 122     if not kwAllowed:
 123         return pp, 0, kw, 0
 124 
 125     for arg in args:
 126         if '=' in arg:
 127             key, value = arg.split('=', 1)
 128             # avoid that urls with "=" are interpreted as keyword
 129             if key.lower() not in kwAllowed:
 130                 if not kw_count and _is_URL(arg):
 131                     # assuming that this is the image
 132                     pp.append(wikiutil.escape(arg, quote=1))
 133                 continue
 134             kw_count += 1
 135             kw[str(key.lower())] = wikiutil.escape(value, quote=1)
 136         else:
 137             pp.append(wikiutil.escape(arg, quote=1))
 138 
 139     return pp, len(pp), kw, len(kw)
 140 
 141 def execute(macro, args):
 142     request = macro.request
 143     _ = request.getText
 144     formatter = macro.formatter
 145 
 146     kwAllowed = ['width', 'height', 'alt']
 147     pp, pp_count, kw, kw_count = explore_args(args, kwAllowed)
 148 
 149     if not pp_count or pp_count and not pp[0]:
 150         msg = 'Not enough arguments given to ImageLink macro! Try <<ImageLink(example.png, WikiName, width=200)>>.'
 151         return "%s%s%s" % (formatter.sysmsg(1), formatter.text(msg), formatter.sysmsg(0))
 152 
 153     image = pp[0]
 154     if pp_count >= 2 and pp[1]:
 155         target = pp[1]
 156         if target.startswith('attachment:') or target.startswith('inline:'):
 157             if target.startswith('attachment:'):
 158                 target = (target.split('attachment:'))[1]
 159                 pagename, attname = AttachFile.absoluteName(target, formatter.page.page_name)
 160                 target = AttachFile.getAttachUrl(pagename, target, request)
 161             elif target.startswith('inline:'):
 162                 target = (target.split('inline:'))[1]
 163                 pagename, attname = AttachFile.absoluteName(target, formatter.page.page_name)
 164                 target = AttachFile.getAttachUrl(pagename, target, request, do='view')
 165 
 166             if not AttachFile.exists(request, pagename, attname):
 167                 linktext = _('Upload new attachment "%(filename)s"', formatted=False)
 168                 return wikiutil.link_tag(request,
 169                                          ('%s?action=AttachFile&rename=%s' % (
 170                                          wikiutil.quoteWikinameURL(pagename),
 171                                          wikiutil.url_quote_plus(attname))),
 172                                          linktext % {'filename': attname})
 173 
 174             kw['src'] = AttachFile.getAttachUrl(pagename, image, request)
 175 
 176     elif pp_count == 1:
 177         pagename, attname = AttachFile.absoluteName(image, formatter.page.page_name)
 178         target = AttachFile.getAttachUrl(pagename, image, request)
 179     else:
 180         target = None
 181 
 182     if _is_URL(image):
 183         kw['src'] = image
 184     else:
 185         pagename, attname = AttachFile.absoluteName(image, formatter.page.page_name)
 186         kw['src'] = AttachFile.getAttachUrl(pagename, attname, request)
 187         if not AttachFile.exists(request, pagename, attname):
 188             linktext = _('Upload new attachment "%(filename)s"', formatted=False)
 189             return wikiutil.link_tag(request,
 190                                      ('%s?action=AttachFile&rename=%s' % (
 191                                          wikiutil.quoteWikinameURL(pagename),
 192                                          wikiutil.url_quote_plus(attname))),
 193                                          linktext % {'filename': attname})
 194 
 195     if 'alt' not in kw:
 196         if target is None or _is_URL(target):
 197             if _is_URL(image):
 198                 # Get image name http://here.com/dir/image.png -> image.png
 199                 kw['alt'] = wikiutil.taintfilename(formatter.text(image.split('/')[-1]))
 200             else:
 201                 kw['alt'] = attname
 202         else:
 203             kw['alt'] = target
 204 
 205     if target is None:
 206         target = kw['src']
 207 
 208     if pp_count == 1:
 209         return "%s%s%s" % (formatter.url(1, kw['src']),
 210                            formatter.image(**kw),
 211                            formatter.url(0))
 212 
 213     if _is_URL(target) or 'action=AttachFile&do=get&target=' in target or 'action=AttachFile&do=view&target=' in target:
 214         return "%s%s%s" % (formatter.url(1, target),
 215                            formatter.image(**kw),
 216                            formatter.url(0))
 217     else:
 218         if ":" in target:
 219             if target.startswith('wiki:'):
 220                 target = target[5:]
 221             wikitag, wikiurl, wikitail, error = wikiutil.resolve_wiki(request, target)
 222             url = wikiurl + wikiutil.quoteWikinameURL(wikitail)
 223             return "%s%s%s" % (formatter.url(1, url),
 224                                formatter.image(**kw),
 225                                formatter.url(0))
 226         else:
 227             return "%s%s%s" % (formatter.pagelink(1, target),
 228                                formatter.image(**kw),
 229                                formatter.pagelink(0))

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2006-06-27 13:22:40, 447.6 KB) [[attachment:Ein Prosit]]
  • [get | view] (2005-04-16 21:38:31, 6.2 KB) [[attachment:ImageLink-1.3.3-7.py]]
  • [get | view] (2005-04-17 15:33:24, 6.6 KB) [[attachment:ImageLink-1.3.3-8.py]]
  • [get | view] (2005-04-21 16:09:30, 6.9 KB) [[attachment:ImageLink-1.3.3-9.py]]
  • [get | view] (2005-12-19 19:47:34, 7.1 KB) [[attachment:ImageLink-1.5.0-10.py]]
  • [get | view] (2006-02-15 17:42:37, 7.2 KB) [[attachment:ImageLink-1.5.2-11.py]]
  • [get | view] (2006-02-22 22:26:50, 7.3 KB) [[attachment:ImageLink-1.5.2-12.py]]
  • [get | view] (2006-03-11 22:20:23, 6.6 KB) [[attachment:ImageLink-1.5.3.py]]
  • [get | view] (2008-01-25 19:57:41, 10.0 KB) [[attachment:ImageLink-1.6.0.py]]
  • [get | view] (2006-03-11 22:46:46, 61.8 KB) [[attachment:München.png]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.