Attachment 'ImageLink-1.2.3-2.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - ImageLink Macro
   4 
   5     PURPOSE:
   6         This macro is used to set a link as WikiName for an attached image. Optional the size of the
   7 	image could be adjusted.
   8 
   9     CALLING SEQUENCE:
  10         [[ImageLink(attachment,WikiName,[width,[height]])]]
  11 
  12     INPUTS:
  13         attachment:image name of attachment
  14 	WikiName: the page to set the link to
  15 
  16     OPTIONAL INPUTS:
  17         width: width of the embedded image
  18 	height: height of the embedded image
  19 
  20     EXAMPLE:
  21       [[ImageLink(plot.gif,FrontPage,20,20)]]
  22       [[ImageLink(plot.gif,FrontPage)]]
  23 
  24     PROCEDURE:
  25       This routine requires attachment enabled. If the attachment isn't downloaded at all
  26       the attachment line is shown.
  27 
  28       It must be in "MoinMoin/macro"
  29 
  30     MODIFICATION HISTORY:
  31         @copyright: 2004 by Reimar Bauer (R.Bauer@fz-juelich.de)
  32         @license: GNU GPL, see COPYING for details.
  33 
  34 	Marcin Zalewski:
  35 	    Some things that were causing problems on my wiki are changed
  36             (wikiutil.link_tag and macro.formatter.pagelink implemented)
  37 
  38 """
  39 
  40 #Dependencies = []
  41 
  42 from MoinMoin.action import AttachFile
  43 from MoinMoin import wikiutil
  44 import os,string
  45 
  46 
  47 def execute(macro, text):
  48 
  49    kw ={} # create a dictionary for the formatter.image call
  50 
  51    if text:
  52       args=text.split(',')
  53    else:
  54       args=[]
  55    number_args=len(args)
  56    if number_args < 2:
  57       return macro.formatter.sysmsg('Not enough arguments to ImageLink macro')
  58    attname=wikiutil.taintfilename(macro.formatter.text(args[0]))
  59    linked_page=macro.formatter.text(args[1])
  60    current_pagename=macro.formatter.page.page_name
  61    kw['src']=AttachFile.getAttachUrl(current_pagename,attname,macro.request)
  62 
  63    # Check if attachment used for image link exists
  64    attachment_path = os.path.join(AttachFile.getAttachDir(current_pagename), attname)
  65    if not os.path.exists(attachment_path):
  66       import urllib
  67       linktext = macro.request.getText('Upload new attachment "%(filename)s"')
  68       return wikiutil.link_tag(macro.request,
  69                                '%s?action=AttachFile&amp;rename=%s' %
  70                                (wikiutil.quoteWikiname(current_pagename),
  71                                 urllib.quote_plus(attname)),
  72                                linktext % {'filename': attname})
  73 
  74 
  75 
  76    if (number_args >= 3 and args[2] != ''):
  77       kw['width']=wikiutil.escape(string.join(args[2],''), quote=1)
  78    if (number_args >= 4 and args[3] != ''):
  79       kw['height']=wikiutil.escape(string.join(args[3],''), quote=1)
  80    image_link=macro.formatter.image(**kw)
  81    return macro.formatter.pagelink(linked_page, image_link)

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] (2004-09-18 14:03:39, 2.6 KB) [[attachment:ImageLink-1.2.3-2.py]]
  • [get | view] (2004-09-20 07:19:06, 3.3 KB) [[attachment:ImageLink-1.2.3-3.py]]
  • [get | view] (2004-09-11 07:34:46, 1.7 KB) [[attachment:ImageLink-1.2.3.py]]
 All files | Selected Files: delete move to page copy to page

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