# Copyright (C) 2010 Alex Schroeder # # This program is free software; you can redistribute it and/or modify it under # the terms of the GNU General Public License as published by the Free Software # Foundation; either version 3 of the License, or (at your option) any later # version. # # This program is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. # # You should have received a copy of the GNU General Public License along with # this program. If not, see . use strict; use v5.10; our ($q, %Action, %Page, $OpenPageName, @MyInitVariables, $UploadAllowed, @UploadTypes, $FullUrl, $HtmlHeaders); our ($SvgMimeType, $SvgEditorUrl); AddModuleDescription('svg-edit.pl', 'SVG Editor Extension'); $SvgMimeType = 'image/svg+xml'; $SvgEditorUrl = 'http://svg-edit.googlecode.com/svn/tags/stable/editor/svg-editor.html'; push (@MyInitVariables, \&SvgInitVariables); sub SvgInitVariables { $UploadAllowed = 1; push (@UploadTypes, $SvgMimeType) unless grep {$_ eq $SvgMimeType} @UploadTypes; } *OldSvgGetDownloadLink = \&GetDownloadLink; *GetDownloadLink = \&NewSvgGetDownloadLink; sub NewSvgGetDownloadLink { my ($name, $image, $revision, $alt) = @_; return OldSvgGetDownloadLink(@_) if $image != 1; # determine if this is SVG data we need to show in an iframe my $data; local (%Page, $OpenPageName); OpenPage($name); if ($revision) { $data = GetTextRevision($revision)->{text}; # ignore revision reset } else { $data = $Page{text}; } return OldSvgGetDownloadLink(@_) unless SvgItIs($data); my ($width, $height) = SvgDimensions($data); # add 20 to compensate for scrollbars? return $q->iframe({-width => $width + 20, -height => $height + 20, -src => OldSvgGetDownloadLink($name, 2, $revision)}, ""); } sub SvgItIs { my ($type) = TextIsFile(shift); return $type eq $SvgMimeType; } sub SvgDimensions { my $data = shift; $data =~ s/.*\n//; # strip first line require MIME::Base64; $data = MIME::Base64::decode($data); # crude hack to avoid parsing the SVG XML my ($x) = $data =~ /width="(.*?)"/; my ($y) = $data =~ /height="(.*?)"/; return $x, $y; } *OldSvgGetEditForm = \&GetEditForm; *GetEditForm = \&NewSvgGetEditForm; sub NewSvgGetEditForm { my $html = OldSvgGetEditForm(@_); my $action = 'action=svg;id=' . UrlEncode($OpenPageName); $action .= ";revision=" . GetParam('revision', '') if GetParam('revision', ''); my $link = ScriptLink($action, T('Edit image in the browser'), 'svg'); my $text1 = T('Replace this file with text'); my $text2 = T('Replace this text with a file'); $html =~ s!($text1|$text2)!$1 $link!; return $html; } $Action{svg} = \&DoSvg; sub DoSvg { my $id = shift; my $summary = T('Summary of your changes:') . ' '; $HtmlHeaders .= qq{ }; print GetHeader('', Ts('Editing %s', $id)); # This only works if editor and file are on the same site, I think. my $url = GetDownloadLink($id, 2, GetParam('revision', '')); my $src = $SvgEditorUrl . '?url=' . UrlEncode($url); print $q->iframe({-src => $src, -name => 'svgeditor', -width => "100%", -height => "500"}, ""); PrintFooter($id, 'edit'); }