Attachment 'Image.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - Image Macro
   4 
   5     This macro is used to display an image with the ability to resize 
   6     and/or provide an alt text for it.
   7 
   8     Syntax:
   9         [[Image(image_src, [width=width, [height=height], [alt=image_src])]]
  10 
  11     Note: Although providing an alt text is optional please do always provide a sensible,
  12     descriptive alt text to ease image accessing to people using screenreaders.
  13 
  14     Parameters:
  15         image_src: image attachment file name or the URL of an image
  16 
  17      Keyword Parameters:    
  18         width:  rendered image width (optional)
  19         height: rendered image height (optional)
  20         alt:    alt text for the image (optional). By default alt is set to
  21                 to image_src, however this does not make much sense for a real
  22                 AccessibleMoin. Please do provide always a sensible, more descriptive
  23                 alt text so that blind people can get a clue, what is shown on the image.
  24                 Compare: "landscape.jpg" vs. "Photo of a typical Scottish landscape showing the
  25                 wideness of the Scottish Highlands".
  26                 If the image is not important for understanding and just there for layout purposes
  27                 please provide in the case an emtpy alt text, so that it is clear to blind people
  28                 that the image is only there for beautification of the page without deeper meaning
  29                 like illustrating something.
  30 
  31     Examples:
  32         Picture included for beautification only:
  33          [[Image(pic.png, height=100, alt= )]]
  34                   
  35         Pictures conveying some important content:
  36          [[Image(pic.png, alt=Short description of the image)]]
  37          [[Image(OtherWikiSite/pic.png, widht=50, alt=Short description of the image)]]
  38          [[Image(http://webcam.portalmuc.de/images/webcam/webcam_marienplatz.jpg, height=100, alt=Short description of the image)]]
  39 
  40     The Image macro is a simple modification of the ImageLink macro.
  41     ImageLink macro copyright:
  42                 2001 by Jeff Kunce,
  43                 2004 by Marcin Zalewski,
  44                 2004-2006 by Reimar Bauer (R.Bauer@fz-juelich.de),
  45                 2006 by Thomas Waldmann
  46 
  47     Image macro            
  48     @copyright: 2007 by Oliver Siemoneit
  49     @license: GNU GPL, see COPYING for details.
  50 
  51     Changes:
  52     21.12.06    Parameter values now also take "=None" e.g. "width=None"
  53     10.02.07    Alt text is set by default to img_src now. 
  54     25.04.07    Fixed problems that urls with "=" are interpreted as keywords
  55 """
  56 
  57 import os
  58 from MoinMoin import wikiutil, config
  59 from MoinMoin.action import AttachFile
  60 
  61 kwAllowed = ['width', 'height', 'alt']
  62 
  63 def _is_URL(text):
  64     """ Answer true if text is an URL.
  65         The method used here is pretty dumb. Improvements are welcome.
  66     """
  67     return '://' in text
  68 
  69 def execute(macro, args):
  70     request = macro.request
  71     _ = request.getText
  72     formatter = macro.formatter
  73     if args:
  74         args = args.split(',')
  75         args = [arg.strip() for arg in args]
  76     else:
  77         args = []
  78 
  79     argc = len(args)
  80     kw_count = 0
  81     kw = {} # create a dictionary for the formatter.image call
  82     for arg in args :
  83         if '=' in arg:
  84             key, value = arg.split('=', 1)
  85             # avoid that urls with "=" are interpreted as keyword
  86             if key.lower() not in kwAllowed:
  87                 continue
  88             if value != 'None':
  89                 kw_count += 1
  90                 kw[str(key.lower())] = wikiutil.escape(value, quote=1)
  91 
  92     argc -= kw_count
  93     if not argc or argc and not args[0]:
  94         msg = 'Not enough arguments to Image macro! Calling syntax: [[Image(image_src, [width=width], [height=height], [alt=img_src])]]'
  95         return "%s%s%s" % (formatter.sysmsg(1), formatter.text(msg), formatter.sysmsg(0))
  96 
  97     image = args[0]
  98     if _is_URL(image):
  99         kw['src'] = image
 100     else:
 101         pagename, attname = AttachFile.absoluteName(image, formatter.page.page_name)
 102         kw['src'] = AttachFile.getAttachUrl(pagename, attname, request)
 103         attachment_fname = AttachFile.getFilename(request, pagename, attname)
 104         if not os.path.exists(attachment_fname):
 105             linktext = _('Upload new attachment "%(filename)s"')
 106             return wikiutil.link_tag(request,
 107                                      ('%s?action=AttachFile&rename=%s' % (
 108                                          wikiutil.quoteWikinameURL(pagename),
 109                                          wikiutil.url_quote_plus(attname))),
 110                                      linktext % {'filename': attname})
 111     if not 'alt' in kw:
 112         kw['alt'] = image
 113 
 114     return "%s" % formatter.image(**kw)    
 115 
 116     

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] (2007-04-24 22:28:54, 4.6 KB) [[attachment:Image.py]]
 All files | Selected Files: delete move to page copy to page

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