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