3 # Produce a hierarchical map of links.
5 # by Alessandro Dotti Contra <alessandro@hyboria.org>
8 package IkiWiki::Plugin::map;
15 hook(type => "preprocess", id => "map", call => \&preprocess);
18 sub preprocess (@) { #{{{
20 $params{pages}="*" unless defined $params{pages};
24 # Get all the items to map.
26 foreach my $page (keys %pagesources) {
27 if (pagespec_match($page, $params{pages}, location => $params{page})) {
30 # Check for a common prefix.
31 if (! defined $common_prefix) {
34 elsif (length $common_prefix &&
35 $page !~ /^\Q$common_prefix\E(\/|$)/) {
36 my @a=split(/\//, $page);
37 my @b=split(/\//, $common_prefix);
39 while (@a && @b && $a[0] eq $b[0]) {
40 if (length $common_prefix) {
43 $common_prefix.=shift(@a);
50 # Common prefix should not be a page in the map.
51 while (defined $common_prefix && length $common_prefix &&
52 exists $mapitems{$common_prefix}) {
53 $common_prefix=IkiWiki::dirname($common_prefix);
56 # Needs to update whenever a page is added or removed, so
57 # register a dependency.
58 add_depends($params{page}, $params{pages});
59 # Explicitly add all currently shown pages, to detect when pages
61 add_depends($params{page}, join(" or ", keys %mapitems));
68 my $map = "<div class='map'>\n<ul>\n";
69 foreach my $item (sort keys %mapitems) {
70 $item=~s/^\Q$common_prefix\E\///
71 if defined $common_prefix && length $common_prefix;
72 my $depth = ($item =~ tr/\//\//) + 1;
73 my $baseitem=IkiWiki::dirname($item);
74 while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
75 $parent=IkiWiki::dirname($parent);
76 last if !$dummy && length $parent && $baseitem =~ /^\Q$parent\E(\/|$)/;
84 while ($depth < $indent) {
91 my @bits=split("/", $item);
93 $p.="/".shift(@bits) for 1..$indent;
94 while ($depth > $indent) {
99 if ($depth > $indent) {
101 $p.="/".shift(@bits);
103 .htmllink($params{page}, $params{destpage},
104 $p, class => "mapparent",
113 $map .= "</li>\n" if $openli;
115 .htmllink($params{page}, $params{destpage},
116 "/".$common_prefix."/".$item,
117 class => "mapitem", noimageinline => 1)
122 while ($indent > 0) {
124 $map .= "</li>\n</ul>\n";