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}) {
34 if (! exists $params{time} || $params{time} ne 'mtime') {
35 $params{timehash} = \%IkiWiki::pagectime;
36 # need to update when pages are added or removed
37 $deptype = deptype("presence");
40 $params{timehash} = \%IkiWiki::pagemtime;
41 # need to update when pages are changed
42 $deptype = deptype("content");
45 if (! exists $params{formula}) {
46 error gettext("missing formula")
48 my $formula=$params{formula};
49 $formula=~s/[^a-zA-Z0-9]*//g;
50 $formula=IkiWiki::possibly_foolish_untaint($formula);
51 if (! length $formula ||
52 ! IkiWiki::Plugin::postsparkline::formula->can($formula)) {
53 error gettext("unknown formula");
56 my @list=sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} }
57 use_pagespec($params{page}, $params{pages},
59 limit => sub { $_[0] ne $params{page} },
62 my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
68 # generate an empty graph
69 push @data, 0 foreach 1..($params{max} / 2);
72 my $color=exists $params{color} ? "($params{color})" : "";
74 delete $params{pages};
75 delete $params{formula};
76 delete $params{ftime};
77 delete $params{color};
78 return IkiWiki::Plugin::sparkline::preprocess(%params,
79 map { $_.$color => "" } reverse @data);
86 my $max=$params->{max};
87 my ($first, $prev, $cur);
91 $cur=$sub->($params->{timehash}->{$_});
94 push @data, "$prev,$count";
98 for ($cur+1 .. $prev-1) {
114 package IkiWiki::Plugin::postsparkline::formula;
117 return IkiWiki::Plugin::postsparkline::perfoo(sub {
118 return (localtime $_[0])[5];
123 return IkiWiki::Plugin::postsparkline::perfoo(sub {
124 my ($month, $year)=(localtime $_[0])[4,5];
125 return $year*12+$month;
130 return IkiWiki::Plugin::postsparkline::perfoo(sub {
131 my ($year, $yday)=(localtime $_[0])[5,7];
132 return $year*365+$yday;
139 my $max=$params->{max};
141 for (my $i=1; $i < @_; $i++) {
142 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};