view data/plugin/action/SvgEditor.py @ 653:127da830be6c

updated copyright
author Reimar Bauer <rb.proj AT googlemail DOT com>
date Tue, 19 Apr 2016 16:29:54 +0200
parents 815a86c7fbf1
children
line wrap: on
line source
# -*- coding: utf-8 -*-
"""
    MoinMoin - SvgEditor

    This action is used to call svg-edit, (http://code.google.com/p/svg-edit/)

    svg-edit pre alpha 2.5 is called
    this action needs moin-1.9 and is currently a mockup for an implementation in moin-2.0

    @copyright: 2009-2016 by MoinMoin:ReimarBauer
    @license: GNU GPL, see COPYING for details.
"""
import re
from MoinMoin import config, wikiutil
from MoinMoin.action import AttachFile, do_show
from MoinMoin.Page import Page

action_name = __name__.split('.')[-1]

def gedit_drawing(self, url, text, **kw):
    # This is called for displaying a drawing image by gui editor.
    _ = self.request.getText
    # TODO: this 'text' argument is kind of superfluous, replace by using alt=... kw arg
    # ToDo: make this clickable for the gui editor
    if 'alt' not in kw or not kw['alt']:
        kw['alt'] = text
    # we force the title here, needed later for html>wiki converter
    kw['title'] = "drawing:%s" % wikiutil.quoteWikinameURL(url)
    pagename, drawing = AttachFile.absoluteName(url, self.page.page_name)
    drawing = wikiutil.taintfilename(drawing)
    drawing_url = AttachFile.getAttachUrl(pagename, drawing, self.request, do='modify')
    if not AttachFile.exists(self.request, pagename, drawing+'.svg'):
        title = _('Create new drawing "%(filename)s (opens in new window)"') % {'filename': drawing}
        img = self.icon('attachimg')  # TODO: we need a new "drawimg" in similar grey style and size
        css = 'nonexistent'

    # ToDo use EmbedObject here

    kw['src'] = drawing + '.svg'
    return self.image(**kw)

def attachment_drawing(self, url, text, **kw):
    # This is called for displaying a clickable drawing image by text_html formatter.
    # XXX text arg is unused!
    _ = self.request.getText
    pagename, drawing = AttachFile.absoluteName(url, self.page.page_name)
    drawing = wikiutil.taintfilename(drawing)
    drawing_url = AttachFile.getAttachUrl(pagename, drawing, self.request, do='modify')
    if not AttachFile.exists(self.request, pagename, drawing):
        title = _('Create new drawing "%(filename)s (opens in new window)"') % {'filename': drawing}
        img = self.icon('attachimg')  # TODO: we need a new "drawimg" in similar grey style and size
        css = 'nonexistent'
        return self.url(1, drawing_url, css=css, title=title) + img + self.url(0)

    title = _('Edit drawing %(filename)s (opens in new window)') % {'filename': self.text(drawing)}

    from MoinMoin import macro
    from MoinMoin.parser.text import Parser
    macro.request = self.request
    macro.formatter = self.request.html_formatter
    p = Parser("##\n", self.request)
    m = macro.Macro(p)
    img = m.execute('EmbedObject', u'target=%s' % drawing)
    return img

class SvgEditor(object):
    """ svg-edit action """
    def __init__(self, request, pagename, target):
        self._ = request.getText
        self.request = request
        self.pagename = pagename
        self.target = target

    def render_msg(self, msg, msgtype):
        """ Called to display some message (can also be the action form) """
        self.request.theme.add_msg(msg, msgtype)
        do_show(self.pagename, self.request)

    def save(self):
        _ = self._
        pagename = self.pagename
        request = self.request
        if not request.user.may.write(pagename):
            return _('You are not allowed to save a drawing on this page.')

        filecontent = request.values.get('svg_data', '')
        target = request.values.get('target', 'example.svg').strip()
        target = wikiutil.taintfilename(target)
        if filecontent:
            AttachFile._addLogEntry(request, 'ATTDRW', pagename, target)
            # get directory, and possibly create it
            attach_dir = AttachFile.getAttachDir(request, pagename, create=1)
            # save file
            fpath = AttachFile.getFilename(request, pagename, target).encode(config.charset)
            content = filecontent.decode('base_64')
            # Workaround bug in svg-edit editor. On edit of an item the target becomes removed 
            p = re.compile(r'target="_blank"', re.MULTILINE | re.DOTALL)
            new_content = p.sub(' ', content)
            p = re.compile(r""" xlink:href""", re.MULTILINE | re.DOTALL)
            new_content = p.sub(' target="_blank" xlink:href', new_content)
            with open(fpath, 'w') as f:
                f.write(new_content)

        url = Page(request, pagename).url(request)
        request.http_redirect(url)

    def render(self):
        _ = self._
        request = self.request
        pagename = self.pagename
        target = self.target
        drawing_url = AttachFile.getAttachUrl(pagename, target, request, do='get')
        url = request.getQualifiedURL()
        base_url = "%s/svg-edit/editor/svg-editor.html" % (request.cfg.url_prefix_static)
        html = u"""<object data="%s%s?url=%s" type="text/html" width="100%%" height="600"></object>\n""" % (url, base_url, drawing_url)
        content = request.formatter.rawHTML(html)
        title = '%s %s:%s' % (_('Edit drawing'), pagename, target)
        request.theme.send_title(title, page=request.page, pagename=pagename)
        request.write(request.formatter.startContent("content"))
        request.write(request.formatter.rawHTML(html))
        request.write(request.formatter.endContent())
        request.theme.send_footer(pagename)
        request.theme.send_closing_html()

def execute(pagename, request):
    #ToDo refactoring to class

    _ = request.getText
    msg = None
    if not request.user.may.read(pagename):
        msg = '<p>%s</p>' % _('You are not allowed to view this page.')
        SvgEditor(request, pagename, target).render_msg(msg, 'error')
        return

    target = request.values.get('target', '')
    if not target:
        msg = '<p>%s</p>' % _("Empty target given.")
        SvgEditor(request, pagename, target).render_msg(msg, 'error')
        return

    do = request.values.get('do', '')
    if do == 'save' and request.user.may.write(pagename):
        msg = SvgEditor(request, pagename, target).save()
        SvgEditor(request, pagename, target).render_msg(msg, 'error')
        return

    SvgEditor(request, pagename, target).render()