Added my site, even though it's in an early state
[ikiwiki.git] / IkiWiki / Plugin / postsparkline.pm
blob2fae9c5fee90ab266aa0be0d0b2fb4a5267afbb3
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::postsparkline;
4 use warnings;
5 use strict;
6 use IkiWiki 3.00;
8 sub import {
9 IkiWiki::loadplugin('sparkline');
10 hook(type => "getsetup", id => "postsparkline", call => \&getsetup);
11 hook(type => "preprocess", id => "postsparkline", call => \&preprocess);
14 sub getsetup () {
15 return
16 plugin => {
17 safe => 1,
18 rebuild => undef,
19 section => "widget",
23 sub preprocess (@) {
24 my %params=@_;
26 if (! exists $params{max}) {
27 $params{max}=100;
30 if (! exists $params{pages}) {
31 return "";
34 my $deptype;
35 if (! exists $params{time} || $params{time} ne 'mtime') {
36 $params{timehash} = \%IkiWiki::pagectime;
37 # need to update when pages are added or removed
38 $deptype = deptype("presence");
40 else {
41 $params{timehash} = \%IkiWiki::pagemtime;
42 # need to update when pages are changed
43 $deptype = deptype("content");
46 if (! exists $params{formula}) {
47 error gettext("missing formula")
49 my $formula=$params{formula};
50 $formula=~s/[^a-zA-Z0-9]*//g;
51 $formula=IkiWiki::possibly_foolish_untaint($formula);
52 if (! length $formula ||
53 ! IkiWiki::Plugin::postsparkline::formula->can($formula)) {
54 error gettext("unknown formula");
57 my @list=sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} }
58 pagespec_match_list($params{page}, $params{pages},
59 deptype => $deptype,
60 filter => sub { $_[0] eq $params{page} },
63 my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
64 if ($@) {
65 error $@;
68 if (! @data) {
69 # generate an empty graph
70 push @data, 0 foreach 1..($params{max} / 2);
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);
83 sub perfoo ($@) {
84 my $sub=shift;
85 my $params=shift;
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;
105 else {
106 $first=$cur;
108 $count++;
109 $prev=$cur;
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;
146 return @data;