summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/map.pm
blob: 27ae0c6a37eb6dce47ec37460b63243bb176c2af (plain)
  1. #!/usr/bin/perl
  2. #
  3. # Produce a hierarchical map of links.
  4. #
  5. # by Alessandro Dotti Contra <alessandro@hyboria.org>
  6. #
  7. # Revision: 0.2
  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 $openli=0;
  32. my $map = "<div class='map'>\n";
  33. $map .= "<ul>\n";
  34. foreach my $item (sort @mapitems) {
  35. my $depth = ($item =~ tr/\//\//);
  36. while ($depth < $indent) {
  37. $indent--;
  38. $map.="</li></ul>\n";
  39. }
  40. while ($depth > $indent) {
  41. $indent++;
  42. $map.="<ul>\n";
  43. $openli=0;
  44. }
  45. $map .= "</li>\n" if $openli;
  46. $map .= "<li>"
  47. .IkiWiki::htmllink($params{page}, $params{destpage}, $item) ."\n";
  48. $openli=1;
  49. }
  50. while ($indent > 0) {
  51. $indent--;
  52. $map.="</li></ul>\n";
  53. }
  54. $map .= "</li></ul>\n";
  55. $map .= "</div>\n";
  56. return $map;
  57. } # }}}
  58. 1