summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/map.pm
blob: 07b63cb708d9759da62ea9e5e8df9701ebd0322a (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. hook(type => "preprocess", id => "map", call => \&preprocess);
  14. } # }}}
  15. sub preprocess (@) { #{{{
  16. my %params=@_;
  17. $params{pages}="*" unless defined $params{pages};
  18. # Needs to update whenever a page is added or removed, so
  19. # register a dependency.
  20. add_depends($params{page}, $params{pages});
  21. # Get all the items to map.
  22. my @mapitems = ();
  23. foreach my $page (keys %links) {
  24. if (pagespec_match($page, $params{pages}, $params{page})) {
  25. push @mapitems, $page;
  26. }
  27. }
  28. # Create the map.
  29. my $indent=0;
  30. my $openli=0;
  31. my $map = "<div class='map'>\n";
  32. $map .= "<ul>\n";
  33. foreach my $item (sort @mapitems) {
  34. my $depth = ($item =~ tr/\//\//);
  35. while ($depth < $indent) {
  36. $indent--;
  37. $map.="</li></ul>\n";
  38. }
  39. while ($depth > $indent) {
  40. $indent++;
  41. $map.="<ul>\n";
  42. $openli=0;
  43. }
  44. $map .= "</li>\n" if $openli;
  45. $map .= "<li>"
  46. .htmllink($params{page}, $params{destpage}, $item)
  47. ."\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