]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/sparkline.pm
support previewing of sparklines: embed in data uris for preview
[ikiwiki.git] / IkiWiki / Plugin / sparkline.pm
index 27e4ff154cdbf36f474971ec9028f8add4a75479..63c2cf94391127018dbc57460ecb5a8569c73e46 100644 (file)
@@ -60,13 +60,13 @@ sub preprocess (@) { #{{{
                        }
                }
                elsif (! length $value) {
-                       return "[[sparkline parse error \"$key\"]]";
+                       return "[[".gettext("sparkline parse error")." \"$key\"]]";
                }
                elsif ($key eq 'featurepoint') {
                        my ($x, $y, $color, $diameter, $text, $location)=
                                split(/\s*,\s*/, $value);
                        if (! defined $diameter || $diameter < 0) {
-                               return "[[sparkline bad featurepoint diameter]]";
+                               return "[[".gettext("sparkline bad featurepoint diameter")."]]";
                        }
                        $x=int($x);
                        $y=int($y);
@@ -76,7 +76,7 @@ sub preprocess (@) { #{{{
                        if (defined $location) {
                                $location=$locmap{$location};
                                if (! defined $location) {
-                                       return "[[sparkline bad featurepoint location]]";
+                                       return "[[".gettext("sparkline bad featurepoint location")."]]";
                                }
                        }
                        $php.=qq{\$sparkline->SetFeaturePoint($x, $y, '$color', $diameter};
@@ -87,31 +87,27 @@ sub preprocess (@) { #{{{
        }
 
        if ($c eq 0) {
-               return "[[sparkline missing values]]";
+               return "[[".gettext("sparkline missing values")."]]";
        }
 
        my $height=int($params{height} || 20);
        if ($height < 2 || $height > 100) {
-               return "[[sparkline bad height value]]";
+               return "[[".gettext("sparkline bad height value")."]]";
        }
        if ($style eq "Bar") {
                $php.=qq{\$sparkline->Render($height);\n};
        }
        else {
                if (! exists $params{width}) {
-                       return "[[sparkline missing width parameter]]";
+                       return "[[".gettext("sparkline missing width parameter")."]]";
                }
                my $width=int($params{width});
                if ($width < 2 || $width > 1024) {
-                       return "[[sparkline bad width value]]";
+                       return "[[".gettext("sparkline bad width value")."]]";
                }
                $php.=qq{\$sparkline->RenderResampled($width, $height);\n};
        }
        
-       if ($params{preview}) {
-               return "[[sparkline previewing not implemented]]";
-       }
-       
        $php.=qq{\$sparkline->Output();\n?>\n};
 
        # Use the sha1 of the php code that generates the sparkline as
@@ -148,7 +144,16 @@ sub preprocess (@) { #{{{
                        return  "[[".gettext("sparkline failed to run php")."]]";
                }
 
-               writefile($fn, $config{destdir}, $png, 1);
+               if (! $params{preview}) {
+                       writefile($fn, $config{destdir}, $png, 1);
+               }
+               else {
+                       # can't write the file, so embed it in a data uri
+                       eval q{use MIME::Base64};
+                       error($@) if $@;
+                       return "<img src=\"data:image/png;base64,".
+                               encode_base64($png)."\" />";
+               }
        }
 
        return '<img src="'.