summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pagestats.pm
blob: de559e2c5a63c762eb3277a85efffb950c912793 (plain)
  1. #!/usr/bin/perl
  2. #
  3. # Produce page statistics in various forms.
  4. #
  5. # Currently supported:
  6. # cloud: produces statistics in the form of a del.icio.us-style tag cloud
  7. # (default)
  8. # table: produces a table with the number of backlinks for each page
  9. #
  10. # By Enrico Zini.
  11. package IkiWiki::Plugin::pagestats;
  12. use warnings;
  13. use strict;
  14. use IkiWiki;
  15. # Names of the HTML classes to use for the tag cloud
  16. our @classes = ('smallestPC', 'smallPC', 'normalPC', 'bigPC', 'biggestPC' );
  17. sub import { #{{{
  18. IkiWiki::hook(type => "preprocess", id => "pagestats",
  19. call => \&preprocess);
  20. } # }}}
  21. sub preprocess (@) { #{{{
  22. my %params=@_;
  23. $params{pages}="*" unless defined $params{pages};
  24. my $style = ($params{style} or 'cloud');
  25. # Needs to update whenever a page is added or removed, so
  26. # register a dependency.
  27. IkiWiki::add_depends($params{page}, $params{pages});
  28. my %counts;
  29. my $max = 0;
  30. foreach my $page (keys %IkiWiki::links) {
  31. if (IkiWiki::pagespec_match($page, $params{pages})) {
  32. my @bl = IkiWiki::backlinks($page);
  33. $counts{$page} = scalar(@bl);
  34. $max = $counts{$page} if $counts{$page} > $max;
  35. }
  36. }
  37. if ($style eq 'table') {
  38. return "<table class='pageStats'>\n".
  39. join("\n", map {
  40. "<tr><td>".
  41. IkiWiki::htmllink($params{page}, $params{destpage}, $_, 1).
  42. "</td><td>".$counts{$_}."</td></tr>"
  43. }
  44. sort { $counts{$b} <=> $counts{$a} } keys %counts).
  45. "\n</table>\n" ;
  46. } else {
  47. # In case of misspelling, default to a page cloud
  48. my $res = "<div class='pagecloud'>\n";
  49. foreach my $page (sort keys %counts) {
  50. my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
  51. $res .= "<span class=\"$class\">".
  52. IkiWiki::htmllink($params{page}, $params{destpage}, $page).
  53. "</span>\n";
  54. }
  55. $res .= "</div>\n";
  56. return $res;
  57. }
  58. } # }}}
  59. 1