* Add a map plugin contributed by Alessandro Dotti Contra.
[ikiwiki.git] / IkiWiki / Plugin / map.pm
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
10 use warnings;
11 use strict;
12 use IkiWiki;
13
14 sub import { #{{{
15         IkiWiki::hook(type => "preprocess", id => "map",
16                 call => \&preprocess);
17 } # }}}
18
19 sub preprocess (@) { #{{{
20         my %params=@_;
21         $params{pages}="*" unless defined $params{pages};
22         
23         # Needs to update whenever a page is added or removed, so
24         # register a dependency.
25         IkiWiki::add_depends($params{page}, $params{pages});
26         
27         # Get all the items to map.
28         my @mapitems = ();
29         foreach my $page (keys %IkiWiki::links) {
30                 if (IkiWiki::pagespec_match($page, $params{pages})) {
31                         push @mapitems, $page;
32                 }
33         }
34
35         # Create the map.
36         my $indent=0;
37         my $map = "<div class='map'>\n";
38         foreach my $item (sort @mapitems) {
39                 my $depth = ($item =~ tr/\//\//) + 1;
40                 next if exists $params{maxdepth} && $depth > $params{maxdepth};
41                 while ($depth < $indent) {
42                         $indent--;
43                         $map.="</ul>\n";
44                 }
45                 while ($depth > $indent) {
46                         $indent++;
47                         $map.="<ul>\n";
48                 }
49                 $map .= "<li>"
50                         .IkiWiki::htmllink($params{page}, $params{destpage}, $item)
51                         ."</li>\n";
52         }
53         while ($indent > 0) {
54                 $indent--;
55                 $map.="</ul>\n";
56         }
57         $map .= "</div>\n";
58         return $map;
59 } # }}}
60
61 1