summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/map.pm
blob: 1194f6ed2a11ab1583257314abadd50277e38cef (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 2.00;
  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. # Get all the items to map.
  19. my @mapitems = ();
  20. foreach my $page (keys %pagesources) {
  21. if (pagespec_match($page, $params{pages}, location => $params{page})) {
  22. push @mapitems, $page;
  23. }
  24. }
  25. # Needs to update whenever a page is added or removed, so
  26. # register a dependency.
  27. add_depends($params{page}, $params{pages});
  28. # Explicitly add all currently shown pages, to detect when pages
  29. # are removed.
  30. add_depends($params{page}, join(" or ", @mapitems));
  31. # Create the map.
  32. my $indent=0;
  33. my $openli=0;
  34. my $map = "<div class='map'>\n";
  35. $map .= "<ul>\n";
  36. foreach my $item (sort @mapitems) {
  37. my $depth = ($item =~ tr/\//\//);
  38. while ($depth < $indent) {
  39. $indent--;
  40. $map.="</li></ul>\n";
  41. }
  42. while ($depth > $indent) {
  43. $indent++;
  44. $map.="<ul>\n";
  45. $openli=0;
  46. }
  47. $map .= "</li>\n" if $openli;
  48. $map .= "<li>"
  49. .htmllink($params{page}, $params{destpage}, $item)
  50. ."\n";
  51. $openli=1;
  52. }
  53. while ($indent > 0) {
  54. $indent--;
  55. $map.="</li></ul>\n";
  56. }
  57. $map .= "</li></ul>\n";
  58. $map .= "</div>\n";
  59. return $map;
  60. } # }}}
  61. 1