]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/map.pm
* Add support for mercurial, contributed by Emanuele Aina.
[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.2
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 $openli=0;
38         my $map = "<div class='map'>\n";
39         $map .= "<ul>\n";
40         foreach my $item (sort @mapitems) {
41                 my $depth = ($item =~ tr/\//\//);
42                 while ($depth < $indent) {
43                         $indent--;
44                         $map.="</li></ul>\n";
45                 }
46                 while ($depth > $indent) {
47                         $indent++;
48                         $map.="<ul>\n";
49                         $openli=0;
50                 }
51                 $map .= "</li>\n" if $openli;
52                 $map .= "<li>"
53                         .IkiWiki::htmllink($params{page}, $params{destpage}, $item) ."\n";
54                 $openli=1;
55         }
56         while ($indent > 0) {
57                 $indent--;
58                 $map.="</li></ul>\n";
59         }
60         $map .= "</li></ul>\n";
61         $map .= "</div>\n";
62         return $map;
63 } # }}}
64
65 1