]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/postsparkline.pm
web commit by http://dmarti.myopenid.com/
[git.ikiwiki.info.git] / IkiWiki / Plugin / postsparkline.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::postsparkline;
4 use warnings;
5 use strict;
6 use IkiWiki;
8 sub import { #{{{
9         IkiWiki::loadplugin('sparkline');
10         hook(type => "preprocess", id => "postsparkline", call => \&preprocess);
11 } # }}}
13 sub preprocess (@) { #{{{
14         my %params=@_;
16         if (! exists $params{max}) {
17                 $params{max}=100;
18         }
20         if (! exists $params{pages}) {
21                 return "";
22         }
24         if (! exists $params{formula}) {
25                 return "[[postsparkline ".gettext("missing formula")."]]";
26         }
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")."]]";
33         }
35         add_depends($params{page}, $params{pages});
37         my @list;
38         foreach my $page (keys %pagesources) {
39                 next if $page eq $params{page};
40                 if (pagespec_match($page, $params{pages}, $params{page})) {
41                         push @list, $page;
42                 }
43         }
44         
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)};
50         if ($@) {
51                 return "[[postsparkline error $@]]";
52         }
53         return IkiWiki::Plugin::sparkline::preprocess(%params, 
54                 map { $_ => "" } reverse @data);
55 } # }}}
57 sub perfoo ($@) {
58         my $sub=shift;
59         my $params=shift;
60         
61         my $max=$params->{max};
62         my ($first, $prev, $cur);
63         my $count=0;
64         my @data;
65         foreach (@_) {
66                 $cur=$sub->($IkiWiki::pagectime{$_});
67                 if (defined $prev) {
68                         if ($prev != $cur) {
69                                 push @data, "$prev,$count";
70                                 $count=0;
71                                 last if --$max <= 0;
73                                 for ($cur+1 .. $prev-1) {
74                                         push @data, "$_,0";
75                                         last if --$max == 0;
76                                 }
77                         }
78                 }
79                 else {
80                         $first=$cur;
81                 }
82                 $count++;
83                 $prev=$cur;
84         }
86         return @data;
87 }
89 package IkiWiki::Plugin::postsparkline::formula;
91 sub peryear (@) {
92         return IkiWiki::Plugin::postsparkline::perfoo(sub {
93                 return (localtime $_[0])[5];
94         }, @_);
95 }
97 sub permonth (@) {
98         return IkiWiki::Plugin::postsparkline::perfoo(sub {
99                 my ($month, $year)=(localtime $_[0])[4,5];
100                 return $year*12+$month;
101         }, @_);
104 sub perday (@) {
105         return IkiWiki::Plugin::postsparkline::perfoo(sub {
106                 my ($year, $yday)=(localtime $_[0])[5,7];
107                 return $year*365+$yday;
108         }, @_);
111 sub interval ($@) {
112         my $params=shift;
114         my $max=$params->{max};
115         my @data;
116         for (my $i=1; $i < @_; $i++) {
117                 push @data, $IkiWiki::pagectime{$_[$i-1]} - $IkiWiki::pagectime{$_[$i]};
118                 last if --$max <= 0;
119         }
120         return @data;