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})) {
28 if (exists $params{show} &&
29 exists $pagestate{$page}{meta}{$params{show}}) {
30 $mapitems{$page}=$pagestate{$page}{meta}{$params{show}};
33 $mapitems{$page}=$page;
35 # Check for a common prefix.
36 if (! defined $common_prefix) {
39 elsif (length $common_prefix &&
40 $page !~ /^\Q$common_prefix\E(\/|$)/) {
41 my @a=split(/\//, $page);
42 my @b=split(/\//, $common_prefix);
44 while (@a && @b && $a[0] eq $b[0]) {
45 if (length $common_prefix) {
48 $common_prefix.=shift(@a);
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);
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
64 add_depends($params{page}, $params{pages});
65 # Explicitly add all currently shown pages, to detect when pages
67 add_depends($params{page}, join(" or ", keys %mapitems));
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(\/|$)/;
90 while ($depth < $indent) {
97 my @bits=split("/", $item);
99 $p.="/".shift(@bits) for 1..$indent;
100 while ($depth > $indent) {
105 if ($depth > $indent) {
107 $p.="/".shift(@bits);
109 .htmllink($params{page}, $params{destpage},
110 $p, class => "mapparent",
119 $map .= "</li>\n" if $openli;
121 .htmllink($params{page}, $params{destpage},
122 "/".$common_prefix."/".$item,
123 linktext => $mapitems{$item},
124 class => "mapitem", noimageinline => 1)
129 while ($indent > 0) {
131 $map .= "</li>\n</ul>\n";