]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/map.pm
change how dependency types are specified to add_depends
[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= is specified), so
72         # register a dependency.
73         add_depends($params{page}, $params{pages},
74                 deptype(exists $params{show} ? "content" : "presence");
75
76         # Create the map.
77         my $parent="";
78         my $indent=0;
79         my $openli=0;
80         my $addparent="";
81         my $map = "<div class='map'>\n";
82
83         # Return empty div if %mapitems is empty
84         if (!scalar(keys %mapitems)) {
85                 $map .= "</div>\n";
86                 return $map; 
87         } 
88         else { # continue populating $map
89                 $map .= "<ul>\n";
90         }
91
92         foreach my $item (sort keys %mapitems) {
93                 my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
94                 $item=~s/^\Q$common_prefix\E\///
95                         if defined $common_prefix && length $common_prefix;
96                 my $depth = ($item =~ tr/\//\//) + 1;
97                 my $baseitem=IkiWiki::dirname($item);
98                 while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
99                         $parent=IkiWiki::dirname($parent);
100                         last if length $addparent && $baseitem =~ /^\Q$addparent\E(\/|$)/;
101                         $addparent="";
102                         $indent--;
103                         $map .= "</li>\n";
104                         if ($indent > 0) {
105                                 $map .= "</ul>\n";
106                         }
107                 }
108                 while ($depth < $indent) {
109                         $indent--;
110                         $map .= "</li>\n";
111                         if ($indent > 0) {
112                                 $map .= "</ul>\n";
113                         }
114                 }
115                 my @bits=split("/", $item);
116                 my $p="";
117                 $p.="/".shift(@bits) for 1..$indent;
118                 while ($depth > $indent) {
119                         $indent++;
120                         if ($indent > 1) {
121                                 $map .= "<ul>\n";
122                         }
123                         if ($depth > $indent) {
124                                 $p.="/".shift(@bits);
125                                 $addparent=$p;
126                                 $addparent=~s/^\///;
127                                 $map .= "<li>"
128                                         .htmllink($params{page}, $params{destpage},
129                                                  "/".$common_prefix.$p, class => "mapparent",
130                                                  noimageinline => 1)
131                                         ."\n";
132                                 $openli=1;
133                         }
134                         else {
135                                 $openli=0;
136                         }
137                 }
138                 $map .= "</li>\n" if $openli;
139                 $map .= "<li>"
140                         .htmllink($params{page}, $params{destpage}, 
141                                 "/".$common_prefix."/".$item,
142                                 @linktext,
143                                 class => "mapitem", noimageinline => 1)
144                         ."\n";
145                 $openli=1;
146                 $parent=$item;
147         }
148         while ($indent > 0) {
149                 $indent--;
150                 $map .= "</li>\n</ul>\n";
151         }
152         $map .= "</div>\n";
153         return $map;
154 }
155
156 1