3 # Produce a hierarchical map of links.
5 # by Alessandro Dotti Contra <alessandro@hyboria.org>
8 package IkiWiki::Plugin::map;
15 hook(type => "getsetup", id => "map", call => \&getsetup);
16 hook(type => "preprocess", id => "map", call => \&preprocess);
29 $params{pages}="*" unless defined $params{pages};
33 # Get all the items to map.
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}};
45 # Check for a common prefix.
46 if (! defined $common_prefix) {
49 elsif (length $common_prefix &&
50 $page !~ /^\Q$common_prefix\E(\/|$)/) {
51 my @a=split(/\//, $page);
52 my @b=split(/\//, $common_prefix);
54 while (@a && @b && $a[0] eq $b[0]) {
55 if (length $common_prefix) {
58 $common_prefix.=shift(@a);
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);
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
73 add_depends($params{page}, $params{pages});
74 # Explicitly add all currently shown pages, to detect when pages
76 foreach my $item (keys %mapitems) {
77 add_depends($params{page}, $item);
85 my $map = "<div class='map'>\n";
87 # Return empty div if %mapitems is empty
88 if (!scalar(keys %mapitems)) {
92 else { # continue populating $map
96 foreach my $item (sort keys %mapitems) {
97 my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
98 $item=~s/^\Q$common_prefix\E\///
99 if defined $common_prefix && length $common_prefix;
100 my $depth = ($item =~ tr/\//\//) + 1;
101 my $baseitem=IkiWiki::dirname($item);
102 while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
103 $parent=IkiWiki::dirname($parent);
104 last if length $addparent && $baseitem =~ /^\Q$addparent\E(\/|$)/;
112 while ($depth < $indent) {
119 my @bits=split("/", $item);
121 $p.="/".shift(@bits) for 1..$indent;
122 while ($depth > $indent) {
127 if ($depth > $indent) {
128 $p.="/".shift(@bits);
132 .htmllink($params{page}, $params{destpage},
133 "/".$common_prefix.$p, class => "mapparent",
142 $map .= "</li>\n" if $openli;
144 .htmllink($params{page}, $params{destpage},
145 "/".$common_prefix."/".$item,
147 class => "mapitem", noimageinline => 1)
152 while ($indent > 0) {
154 $map .= "</li>\n</ul>\n";