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