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 $common_prefix.=shift(@a);
47 # Common prefix should not be a page in the map.
48 while (length $common_prefix && exists $mapitems{$common_prefix}) {
49 $common_prefix=IkiWiki::dirname($common_prefix);
52 # Needs to update whenever a page is added or removed, so
53 # register a dependency.
54 add_depends($params{page}, $params{pages});
55 # Explicitly add all currently shown pages, to detect when pages
57 add_depends($params{page}, join(" or ", keys %mapitems));
63 my $map = "<div class='map'>\n<ul>\n";
64 foreach my $item (sort keys %mapitems) {
65 $item=~s/^\Q$common_prefix\E\/// if length $common_prefix;
66 my $depth = ($item =~ tr/\//\//) + 1;
67 my $baseitem=IkiWiki::dirname($item);
68 while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
69 $parent=IkiWiki::dirname($parent);
76 while ($depth < $indent) {
83 my @bits=split("/", $item);
85 $p.="/".shift(@bits) for 1..$indent;
86 while ($depth > $indent) {
91 if ($depth > $indent) {
94 .htmllink($params{page}, $params{destpage}, $p, class => "mapparent")
102 $map .= "</li>\n" if $openli;
104 .htmllink($params{page}, $params{destpage},
105 "/".$common_prefix."/".$item, class => "mapitem")
110 while ($indent > 0) {
112 $map .= "</li>\n</ul>\n";