2 package IkiWiki::Plugin::postsparkline;
9 IkiWiki::loadplugin('sparkline');
10 hook(type => "preprocess", id => "postsparkline", call => \&preprocess);
13 sub preprocess (@) { #{{{
16 if (! exists $params{max}) {
20 if (! exists $params{pages}) {
24 if (! exists $params{time} || $params{time} ne 'mtime') {
25 $params{timehash} = \%IkiWiki::pagectime;
28 $params{timehash} = \%IkiWiki::pagemtime;
31 if (! exists $params{formula}) {
32 error gettext("missing formula")
34 my $formula=$params{formula};
35 $formula=~s/[^a-zA-Z0-9]*//g;
36 $formula=IkiWiki::possibly_foolish_untaint($formula);
37 if (! length $formula ||
38 ! IkiWiki::Plugin::postsparkline::formula->can($formula)) {
39 error gettext("unknown formula");
42 add_depends($params{page}, $params{pages});
45 foreach my $page (keys %pagesources) {
46 next if $page eq $params{page};
47 if (pagespec_match($page, $params{pages}, location => $params{page})) {
52 @list = sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} } @list;
54 my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
60 # generate an empty graph
61 push @data, 0 foreach 1..($params{max} / 2);
64 my $color=exists $params{color} ? "($params{color})" : "";
66 delete $params{pages};
67 delete $params{formula};
68 delete $params{ftime};
69 delete $params{color};
70 return IkiWiki::Plugin::sparkline::preprocess(%params,
71 map { $_.$color => "" } reverse @data);
78 my $max=$params->{max};
79 my ($first, $prev, $cur);
83 $cur=$sub->($params->{timehash}->{$_});
86 push @data, "$prev,$count";
90 for ($cur+1 .. $prev-1) {
106 package IkiWiki::Plugin::postsparkline::formula;
109 return IkiWiki::Plugin::postsparkline::perfoo(sub {
110 return (localtime $_[0])[5];
115 return IkiWiki::Plugin::postsparkline::perfoo(sub {
116 my ($month, $year)=(localtime $_[0])[4,5];
117 return $year*12+$month;
122 return IkiWiki::Plugin::postsparkline::perfoo(sub {
123 my ($year, $yday)=(localtime $_[0])[5,7];
124 return $year*365+$yday;
131 my $max=$params->{max};
133 for (my $i=1; $i < @_; $i++) {
134 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};