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