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=pagespec_match_list(
54 [ grep { $_ ne $params{page} } keys %pagesources],
55 $params{pages}, location => $params{page});
57 @list = sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} } @list;
59 my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
65 # generate an empty graph
66 push @data, 0 foreach 1..($params{max} / 2);
69 my $color=exists $params{color} ? "($params{color})" : "";
71 delete $params{pages};
72 delete $params{formula};
73 delete $params{ftime};
74 delete $params{color};
75 return IkiWiki::Plugin::sparkline::preprocess(%params,
76 map { $_.$color => "" } reverse @data);
83 my $max=$params->{max};
84 my ($first, $prev, $cur);
88 $cur=$sub->($params->{timehash}->{$_});
91 push @data, "$prev,$count";
95 for ($cur+1 .. $prev-1) {
111 package IkiWiki::Plugin::postsparkline::formula;
114 return IkiWiki::Plugin::postsparkline::perfoo(sub {
115 return (localtime $_[0])[5];
120 return IkiWiki::Plugin::postsparkline::perfoo(sub {
121 my ($month, $year)=(localtime $_[0])[4,5];
122 return $year*12+$month;
127 return IkiWiki::Plugin::postsparkline::perfoo(sub {
128 my ($year, $yday)=(localtime $_[0])[5,7];
129 return $year*365+$yday;
136 my $max=$params->{max};
138 for (my $i=1; $i < @_; $i++) {
139 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};