summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/map.pm
blob: f2e8722c8e9770ff43654881da8674b751d6040f (plain)
  1. #!/usr/bin/perl
  2. #
  3. # Produce a hyerarchical map of links.
  4. #
  5. # By Alessandro Dotti Contra <alessandro@hyboria.org>
  6. #
  7. # Revision: 0.1
  8. package IkiWiki::Plugin::map;
  9. use warnings;
  10. use strict;
  11. use IkiWiki;
  12. sub import { #{{{
  13. IkiWiki::hook(type => "preprocess", id => "map",
  14. call => \&preprocess);
  15. } # }}}
  16. sub preprocess (@) { #{{{
  17. my %params=@_;
  18. $params{pages}="*" unless defined $params{pages};
  19. # Needs to update whenever a page is added or removed, so
  20. # register a dependency.
  21. IkiWiki::add_depends($params{page}, $params{pages});
  22. # Get all the items to map.
  23. my @mapitems = ();
  24. foreach my $page (keys %IkiWiki::links) {
  25. if (IkiWiki::pagespec_match($page, $params{pages})) {
  26. push @mapitems, $page;
  27. }
  28. }
  29. # Create the map.
  30. my $indent=0;
  31. my $map = "<div class='map'>\n";
  32. foreach my $item (sort @mapitems) {
  33. my $depth = ($item =~ tr/\//\//) + 1;
  34. while ($depth < $indent) {
  35. $indent--;
  36. $map.="</ul>\n";
  37. }
  38. while ($depth > $indent) {
  39. $indent++;
  40. $map.="<ul>\n";
  41. }
  42. $map .= "<li>"
  43. .IkiWiki::htmllink($params{page}, $params{destpage}, $item)
  44. ."</li>\n";
  45. }
  46. while ($indent > 0) {
  47. $indent--;
  48. $map.="</ul>\n";
  49. }
  50. $map .= "</div>\n";
  51. return $map;
  52. } # }}}
  53. 1