Attachment 'svg.py'

Download

   1 #format python
   2 # -*- coding: iso-8859-1 -*-
   3 """
   4     MoinMoin - svg Macro
   5 
   6     PURPOSE:
   7         This macro is used to enable an attached SVG image to be displayed
   8         in a browser. Optionally, the size of the image can be adjusted.
   9 
  10     CALLING SEQUENCE:
  11         [[svg(attachment,width=width,[height=height]])]]
  12 
  13     INPUTS:
  14         attachment:image name of attachment
  15 
  16     KEYWORD PARAMETERS:
  17         width: width of the embedded image (default is 100%)
  18 	      height: height of the embedded image (default is 100%)
  19 
  20     EXAMPLE:
  21       [[svg(plot.svg]]
  22       [[svg(plot.svg,width=800,height=600)]]
  23       [[svg(plot.svg,width=50%)]]
  24 
  25 
  26     PROCEDURE:
  27       This routine requires attachments enabled. If the attachment is not already
  28       present on the wiki then the attachment upload page is shown allowing you to
  29       upload the missing attachment.
  30 
  31       You must map the mimetype "image/svg+xml" to the .svg and .svgz extensions
  32       - either in your web server configuration...Python will automatically
  33         add the mimetypes found in the following files:
  34             "/etc/mime.types",
  35             "/usr/local/etc/httpd/conf/mime.types",
  36             "/usr/local/lib/netscape/mime.types",
  37             "/usr/local/etc/httpd/conf/mime.types",     # Apache 1.2
  38             "/usr/local/etc/mime.types",                # Apache 1.3
  39         ...but they most likely are not already defined in any of them.
  40       - or by adding the following lines to wikiconfig.py:
  41         import mimetypes
  42         mimetypes.add_type("image/svg_xml", ".svg", True)
  43         mimetypes.add_type("image/svg_xml", ".svgz", True)
  44       - or by some other method that I am not aware of.
  45 
  46       You will need Internet Explorer and the Adobe SvgViewer plugin before
  47       SVG content can be seen. Mozilla and Firefox, as at 2005, are not
  48       up to it. Who knows about the others.
  49 
  50       This macro must be put in "[yourwiki]/data/plugin/macro/"
  51 
  52     CREDITS:
  53       This macro was built using the ImageLink macro as a template.
  54       Please feel free to post improved versions of it.
  55 
  56     MODIFICATION HISTORY:
  57         @license: GNU GPL, see COPYING for details.
  58 
  59 
  60 """
  61 
  62 from MoinMoin.action  import AttachFile
  63 from MoinMoin         import wikiutil, config
  64 import os,string
  65 
  66 
  67 def execute(macro, text):
  68 
  69   kw = {'width': '100%', 'height': '100%'}
  70 
  71   if text:
  72     args = text.split(',')
  73   else:
  74     args = []
  75 
  76   argCount = len(args)
  77   kwCount = 0
  78   for a in args:
  79     if (a.find('=') > -1):
  80       kwCount = kwCount + 1
  81       key = a.split('=')
  82       kw[str(key[0])] = wikiutil.escape(string.join(key[1],''), quote=1)
  83 
  84   argCount = argCount - kwCount
  85 
  86   if (argCount < 1):
  87     msg = 'Not enough arguments to svg macro! Try [[svg(attachment[,width=n][,height=n])]]'
  88     return macro.formatter.sysmsg(1) + macro.formatter.text(msg) + macro.formatter.sysmsg(0)
  89 
  90   attachmentName = wikiutil.taintfilename(macro.formatter.text(args[0]))
  91   currentPageName = macro.formatter.page.page_name
  92   kw['src'] = AttachFile.getAttachUrl(currentPageName,attachmentName,macro.request)
  93   attachmentPath = os.path.join(AttachFile.getAttachDir(macro.request,currentPageName), attachmentName)
  94 
  95   if not os.path.exists(attachmentPath):
  96     import urllib
  97     linktext = macro.request.getText('Upload new attachment "%(filename)s"')
  98     return wikiutil.link_tag(macro.request,
  99                              '%s?action=AttachFile&amp;rename=%s' %
 100                              (wikiutil.quoteWikinameFS(currentPageName),
 101                               urllib.quote_plus(attachmentName)),
 102                              linktext % {'filename': attachmentName})
 103 
 104   if (argCount == 1):
 105     objectTag = '<object type="image/svg+xml" data="%s" width="%s" height="%s">Your browser cannot display SVG</object>' \
 106                 % (wikiutil.escape(kw['src']), kw['width'], kw['height'])
 107     return macro.formatter.rawHTML(objectTag)

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] (2010-12-18 06:54:53, 0.4 KB) [[attachment:example.svg]]
  • [get | view] (2010-12-18 06:58:15, 0.3 KB) [[attachment:example.svgz]]
  • [get | view] (2005-03-04 12:52:27, 36.1 KB) [[attachment:plot.svg]]
  • [get | view] (2005-03-04 12:54:18, 9.0 KB) [[attachment:plot.svgz]]
  • [get | view] (2005-03-04 12:19:19, 3.9 KB) [[attachment:svg.py]]
 All files | Selected Files: delete move to page copy to page

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