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 $@]]";
59 my $color=exists $params{color} ? "($params{color})" : "";
61 delete $params{pages};
62 delete $params{formula};
63 delete $params{ftime};
64 delete $params{color};
65 return IkiWiki::Plugin::sparkline::preprocess(%params,
66 map { $_.$color => "" } reverse @data);
73 my $max=$params->{max};
74 my ($first, $prev, $cur);
78 $cur=$sub->($params->{timehash}->{$_});
81 push @data, "$prev,$count";
85 for ($cur+1 .. $prev-1) {
101 package IkiWiki::Plugin::postsparkline::formula;
104 return IkiWiki::Plugin::postsparkline::perfoo(sub {
105 return (localtime $_[0])[5];
110 return IkiWiki::Plugin::postsparkline::perfoo(sub {
111 my ($month, $year)=(localtime $_[0])[4,5];
112 return $year*12+$month;
117 return IkiWiki::Plugin::postsparkline::perfoo(sub {
118 my ($year, $yday)=(localtime $_[0])[5,7];
119 return $year*365+$yday;
126 my $max=$params->{max};
128 for (my $i=1; $i < @_; $i++) {
129 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};