]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/postsparkline.pm
if an inlined page has the same copyright or license as the page it's
[ikiwiki.git] / IkiWiki / Plugin / postsparkline.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::postsparkline;
3
4 use warnings;
5 use strict;
6 use IkiWiki 2.00;
7
8 sub import { #{{{
9         IkiWiki::loadplugin('sparkline');
10         hook(type => "preprocess", id => "postsparkline", call => \&preprocess);
11 } # }}}
12
13 sub preprocess (@) { #{{{
14         my %params=@_;
15
16         if (! exists $params{max}) {
17                 $params{max}=100;
18         }
19
20         if (! exists $params{pages}) {
21                 return "";
22         }
23
24         if (! exists $params{time} || $params{time} ne 'mtime') {
25                 $params{timehash} = \%IkiWiki::pagectime;
26         }
27         else {
28                 $params{timehash} = \%IkiWiki::pagemtime;
29         }
30
31         if (! exists $params{formula}) {
32                 return "[[postsparkline ".gettext("missing formula")."]]";
33         }
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")."]]";
40         }
41
42         add_depends($params{page}, $params{pages});
43
44         my @list;
45         foreach my $page (keys %pagesources) {
46                 next if $page eq $params{page};
47                 if (pagespec_match($page, $params{pages}, location => $params{page})) {
48                         push @list, $page;
49                 }
50         }
51         
52         @list = sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} } @list;
53
54         my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
55         if ($@) {
56                 return "[[postsparkline error $@]]";
57         }
58
59         my $color=exists $params{color} ? "($params{color})" : "";
60
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);
67 } # }}}
68
69 sub perfoo ($@) {
70         my $sub=shift;
71         my $params=shift;
72         
73         my $max=$params->{max};
74         my ($first, $prev, $cur);
75         my $count=0;
76         my @data;
77         foreach (@_) {
78                 $cur=$sub->($params->{timehash}->{$_});
79                 if (defined $prev) {
80                         if ($prev != $cur) {
81                                 push @data, "$prev,$count";
82                                 $count=0;
83                                 last if --$max <= 0;
84
85                                 for ($cur+1 .. $prev-1) {
86                                         push @data, "$_,0";
87                                         last if --$max == 0;
88                                 }
89                         }
90                 }
91                 else {
92                         $first=$cur;
93                 }
94                 $count++;
95                 $prev=$cur;
96         }
97
98         return @data;
99 }
100
101 package IkiWiki::Plugin::postsparkline::formula;
102
103 sub peryear (@) {
104         return IkiWiki::Plugin::postsparkline::perfoo(sub {
105                 return (localtime $_[0])[5];
106         }, @_);
107 }
108
109 sub permonth (@) {
110         return IkiWiki::Plugin::postsparkline::perfoo(sub {
111                 my ($month, $year)=(localtime $_[0])[4,5];
112                 return $year*12+$month;
113         }, @_);
114 }
115
116 sub perday (@) {
117         return IkiWiki::Plugin::postsparkline::perfoo(sub {
118                 my ($year, $yday)=(localtime $_[0])[5,7];
119                 return $year*365+$yday;
120         }, @_);
121 }
122
123 sub interval ($@) {
124         my $params=shift;
125
126         my $max=$params->{max};
127         my @data;
128         for (my $i=1; $i < @_; $i++) {
129                 push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};
130                 last if --$max <= 0;
131         }
132         return @data;
133 }
134
135 1