summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/map.pm
blob: 2ff840ff7b0121b55f7ece7654c579f9fdc7403a (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 $parent="";
  33. my $indent=0;
  34. my $openli=0;
  35. my $map = "<div class='map'>\n";
  36. foreach my $item (sort @mapitems) {
  37. my $depth = ($item =~ tr/\//\//);
  38. my $baseitem=IkiWiki::dirname($item);
  39. while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E/) {
  40. $parent=IkiWiki::dirname($parent);
  41. $indent--;
  42. $map.="</li></ul>\n";
  43. }
  44. while ($depth < $indent) {
  45. $indent--;
  46. $map.="</li></ul>\n";
  47. }
  48. while ($depth > $indent) {
  49. $indent++;
  50. $map.="<ul>\n";
  51. if ($depth > $indent) {
  52. $map .= "<li>\n";
  53. $openli=1;
  54. }
  55. else {
  56. $openli=0;
  57. }
  58. }
  59. $map .= "</li>\n" if $openli;
  60. $map .= "<li>"
  61. .htmllink($params{page}, $params{destpage}, $item)
  62. ."\n";
  63. $openli=1;
  64. $parent=$item;
  65. }
  66. while ($indent > 0) {
  67. $indent--;
  68. $map.="</li></ul>\n";
  69. }
  70. $map .= "</div>\n";
  71. return $map;
  72. } # }}}
  73. 1