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{formula}) {
25 return "[[postsparkline ".gettext("missing formula")."]]";
27 my $formula=$params{formula};
28 $formula=~s/[^a-zA-Z0-9]*//g;
29 $formula=IkiWiki::possibly_foolish_untaint($formula);
30 if (! length $formula ||
31 ! IkiWiki::Plugin::postsparkline::formula->can($formula)) {
32 return "[[postsparkline ".gettext("unknown formula")."]]";
35 add_depends($params{page}, $params{pages});
38 foreach my $page (keys %pagesources) {
39 next if $page eq $params{page};
40 if (pagespec_match($page, $params{pages}, location => $params{page})) {
45 @list = sort { $IkiWiki::pagectime{$b} <=> $IkiWiki::pagectime{$a} } @list;
47 delete $params{pages};
48 delete $params{formula};
49 my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
51 return "[[postsparkline error $@]]";
53 return IkiWiki::Plugin::sparkline::preprocess(%params,
54 map { $_ => "" } reverse @data);
61 my $max=$params->{max};
62 my ($first, $prev, $cur);
66 $cur=$sub->($IkiWiki::pagectime{$_});
69 push @data, "$prev,$count";
73 for ($cur+1 .. $prev-1) {
89 package IkiWiki::Plugin::postsparkline::formula;
92 return IkiWiki::Plugin::postsparkline::perfoo(sub {
93 return (localtime $_[0])[5];
98 return IkiWiki::Plugin::postsparkline::perfoo(sub {
99 my ($month, $year)=(localtime $_[0])[4,5];
100 return $year*12+$month;
105 return IkiWiki::Plugin::postsparkline::perfoo(sub {
106 my ($year, $yday)=(localtime $_[0])[5,7];
107 return $year*365+$yday;
114 my $max=$params->{max};
116 for (my $i=1; $i < @_; $i++) {
117 push @data, $IkiWiki::pagectime{$_[$i-1]} - $IkiWiki::pagectime{$_[$i]};