120451b5dbde60a26ae165e391cf56232cc30111
[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 3.00;
13
14 sub import {
15         hook(type => "getsetup", id => "map", call => \&getsetup);
16         hook(type => "preprocess", id => "map", call => \&preprocess);
17 }
18
19 sub getsetup () {
20         return
21                 plugin => {
22                         safe => 1,
23                         rebuild => undef,
24                 },
25 }
26
27 sub preprocess (@) {
28         my %params=@_;
29         $params{pages}="*" unless defined $params{pages};
30         
31         my $common_prefix;
32
33         # Get all the items to map.
34         my %mapitems;
35         foreach my $page (pagespec_match_list([keys %pagesources],
36                                 $params{pages}, location => $params{page})) {
37                 if (exists $params{show} && 
38                     exists $pagestate{$page} &&
39                     exists $pagestate{$page}{meta}{$params{show}}) {
40                         $mapitems{$page}=$pagestate{$page}{meta}{$params{show}};
41                 }
42                 else {
43                         $mapitems{$page}='';
44                 }
45                 # Check for a common prefix.
46                 if (! defined $common_prefix) {
47                         $common_prefix=$page;
48                 }
49                 elsif (length $common_prefix &&
50                        $page !~ /^\Q$common_prefix\E(\/|$)/) {
51                         my @a=split(/\//, $page);
52                         my @b=split(/\//, $common_prefix);
53                         $common_prefix="";
54                         while (@a && @b && $a[0] eq $b[0]) {
55                                 if (length $common_prefix) {
56                                         $common_prefix.="/";
57                                 }
58                                 $common_prefix.=shift(@a);
59                                 shift @b;
60                         }
61                 }
62         }
63         
64         # Common prefix should not be a page in the map.
65         while (defined $common_prefix && length $common_prefix &&
66                exists $mapitems{$common_prefix}) {
67                 $common_prefix=IkiWiki::dirname($common_prefix);
68         }
69
70         # Needs to update whenever a page is added or removed (or in some
71         # cases, when its content changes, if show=title), so register a
72         # dependency.
73         add_depends($params{page}, $params{pages});
74         # Explicitly add all currently shown pages, to detect when pages
75         # are removed.
76         add_depends($params{page}, join(" or ", keys %mapitems));
77
78         # Create the map.
79         my $parent="";
80         my $indent=0;
81         my $openli=0;
82         my $addparent="";
83         my $map = "<div class='map'>\n<ul>\n";
84         foreach my $item (sort keys %mapitems) {
85                 my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
86                 $item=~s/^\Q$common_prefix\E\///
87                         if defined $common_prefix && length $common_prefix;
88                 my $depth = ($item =~ tr/\//\//) + 1;
89                 my $baseitem=IkiWiki::dirname($item);
90                 while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
91                         $parent=IkiWiki::dirname($parent);
92                         last if length $addparent && $baseitem =~ /^\Q$addparent\E(\/|$)/;
93                         $addparent="";
94                         $indent--;
95                         $map .= "</li>\n";
96                         if ($indent > 0) {
97                                 $map .= "</ul>\n";
98                         }
99                 }
100                 while ($depth < $indent) {
101                         $indent--;
102                         $map .= "</li>\n";
103                         if ($indent > 0) {
104                                 $map .= "</ul>\n";
105                         }
106                 }
107                 my @bits=split("/", $item);
108                 my $p="";
109                 $p.="/".shift(@bits) for 1..$indent;
110                 while ($depth > $indent) {
111                         $indent++;
112                         if ($indent > 1) {
113                                 $map .= "<ul>\n";
114                         }
115                         if ($depth > $indent) {
116                                 $p.="/".shift(@bits);
117                                 $addparent=$p;
118                                 $addparent=~s/^\///;
119                                 $map .= "<li>"
120                                         .htmllink($params{page}, $params{destpage},
121                                                  "/".$common_prefix.$p, class => "mapparent",
122                                                  noimageinline => 1)
123                                         ."\n";
124                                 $openli=1;
125                         }
126                         else {
127                                 $openli=0;
128                         }
129                 }
130                 $map .= "</li>\n" if $openli;
131                 $map .= "<li>"
132                         .htmllink($params{page}, $params{destpage}, 
133                                 "/".$common_prefix."/".$item,
134                                 @linktext,
135                                 class => "mapitem", noimageinline => 1)
136                         ."\n";
137                 $openli=1;
138                 $parent=$item;
139         }
140         while ($indent > 0) {
141                 $indent--;
142                 $map .= "</li>\n</ul>\n";
143         }
144         $map .= "</div>\n";
145         return $map;
146 }
147
148 1