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 return "[[postsparkline ".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 return "[[postsparkline ".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)};
56 return "[[postsparkline error $@]]";
58 delete $params{pages};
59 delete $params{formula};
60 delete $params{ftime};
61 return IkiWiki::Plugin::sparkline::preprocess(%params,
62 map { $_ => "" } reverse @data);
69 my $max=$params->{max};
70 my ($first, $prev, $cur);
74 $cur=$sub->($params->{timehash}->{$_});
77 push @data, "$prev,$count";
81 for ($cur+1 .. $prev-1) {
97 package IkiWiki::Plugin::postsparkline::formula;
100 return IkiWiki::Plugin::postsparkline::perfoo(sub {
101 return (localtime $_[0])[5];
106 return IkiWiki::Plugin::postsparkline::perfoo(sub {
107 my ($month, $year)=(localtime $_[0])[4,5];
108 return $year*12+$month;
113 return IkiWiki::Plugin::postsparkline::perfoo(sub {
114 my ($year, $yday)=(localtime $_[0])[5,7];
115 return $year*365+$yday;
122 my $max=$params->{max};
124 for (my $i=1; $i < @_; $i++) {
125 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};