summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pagestats.pm
blob: 34fd11715d24e03fa7012023a97c1aef7d86d13d (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 (%IkiWiki::links) {
  31. if (IkiWiki::globlist_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".join("\n", map { "<tr><td>$_</td><td>".$counts{$_}."</td></tr>" }
  39. sort { $counts{$b} <=> $counts{$a} } keys %counts)."\n</table>\n" ;
  40. } else {
  41. # In case of misspelling, default to a page cloud
  42. my $res = "<div class='pagecloud'>\n";
  43. foreach my $page (sort keys %counts) {
  44. my $class = $classes[$counts{$page} * scalar(@classes) / ($max + 1)];
  45. my $link = IkiWiki::abs2rel(IkiWiki::htmlpage($page), IkiWiki::dirname($params{page}));
  46. $res .= sprintf("<span class='%s'><a href='%s'>%s</a></span>\n",
  47. $class, $link, $page);
  48. }
  49. $res .= "</div>\n";
  50. return $res;
  51. }
  52. } # }}}
  53. 1