summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/postsparkline.pm
blob: 6fc96f8b343cbca8eeeb893c41e2569a962d915c (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. error 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. error 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. error $@;
  46. }
  47. if (! @data) {
  48. # generate an empty graph
  49. push @data, 0 foreach 1..($params{max} / 2);
  50. }
  51. my $color=exists $params{color} ? "($params{color})" : "";
  52. delete $params{pages};
  53. delete $params{formula};
  54. delete $params{ftime};
  55. delete $params{color};
  56. return IkiWiki::Plugin::sparkline::preprocess(%params,
  57. map { $_.$color => "" } reverse @data);
  58. } # }}}
  59. sub perfoo ($@) {
  60. my $sub=shift;
  61. my $params=shift;
  62. my $max=$params->{max};
  63. my ($first, $prev, $cur);
  64. my $count=0;
  65. my @data;
  66. foreach (@_) {
  67. $cur=$sub->($params->{timehash}->{$_});
  68. if (defined $prev) {
  69. if ($prev != $cur) {
  70. push @data, "$prev,$count";
  71. $count=0;
  72. last if --$max <= 0;
  73. for ($cur+1 .. $prev-1) {
  74. push @data, "$_,0";
  75. last if --$max == 0;
  76. }
  77. }
  78. }
  79. else {
  80. $first=$cur;
  81. }
  82. $count++;
  83. $prev=$cur;
  84. }
  85. return @data;
  86. }
  87. package IkiWiki::Plugin::postsparkline::formula;
  88. sub peryear (@) {
  89. return IkiWiki::Plugin::postsparkline::perfoo(sub {
  90. return (localtime $_[0])[5];
  91. }, @_);
  92. }
  93. sub permonth (@) {
  94. return IkiWiki::Plugin::postsparkline::perfoo(sub {
  95. my ($month, $year)=(localtime $_[0])[4,5];
  96. return $year*12+$month;
  97. }, @_);
  98. }
  99. sub perday (@) {
  100. return IkiWiki::Plugin::postsparkline::perfoo(sub {
  101. my ($year, $yday)=(localtime $_[0])[5,7];
  102. return $year*365+$yday;
  103. }, @_);
  104. }
  105. sub interval ($@) {
  106. my $params=shift;
  107. my $max=$params->{max};
  108. my @data;
  109. for (my $i=1; $i < @_; $i++) {
  110. push @data, $params->{timehash}->{$_[$i-1]} - $params->{timehash}->{$_[$i]};
  111. last if --$max <= 0;
  112. }
  113. return @data;
  114. }
  115. 1