2 package IkiWiki::Plugin::postsparkline;
9 IkiWiki::loadplugin('sparkline');
10 hook(type => "getsetup", id => "postsparkline", call => \&getsetup);
11 hook(type => "preprocess", id => "postsparkline", call => \&preprocess);
25 if (! exists $params{max}) {
29 if (! exists $params{pages}) {
33 if (! exists $params{time} || $params{time} ne 'mtime') {
34 $params{timehash} = \%IkiWiki::pagectime;
37 $params{timehash} = \%IkiWiki::pagemtime;
40 if (! exists $params{formula}) {
41 error gettext("missing formula")
43 my $formula=$params{formula};
44 $formula=~s/[^a-zA-Z0-9]*//g;
45 $formula=IkiWiki::possibly_foolish_untaint($formula);
46 if (! length $formula ||
47 ! IkiWiki::Plugin::postsparkline::formula->can($formula)) {
48 error gettext("unknown formula");
51 add_depends($params{page}, $params{pages});
53 my @list=sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} }
55 [ grep { $_ ne $params{page} } keys %pagesources],
56 $params{pages}, location => $params{page});
58 my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
64 # generate an empty graph
65 push @data, 0 foreach 1..($params{max} / 2);
68 my $color=exists $params{color} ? "($params{color})" : "";
70 delete $params{pages};
71 delete $params{formula};
72 delete $params{ftime};
73 delete $params{color};
74 return IkiWiki::Plugin::sparkline::preprocess(%params,
75 map { $_.$color => "" } reverse @data);
82 my $max=$params->{max};
83 my ($first, $prev, $cur);
87 $cur=$sub->($params->{timehash}->{$_});
90 push @data, "$prev,$count";
94 for ($cur+1 .. $prev-1) {
110 package IkiWiki::Plugin::postsparkline::formula;
113 return IkiWiki::Plugin::postsparkline::perfoo(sub {
114 return (localtime $_[0])[5];
119 return IkiWiki::Plugin::postsparkline::perfoo(sub {
120 my ($month, $year)=(localtime $_[0])[4,5];
121 return $year*12+$month;
126 return IkiWiki::Plugin::postsparkline::perfoo(sub {
127 my ($year, $yday)=(localtime $_[0])[5,7];
128 return $year*365+$yday;
135 my $max=$params->{max};
137 for (my $i=1; $i < @_; $i++) {
138 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};