* map: Render empty nodes on the way to subpages whose parent pages
[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 $parent="";
39         my $indent=0;
40         my $openli=0;
41         my $map = "<div class='map'>\n";
42         foreach my $item (sort @mapitems) {
43                 my $depth = ($item =~ tr/\//\//);
44                 my $baseitem=IkiWiki::dirname($item);
45                 while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E/) {
46                         $parent=IkiWiki::dirname($parent);
47                         $indent--;
48                         $map.="</li></ul>\n";
49                 }
50                 while ($depth < $indent) {
51                         $indent--;
52                         $map.="</li></ul>\n";
53                 }
54                 while ($depth > $indent) {
55                         $indent++;
56                         $map.="<ul>\n";
57                         if ($depth > $indent) {
58                                 $map .= "<li>\n";
59                                 $openli=1;
60                         }
61                         else {
62                                 $openli=0;
63                         }
64                 }
65                 $map .= "</li>\n" if $openli;
66                 $map .= "<li>"
67                         .htmllink($params{page}, $params{destpage}, $item)
68                         ."\n";
69                 $openli=1;
70                 $parent=$item;
71         }
72         while ($indent > 0) {
73                 $indent--;
74                 $map.="</li></ul>\n";
75         }
76         $map .= "</div>\n";
77         return $map;
78 } # }}}
79
80 1