Attachment 'ImageLink-1.5.3.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     Syntax:
  11         [[ImageLink(image, [target,] [width=width, [height=height]])]]
  12 
  13     Parameters:
  14         image: image attachment file name or the URL of an image
  15         target: link target wiki page or URL (optional)
  16 
  17     Keyword Parameters:
  18         width: rendered image width (optional)
  19         height: rendered image heigth (optional)
  20         alt: text for img tag "alt" attribute
  21 
  22     Examples:
  23         [[ImageLink(München.png,München,height=100)]]
  24         [[ImageLink(Images/München.png,München,height=100)]]
  25         [[ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg,München Marienplatz)]]
  26         [[ImageLink(plot.png,width=200)]]
  27         [[ImageLink(plot.png,height=200)]]
  28         [[ImageLink(plot.png)]]
  29         [[ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg,http://www.muenchen.de,width=150)]]
  30         [[ImageLink(münchen.png,http://www.muenchen.de,width=50)]]
  31         [[ImageLink(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg)]]
  32         [[ImageLink(example.png,alt=whateveryouwant(üöä))]]
  33 
  34     History:
  35         Jeff Kunce: wrote the first published version of this macro in 2001.
  36 
  37         Marcin Zalewski:
  38         Implemented wikiutil.link_tag and macro.formatter.pagelink.
  39         Added title attribute to the created link. One could generalize that to
  40         add arbitrary attributes.
  41 
  42         One could also add class attributes to <a> and/or <img> elements.
  43         I do not see the need for such modifications. If however this is
  44         to be done in the future one would need to add 'html_class' key to the kw dictionary
  45         with a corresponding value to add class to <img> element. To add class to <a> element
  46         one needs to add 'css_class' key with a corresponding value to the dictionary passed to
  47         pagelink call.
  48         
  49         Reimar Bauer:
  50             2004-12-23 Adapted to MoinMoin Version 1.3.1-1
  51             2004-12-23 Syntax change Version 1.3.1-2
  52                    width and height and probably other keywords must be given as keywords (e.g. height=20)
  53             2004-12-31 Version 1.3.1-3 code clean up
  54             2005-01-16 Bug fixed in the errorhandler - found and patched by Malte Helmert
  55             2005-03-05 Version 1.3.3-5 Bug fixed found by cypress ("If I put [[ImageLink(moinmoin.png)]] it bombs")
  56             2005-03-28 Version 1.3.3-6 feature request added by CDPark:
  57                        "Can we use an external image? And an external target?"
  58             2005-04-16 Version 1.3.3-7 no default alt tag definition as requested by CDPark and AlexanderSchremmer
  59        
  60        Chong-Dae Park:
  61             2005-04-17 Version 1.3.3-8 code refactored
  62                        IMG with no alt tag is not recommended in the HTML standard.
  63                        It keeps a user specified alt tag. If there is none, it tries to make on using the WikiName
  64                        or image name instead.
  65        
  66        Reimar Bauer:
  67             2005-04-21 Version 1.3.3-9 bug fixed
  68                        When the image does not exist yet, it gives you a "Upload Image" link, this link does not
  69                        work. I suspect that this only is a problem on sub-pages, caused by incorrect escaping of
  70                         "/". -- CraigJohnson
  71  
  72             2005-12-19 Versiom 1.5.0-10 feature added to link to images on different wiki pages
  73             2006-02-14 Version 1.5.2-11 bug fixed for filename of attached image is Chinese (encode added)
  74             2006-02-22 Version 1.5.2-12 code refactored
  75 
  76       Thomas Waldmann
  77             2006-03-10 code refactored
  78 
  79     @copyright: 2001 Jeff Kunce
  80     @copyright: 2004-2006 by Reimar Bauer (R.Bauer@fz-juelich.de)
  81     @copyright: 2006 refactoring by Thomas Waldmann
  82     @license: GNU GPL, see COPYING for details.
  83 """
  84 
  85 import os
  86 from MoinMoin import wikiutil, config
  87 from MoinMoin.action import AttachFile
  88 
  89 def _is_URL(text):
  90     """ Answer true if text is an URL.
  91         The method used here is pretty dumb. Improvements are welcome.
  92     """
  93     return '://' in text
  94 
  95 def execute(macro, args):
  96     request = macro.request
  97     _ = request.getText
  98     formatter = macro.formatter
  99     if args:
 100         args = args.split(',')
 101         args = [arg.strip() for arg in args]
 102     else:
 103         args = []
 104 
 105     argc = len(args)
 106     kw_count = 0
 107     kw = {} # create a dictionary for the formatter.image call
 108     for arg in args :
 109         if '=' in arg:
 110             kw_count += 1
 111             key, value = arg.split('=', 1)
 112             kw[str(key)] = wikiutil.escape(value, quote=1)
 113 
 114     argc -= kw_count
 115     if not argc:
 116         msg = 'Not enough arguments to ImageLink macro! e.g. [[ImageLink(example.png, WikiName, width=200)]].'
 117         return "%s%s%s" % (formatter.sysmsg(1), formatter.text(msg), formatter.sysmsg(0))
 118 
 119     image = args[0]
 120     if argc >= 2:
 121         target = args[1]
 122     else:
 123         target = None
 124         
 125     if _is_URL(image):
 126         kw['src'] = image
 127     else:
 128         pagename, attname = AttachFile.absoluteName(image, formatter.page.page_name)
 129         kw['src'] = AttachFile.getAttachUrl(pagename, attname, request)
 130         attachment_fname = AttachFile.getFilename(request, pagename, attname)
 131         if not os.path.exists(attachment_fname):
 132             linktext = _('Upload new attachment "%(filename)s"')
 133             return wikiutil.link_tag(request,
 134                                      ('%s?action=AttachFile&rename=%s' % (
 135                                          wikiutil.quoteWikinameURL(pagename),
 136                                          wikiutil.url_quote_plus(attname))),
 137                                      linktext % {'filename': attname})
 138 
 139     if not kw.has_key('alt'):
 140         if target is None or _is_URL(target):
 141             if _is_URL(image):
 142                 # Get image name http://here.com/dir/image.png -> image.png
 143                 kw['alt'] = wikiutil.taintfilename(formatter.text(image.split('/')[-1])) # XXX
 144             else:
 145                 kw['alt'] = attname
 146         else:
 147             kw['alt'] = target
 148 
 149     if target is None:
 150         target = kw['src']
 151 
 152     if _is_URL(target):
 153         return "%s%s%s" % (formatter.url(1, target),
 154                            formatter.image(**kw),
 155                            formatter.url(0))
 156     else:
 157         return "%s%s%s" % (formatter.pagelink(1, target),
 158                            formatter.image(**kw),
 159                            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.