]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/postsparkline.pm
po plugin: move translatable pages to setup file, refactor
[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 2.00;
8 sub import { #{{{
9         IkiWiki::loadplugin('sparkline');
10         hook(type => "getsetup", id => "postsparkline", call => \&getsetup);
11         hook(type => "preprocess", id => "postsparkline", call => \&preprocess);
12 } # }}}
14 sub getsetup () { #{{{
15         return 
16                 plugin => {
17                         safe => 1,
18                         rebuild => undef,
19                 },
20 } #}}}
22 sub preprocess (@) { #{{{
23         my %params=@_;
25         if (! exists $params{max}) {
26                 $params{max}=100;
27         }
29         if (! exists $params{pages}) {
30                 return "";
31         }
33         if (! exists $params{time} || $params{time} ne 'mtime') {
34                 $params{timehash} = \%IkiWiki::pagectime;
35         }
36         else {
37                 $params{timehash} = \%IkiWiki::pagemtime;
38         }
40         if (! exists $params{formula}) {
41                 error gettext("missing formula")
42         }
43         my $formula=$params{formula};
44         $formula=~s/[^a-zA-Z0-9]*//g;
45         $formula=IkiWiki::possibly_foolish_untaint($formula);
46         if (! length $formula ||
47             ! IkiWiki::Plugin::postsparkline::formula->can($formula)) {
48                 error gettext("unknown formula");
49         }
51         add_depends($params{page}, $params{pages});
53         my @list;
54         foreach my $page (keys %pagesources) {
55                 next if $page eq $params{page};
56                 if (pagespec_match($page, $params{pages}, location => $params{page})) {
57                         push @list, $page;
58                 }
59         }
60         
61         @list = sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} } @list;
63         my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
64         if ($@) {
65                 error $@;
66         }
68         if (! @data) {
69                 # generate an empty graph
70                 push @data, 0 foreach 1..($params{max} / 2);
71         }
73         my $color=exists $params{color} ? "($params{color})" : "";
75         delete $params{pages};
76         delete $params{formula};
77         delete $params{ftime};
78         delete $params{color};
79         return IkiWiki::Plugin::sparkline::preprocess(%params, 
80                 map { $_.$color => "" } reverse @data);
81 } # }}}
83 sub perfoo ($@) {
84         my $sub=shift;
85         my $params=shift;
86         
87         my $max=$params->{max};
88         my ($first, $prev, $cur);
89         my $count=0;
90         my @data;
91         foreach (@_) {
92                 $cur=$sub->($params->{timehash}->{$_});
93                 if (defined $prev) {
94                         if ($prev != $cur) {
95                                 push @data, "$prev,$count";
96                                 $count=0;
97                                 last if --$max <= 0;
99                                 for ($cur+1 .. $prev-1) {
100                                         push @data, "$_,0";
101                                         last if --$max == 0;
102                                 }
103                         }
104                 }
105                 else {
106                         $first=$cur;
107                 }
108                 $count++;
109                 $prev=$cur;
110         }
112         return @data;
115 package IkiWiki::Plugin::postsparkline::formula;
117 sub peryear (@) {
118         return IkiWiki::Plugin::postsparkline::perfoo(sub {
119                 return (localtime $_[0])[5];
120         }, @_);
123 sub permonth (@) {
124         return IkiWiki::Plugin::postsparkline::perfoo(sub {
125                 my ($month, $year)=(localtime $_[0])[4,5];
126                 return $year*12+$month;
127         }, @_);
130 sub perday (@) {
131         return IkiWiki::Plugin::postsparkline::perfoo(sub {
132                 my ($year, $yday)=(localtime $_[0])[5,7];
133                 return $year*365+$yday;
134         }, @_);
137 sub interval ($@) {
138         my $params=shift;
140         my $max=$params->{max};
141         my @data;
142         for (my $i=1; $i < @_; $i++) {
143                 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};
144                 last if --$max <= 0;
145         }
146         return @data;