improve l10n
[ikiwiki] / IkiWiki / Plugin / sparkline.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::sparkline;
3
4 use warnings;
5 use strict;
6 use IkiWiki;
7 use IPC::Open2;
8
9 my $match_num=qr/[-+]?[0-9]+(?:\.[0-9]+)?/;
10 my %locmap=(
11         top => 'TEXT_TOP',
12         right => 'TEXT_RIGHT',
13         bottom => 'TEXT_BOTTOM',
14         left => 'TEXT_LEFT',
15 );
16
17 sub import { #{{{
18         hook(type => "preprocess", id => "sparkline", call => \&preprocess);
19 } # }}}
20
21 sub preprocess (@) { #{{{
22         my %params=@_;
23
24         my $php;
25
26         my $style=(exists $params{style} && $params{style} eq "bar") ? "Bar" : "Line";
27         $php=qq{<?php
28                 require_once('sparkline/Sparkline_$style.php');
29                 \$sparkline = new Sparkline_$style();
30                 \$sparkline->SetDebugLevel(DEBUG_NONE);
31         };
32
33         foreach my $param (qw{BarWidth BarSpacing YMin YMaz}) {
34                 if (exists $params{lc($param)}) {
35                         $php.=qq{\$sparkline->Set$param(}.int($params{lc($param)}).qq{);\n};
36                 }
37         }
38
39         my $c=0;
40         while (@_) {
41                 my $key=shift;
42                 my $value=shift;
43
44                 if ($key=~/^($match_num)(?:,($match_num))?(?:\(([a-z]+)\))?$/) {
45                         $c++;
46                         my ($x, $y);
47                         if (defined $2) {
48                                 $x=$1;
49                                 $y=$2;
50                         }
51                         else {
52                                 $x=$c;
53                                 $y=$1;
54                         }
55                         if ($style eq "Bar" && defined $3) {
56                                 $php.=qq{\$sparkline->SetData($x, $y, '$3');\n};
57                         }
58                         else {
59                                 $php.=qq{\$sparkline->SetData($x, $y);\n};
60                         }
61                 }
62                 elsif (! length $value) {
63                         return "[[".gettext("sparkline parse error")." \"$key\"]]";
64                 }
65                 elsif ($key eq 'featurepoint') {
66                         my ($x, $y, $color, $diameter, $text, $location)=
67                                 split(/\s*,\s*/, $value);
68                         if (! defined $diameter || $diameter < 0) {
69                                 return "[[".gettext("sparkline bad featurepoint diameter")."]]";
70                         }
71                         $x=int($x);
72                         $y=int($y);
73                         $color=~s/[^a-z]+//g;
74                         $diameter=int($diameter);
75                         $text=~s/[^-a-zA-Z0-9]+//g if defined $text;
76                         if (defined $location) {
77                                 $location=$locmap{$location};
78                                 if (! defined $location) {
79                                         return "[[".gettext("sparkline bad featurepoint location")."]]";
80                                 }
81                         }
82                         $php.=qq{\$sparkline->SetFeaturePoint($x, $y, '$color', $diameter};
83                         $php.=qq{, '$text'} if defined $text;
84                         $php.=qq{, $location} if defined $location;
85                         $php.=qq{);\n};
86                 }
87         }
88
89         if ($c eq 0) {
90                 return "[[".gettext("sparkline missing values")."]]";
91         }
92
93         my $height=int($params{height} || 20);
94         if ($height < 2 || $height > 100) {
95                 return "[[".gettext("sparkline bad height value")."]]";
96         }
97         if ($style eq "Bar") {
98                 $php.=qq{\$sparkline->Render($height);\n};
99         }
100         else {
101                 if (! exists $params{width}) {
102                         return "[[".gettext("sparkline missing width parameter")."]]";
103                 }
104                 my $width=int($params{width});
105                 if ($width < 2 || $width > 1024) {
106                         return "[[".gettext("sparkline bad width value")."]]";
107                 }
108                 $php.=qq{\$sparkline->RenderResampled($width, $height);\n};
109         }
110         
111         if ($params{preview}) {
112                 return "[[".gettext("sparkline previewing not implemented")."]]";
113         }
114         
115         $php.=qq{\$sparkline->Output();\n?>\n};
116
117         # Use the sha1 of the php code that generates the sparkline as
118         # the base for its filename.
119         eval q{use Digest::SHA1};
120         error($@) if $@;
121         my $fn=$params{page}."/sparkline-".
122                 IkiWiki::possibly_foolish_untaint(Digest::SHA1::sha1_hex($php)).
123                 ".png";
124         will_render($params{page}, $fn);
125
126         if (! -e "$config{destdir}/$fn") {
127                 my $pid;
128                 my $sigpipe=0;;
129                 $SIG{PIPE}=sub { $sigpipe=1 };
130                 $pid=open2(*IN, *OUT, "php");
131
132                 # open2 doesn't respect "use open ':utf8'"
133                 binmode (OUT, ':utf8');
134
135                 print OUT $php;
136                 close OUT;
137
138                 my $png;
139                 {
140                         local $/=undef;
141                         $png=<IN>;
142                 }
143                 close IN;
144
145                 waitpid $pid, 0;
146                 $SIG{PIPE}="DEFAULT";
147                 if ($sigpipe) {
148                         return  "[[".gettext("sparkline failed to run php")."]]";
149                 }
150
151                 writefile($fn, $config{destdir}, $png, 1);
152         }
153
154         return '<img src="'.
155                 IkiWiki::abs2rel($fn, IkiWiki::dirname($params{destpage})).
156                 '" alt="graph" />';
157 } # }}}
158
159 1