]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/map.pm
Merge branch 'master' of ssh://git.kitenet.net/srv/git/ikiwiki.info
[git.ikiwiki.info.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;
10 use warnings;
11 use strict;
12 use IkiWiki 2.00;
14 sub import { #{{{
15         hook(type => "preprocess", id => "map", call => \&preprocess);
16 } # }}}
18 sub preprocess (@) { #{{{
19         my %params=@_;
20         $params{pages}="*" unless defined $params{pages};
21         
22         my $common_prefix;
24         # Get all the items to map.
25         my %mapitems;
26         foreach my $page (keys %pagesources) {
27                 if (pagespec_match($page, $params{pages}, location => $params{page})) {
28                         $mapitems{$page}=1;
30                         # Check for a common prefix.
31                         if (! defined $common_prefix) {
32                                 $common_prefix=$page;
33                         }
34                         elsif (length $common_prefix &&
35                                $page !~ /^\Q$common_prefix\E(\/|$)/) {
36                                 my @a=split(/\//, $page);
37                                 my @b=split(/\//, $common_prefix);
38                                 $common_prefix="";
39                                 while (@a && @b && $a[0] eq $b[0]) {
40                                         $common_prefix.=shift(@a);
41                                         shift @b;
42                                 }
43                         }
44                 }
45         }
46         
47         # Common prefix should not be a page in the map.
48         while (defined $common_prefix && length $common_prefix &&
49                exists $mapitems{$common_prefix}) {
50                 $common_prefix=IkiWiki::dirname($common_prefix);
51         }
53         # Needs to update whenever a page is added or removed, so
54         # register a dependency.
55         add_depends($params{page}, $params{pages});
56         # Explicitly add all currently shown pages, to detect when pages
57         # are removed.
58         add_depends($params{page}, join(" or ", keys %mapitems));
60         # Create the map.
61         my $parent="";
62         my $indent=0;
63         my $openli=0;
64         my $map = "<div class='map'>\n<ul>\n";
65         foreach my $item (sort keys %mapitems) {
66                 $item=~s/^\Q$common_prefix\E\///
67                         if defined $common_prefix && length $common_prefix;
68                 my $depth = ($item =~ tr/\//\//) + 1;
69                 my $baseitem=IkiWiki::dirname($item);
70                 while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
71                         $parent=IkiWiki::dirname($parent);
72                         $indent--;
73                         $map .= "</li>\n";
74                         if ($indent > 0) {
75                                 $map .= "</ul>\n";
76                         }
77                 }
78                 while ($depth < $indent) {
79                         $indent--;
80                         $map .= "</li>\n";
81                         if ($indent > 0) {
82                                 $map .= "</ul>\n";
83                         }
84                 }
85                 my @bits=split("/", $item);
86                 my $p="";
87                 $p.="/".shift(@bits) for 1..$indent;
88                 while ($depth > $indent) {
89                         $indent++;
90                         if ($indent > 1) {
91                                 $map .= "<ul>\n";
92                         }
93                         if ($depth > $indent) {
94                                 $p.="/".shift(@bits);
95                                 $map .= "<li>"
96                                         .htmllink($params{page}, $params{destpage}, $p, class => "mapparent")
97                                         ."\n";
98                                 $openli=1;
99                         }
100                         else {
101                                 $openli=0;
102                         }
103                 }
104                 $map .= "</li>\n" if $openli;
105                 $map .= "<li>"
106                         .htmllink($params{page}, $params{destpage}, 
107                                 "/".$common_prefix."/".$item, class => "mapitem")
108                         ."\n";
109                 $openli=1;
110                 $parent=$item;
111         }
112         while ($indent > 0) {
113                 $indent--;
114                 $map .= "</li>\n</ul>\n";
115         }
116         $map .= "</div>\n";
117         return $map;
118 } # }}}