summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/postsparkline.pm
blob: e4fe9c154d0fa95420243c0f4bf8c65ab2187322 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::postsparkline;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. sub import { #{{{
  7. IkiWiki::loadplugin('sparkline');
  8. hook(type => "preprocess", id => "postsparkline", call => \&preprocess);
  9. } # }}}
  10. sub preprocess (@) { #{{{
  11. my %params=@_;
  12. if (! exists $params{max}) {
  13. $params{max}=100;
  14. }
  15. if (! exists $params{pages}) {
  16. return "";
  17. }
  18. if (! exists $params{formula}) {
  19. return "[[postsparkline ".gettext("missing formula")."]]";
  20. }
  21. my $formula=$params{formula};
  22. $formula=~s/[^a-zA-Z0-9]*//g;
  23. $formula=IkiWiki::possibly_foolish_untaint($formula);
  24. if (! length $formula ||
  25. ! IkiWiki::Plugin::postsparkline::formula->can($formula)) {
  26. return "[[postsparkline ".gettext("unknown formula")."]]";
  27. }
  28. add_depends($params{page}, $params{pages});
  29. my @list;
  30. foreach my $page (keys %pagesources) {
  31. next if $page eq $params{page};
  32. if (pagespec_match($page, $params{pages}, location => $params{page})) {
  33. push @list, $page;
  34. }
  35. }
  36. @list = sort { $IkiWiki::pagectime{$b} <=> $IkiWiki::pagectime{$a} } @list;
  37. delete $params{pages};
  38. delete $params{formula};
  39. my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
  40. if ($@) {
  41. return "[[postsparkline error $@]]";
  42. }
  43. return IkiWiki::Plugin::sparkline::preprocess(%params,
  44. map { $_ => "" } reverse @data);
  45. } # }}}
  46. sub perfoo ($@) {
  47. my $sub=shift;
  48. my $params=shift;
  49. my $max=$params->{max};
  50. my ($first, $prev, $cur);
  51. my $count=0;
  52. my @data;
  53. foreach (@_) {
  54. $cur=$sub->($IkiWiki::pagectime{$_});
  55. if (defined $prev) {
  56. if ($prev != $cur) {
  57. push @data, "$prev,$count";
  58. $count=0;
  59. last if --$max <= 0;
  60. for ($cur+1 .. $prev-1) {
  61. push @data, "$_,0";
  62. last if --$max == 0;
  63. }
  64. }
  65. }
  66. else {
  67. $first=$cur;
  68. }
  69. $count++;
  70. $prev=$cur;
  71. }
  72. return @data;
  73. }
  74. package IkiWiki::Plugin::postsparkline::formula;
  75. sub peryear (@) {
  76. return IkiWiki::Plugin::postsparkline::perfoo(sub {
  77. return (localtime $_[0])[5];
  78. }, @_);
  79. }
  80. sub permonth (@) {
  81. return IkiWiki::Plugin::postsparkline::perfoo(sub {
  82. my ($month, $year)=(localtime $_[0])[4,5];
  83. return $year*12+$month;
  84. }, @_);
  85. }
  86. sub perday (@) {
  87. return IkiWiki::Plugin::postsparkline::perfoo(sub {
  88. my ($year, $yday)=(localtime $_[0])[5,7];
  89. return $year*365+$yday;
  90. }, @_);
  91. }
  92. sub interval ($@) {
  93. my $params=shift;
  94. my $max=$params->{max};
  95. my @data;
  96. for (my $i=1; $i < @_; $i++) {
  97. push @data, $IkiWiki::pagectime{$_[$i-1]} - $IkiWiki::pagectime{$_[$i]};
  98. last if --$max <= 0;
  99. }
  100. return @data;
  101. }
  102. 1