]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/map.pm
calendar, inline, map: don't pre-join dependencies
[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 3.00;
14 sub import {
15         hook(type => "getsetup", id => "map", call => \&getsetup);
16         hook(type => "preprocess", id => "map", call => \&preprocess);
17 }
19 sub getsetup () {
20         return
21                 plugin => {
22                         safe => 1,
23                         rebuild => undef,
24                 },
25 }
27 sub preprocess (@) {
28         my %params=@_;
29         $params{pages}="*" unless defined $params{pages};
30         
31         my $common_prefix;
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         }
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         foreach my $item (keys %mapitems) {
77                 add_depends($params{page}, $item);
78         }
80         # Create the map.
81         my $parent="";
82         my $indent=0;
83         my $openli=0;
84         my $addparent="";
85         my $map = "<div class='map'>\n<ul>\n";
86         foreach my $item (sort keys %mapitems) {
87                 my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
88                 $item=~s/^\Q$common_prefix\E\///
89                         if defined $common_prefix && length $common_prefix;
90                 my $depth = ($item =~ tr/\//\//) + 1;
91                 my $baseitem=IkiWiki::dirname($item);
92                 while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
93                         $parent=IkiWiki::dirname($parent);
94                         last if length $addparent && $baseitem =~ /^\Q$addparent\E(\/|$)/;
95                         $addparent="";
96                         $indent--;
97                         $map .= "</li>\n";
98                         if ($indent > 0) {
99                                 $map .= "</ul>\n";
100                         }
101                 }
102                 while ($depth < $indent) {
103                         $indent--;
104                         $map .= "</li>\n";
105                         if ($indent > 0) {
106                                 $map .= "</ul>\n";
107                         }
108                 }
109                 my @bits=split("/", $item);
110                 my $p="";
111                 $p.="/".shift(@bits) for 1..$indent;
112                 while ($depth > $indent) {
113                         $indent++;
114                         if ($indent > 1) {
115                                 $map .= "<ul>\n";
116                         }
117                         if ($depth > $indent) {
118                                 $p.="/".shift(@bits);
119                                 $addparent=$p;
120                                 $addparent=~s/^\///;
121                                 $map .= "<li>"
122                                         .htmllink($params{page}, $params{destpage},
123                                                  "/".$common_prefix.$p, class => "mapparent",
124                                                  noimageinline => 1)
125                                         ."\n";
126                                 $openli=1;
127                         }
128                         else {
129                                 $openli=0;
130                         }
131                 }
132                 $map .= "</li>\n" if $openli;
133                 $map .= "<li>"
134                         .htmllink($params{page}, $params{destpage}, 
135                                 "/".$common_prefix."/".$item,
136                                 @linktext,
137                                 class => "mapitem", noimageinline => 1)
138                         ."\n";
139                 $openli=1;
140                 $parent=$item;
141         }
142         while ($indent > 0) {
143                 $indent--;
144                 $map .= "</li>\n</ul>\n";
145         }
146         $map .= "</div>\n";
147         return $map;