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