]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/sparkline.pm
Merge branch 'master' of ssh://git.ikiwiki.info
[git.ikiwiki.info.git] / IkiWiki / Plugin / sparkline.pm
index a44e062520f9132d89f91f83ef37a8d3e49f50bc..e28d2605a2c1802e6afe798cab14548529d55936 100644 (file)
@@ -3,7 +3,7 @@ package IkiWiki::Plugin::sparkline;
 
 use warnings;
 use strict;
-use IkiWiki;
+use IkiWiki 3.00;
 use IPC::Open2;
 
 my $match_num=qr/[-+]?[0-9]+(?:\.[0-9]+)?/;
@@ -14,11 +14,21 @@ my %locmap=(
        left => 'TEXT_LEFT',
 );
 
-sub import { #{{{
+sub import {
+       hook(type => "getsetup", id => "sparkline", call => \&getsetup);
        hook(type => "preprocess", id => "sparkline", call => \&preprocess);
-} # }}}
-
-sub preprocess (@) { #{{{
+}
+
+sub getsetup () {
+       return
+               plugin => {
+                       safe => 1,
+                       rebuild => undef,
+                       section => "widget",
+               },
+}
+
+sub preprocess (@) {
        my %params=@_;
 
        my $php;
@@ -60,13 +70,13 @@ sub preprocess (@) { #{{{
                        }
                }
                elsif (! length $value) {
-                       return "[[".gettext("sparkline parse error")." \"$key\"]]";
+                       error gettext("parse error")." \"$key\"";
                }
                elsif ($key eq 'featurepoint') {
                        my ($x, $y, $color, $diameter, $text, $location)=
                                split(/\s*,\s*/, $value);
                        if (! defined $diameter || $diameter < 0) {
-                               return "[[".gettext("sparkline bad featurepoint diameter")."]]";
+                               error gettext("invalid featurepoint diameter");
                        }
                        $x=int($x);
                        $y=int($y);
@@ -76,7 +86,7 @@ sub preprocess (@) { #{{{
                        if (defined $location) {
                                $location=$locmap{$location};
                                if (! defined $location) {
-                                       return "[[".gettext("sparkline bad featurepoint location")."]]";
+                                       error gettext("invalid featurepoint location");
                                }
                        }
                        $php.=qq{\$sparkline->SetFeaturePoint($x, $y, '$color', $diameter};
@@ -87,45 +97,41 @@ sub preprocess (@) { #{{{
        }
 
        if ($c eq 0) {
-               return "[[".gettext("sparkline missing values")."]]";
+               error gettext("missing values");
        }
 
        my $height=int($params{height} || 20);
        if ($height < 2 || $height > 100) {
-               return "[[".gettext("sparkline bad height value")."]]";
+               error gettext("invalid height value");
        }
        if ($style eq "Bar") {
                $php.=qq{\$sparkline->Render($height);\n};
        }
        else {
                if (! exists $params{width}) {
-                       return "[[".gettext("sparkline missing width parameter")."]]";
+                       error gettext("missing width parameter");
                }
                my $width=int($params{width});
                if ($width < 2 || $width > 1024) {
-                       return "[[".gettext("sparkline bad width value")."]]";
+                       error gettext("invalid width value");
                }
                $php.=qq{\$sparkline->RenderResampled($width, $height);\n};
        }
        
-       if ($params{preview}) {
-               return "[[".gettext("sparkline previewing not implemented")."]]";
-       }
-       
        $php.=qq{\$sparkline->Output();\n?>\n};
 
        # Use the sha1 of the php code that generates the sparkline as
        # the base for its filename.
-       eval q{use Digest::SHA1};
+       eval q{use Digest::SHA};
         error($@) if $@;
        my $fn=$params{page}."/sparkline-".
-               IkiWiki::possibly_foolish_untaint(Digest::SHA1::sha1_hex($php)).
+               IkiWiki::possibly_foolish_untaint(Digest::SHA::sha1_hex($php)).
                ".png";
        will_render($params{page}, $fn);
 
        if (! -e "$config{destdir}/$fn") {
                my $pid;
-               my $sigpipe=0;;
+               my $sigpipe=0;
                $SIG{PIPE}=sub { $sigpipe=1 };
                $pid=open2(*IN, *OUT, "php");
 
@@ -144,16 +150,24 @@ sub preprocess (@) { #{{{
 
                waitpid $pid, 0;
                $SIG{PIPE}="DEFAULT";
-               if ($sigpipe) {
-                       return  "[[".gettext("sparkline failed to run php")."]]";
+               if ($sigpipe || ! defined $png) {
+                       error gettext("failed to run php");
                }
 
-               writefile($fn, $config{destdir}, $png, 1);
+               if (! $params{preview}) {
+                       writefile($fn, $config{destdir}, $png, 1);
+               }
+               else {
+                       # in preview mode, embed the image in a data uri
+                       # to avoid temp file clutter
+                       eval q{use MIME::Base64};
+                       error($@) if $@;
+                       return "<img src=\"data:image/png;base64,".
+                               encode_base64($png)."\" />";
+               }
        }
 
-       return '<img src="'.
-               IkiWiki::abs2rel($fn, IkiWiki::dirname($params{destpage})).
-               '" alt="graph" />';
-} # }}}
+       return '<img src="'.urlto($fn, $params{destpage}).'" alt="graph" />';
+}
 
 1