summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/postsparkline.pm
blob: 6f7558bc44d2375afcfb3a6a38a7680fe3ffbb1b (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{time} || $params{time} ne 'mtime') {
  19. $params{timehash} = \%IkiWiki::pagectime;
  20. }
  21. else {
  22. $params{timehash} = \%IkiWiki::pagemtime;
  23. }
  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. }
  34. add_depends($params{page}, $params{pages});
  35. my @list;
  36. foreach my $page (keys %pagesources) {
  37. next if $page eq $params{page};
  38. if (pagespec_match($page, $params{pages}, location => $params{page})) {
  39. push @list, $page;
  40. }
  41. }
  42. @list = sort { $params{timehash}->{$b} <=> $params{timehash}->{$a} } @list;
  43. my @data=eval qq{IkiWiki::Plugin::postsparkline::formula::$formula(\\\%params, \@list)};
  44. if ($@) {
  45. return "[[postsparkline error $@]]";
  46. }
  47. delete $params{pages};
  48. delete $params{formula};
  49. delete $params{ftime};
  50. return IkiWiki::Plugin::sparkline::preprocess(%params,
  51. map { $_ => "" } reverse @data);
  52. } # }}}
  53. sub perfoo ($@) {
  54. my $sub=shift;
  55. my $params=shift;
  56. my $max=$params->{max};
  57. my ($first, $prev, $cur);
  58. my $count=0;
  59. my @data;
  60. foreach (@_) {
  61. $cur=$sub->($params->{timehash}->{$_});
  62. if (defined $prev) {
  63. if ($prev != $cur) {
  64. push @data, "$prev,$count";
  65. $count=0;
  66. last if --$max <= 0;
  67. for ($cur+1 .. $prev-1) {
  68. push @data, "$_,0";
  69. last if --$max == 0;
  70. }
  71. }
  72. }
  73. else {
  74. $first=$cur;
  75. }
  76. $count++;
  77. $prev=$cur;
  78. }
  79. return @data;
  80. }
  81. package IkiWiki::Plugin::postsparkline::formula;
  82. sub peryear (@) {
  83. return IkiWiki::Plugin::postsparkline::perfoo(sub {
  84. return (localtime $_[0])[5];
  85. }, @_);
  86. }
  87. sub permonth (@) {
  88. return IkiWiki::Plugin::postsparkline::perfoo(sub {
  89. my ($month, $year)=(localtime $_[0])[4,5];
  90. return $year*12+$month;
  91. }, @_);
  92. }
  93. sub perday (@) {
  94. return IkiWiki::Plugin::postsparkline::perfoo(sub {
  95. my ($year, $yday)=(localtime $_[0])[5,7];
  96. return $year*365+$yday;
  97. }, @_);
  98. }
  99. sub interval ($@) {
  100. my $params=shift;
  101. my $max=$params->{max};
  102. my @data;
  103. for (my $i=1; $i < @_; $i++) {
  104. push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};
  105. last if --$max <= 0;
  106. }
  107. return @data;
  108. }
  109. 1