#format python
# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - svg Macro

    PURPOSE:
        This macro is used to enable an attached SVG image to be displayed
        in a browser. Optionally, the size of the image can be adjusted.

    CALLING SEQUENCE:
        [[svg(attachment,width=width,[height=height]])]]

    INPUTS:
        attachment:image name of attachment

    KEYWORD PARAMETERS:
        width: width of the embedded image (default is 100%)
	      height: height of the embedded image (default is 100%)

    EXAMPLE:
      [[svg(plot.svg]]
      [[svg(plot.svg,width=800,height=600)]]
      [[svg(plot.svg,width=50%)]]


    PROCEDURE:
      This routine requires attachments enabled. If the attachment is not already
      present on the wiki then the attachment upload page is shown allowing you to
      upload the missing attachment.

      You must map the mimetype "image/svg+xml" to the .svg and .svgz extensions
      - either in your web server configuration...Python will automatically
        add the mimetypes found in the following files:
            "/etc/mime.types",
            "/usr/local/etc/httpd/conf/mime.types",
            "/usr/local/lib/netscape/mime.types",
            "/usr/local/etc/httpd/conf/mime.types",     # Apache 1.2
            "/usr/local/etc/mime.types",                # Apache 1.3
        ...but they most likely are not already defined in any of them.
      - or by adding the following lines to wikiconfig.py:
        import mimetypes
        mimetypes.add_type("image/svg_xml", ".svg", True)
        mimetypes.add_type("image/svg_xml", ".svgz", True)
      - or by some other method that I am not aware of.

      You will need Internet Explorer and the Adobe SvgViewer plugin before
      SVG content can be seen. Mozilla and Firefox, as at 2005, are not
      up to it. Who knows about the others.

      This macro must be put in "[yourwiki]/data/plugin/macro/"

    CREDITS:
      This macro was built using the ImageLink macro as a template.
      Please feel free to post improved versions of it.

    MODIFICATION HISTORY:
        @license: GNU GPL, see COPYING for details.


"""

from MoinMoin.action  import AttachFile
from MoinMoin         import wikiutil, config
import os,string


def execute(macro, text):

  kw = {'width': '100%', 'height': '100%'}

  if text:
    args = text.split(',')
  else:
    args = []

  argCount = len(args)
  kwCount = 0
  for a in args:
    if (a.find('=') > -1):
      kwCount = kwCount + 1
      key = a.split('=')
      kw[str(key[0])] = wikiutil.escape(string.join(key[1],''), quote=1)

  argCount = argCount - kwCount

  if (argCount < 1):
    msg = 'Not enough arguments to svg macro! Try [[svg(attachment[,width=n][,height=n])]]'
    return macro.formatter.sysmsg(1) + macro.formatter.text(msg) + macro.formatter.sysmsg(0)

  attachmentName = wikiutil.taintfilename(macro.formatter.text(args[0]))
  currentPageName = macro.formatter.page.page_name
  kw['src'] = AttachFile.getAttachUrl(currentPageName,attachmentName,macro.request)
  attachmentPath = os.path.join(AttachFile.getAttachDir(macro.request,currentPageName), attachmentName)

  if not os.path.exists(attachmentPath):
    import urllib
    linktext = macro.request.getText('Upload new attachment "%(filename)s"')
    return wikiutil.link_tag(macro.request,
                             '%s?action=AttachFile&amp;rename=%s' %
                             (wikiutil.quoteWikinameFS(currentPageName),
                              urllib.quote_plus(attachmentName)),
                             linktext % {'filename': attachmentName})

  if (argCount == 1):
    objectTag = '<object type="image/svg+xml" data="%s" width="%s" height="%s">Your browser cannot display SVG</object>' \
                % (wikiutil.escape(kw['src']), kw['width'], kw['height'])
    return macro.formatter.rawHTML(objectTag)


