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};
31 # Needs to update whenever a page is added or removed (or in some
32 # cases, when its content changes, if show= is specified).
33 my $deptype=deptype(exists $params{show} ? "content" : "presence");
37 # Get all the items to map.
39 foreach my $page (pagespec_match_list($params{page}, $params{pages},
40 deptype => $deptype)) {
41 if (exists $params{show} &&
42 exists $pagestate{$page} &&
43 exists $pagestate{$page}{meta}{$params{show}}) {
44 $mapitems{$page}=$pagestate{$page}{meta}{$params{show}};
49 # Check for a common prefix.
50 if (! defined $common_prefix) {
53 elsif (length $common_prefix &&
54 $page !~ /^\Q$common_prefix\E(\/|$)/) {
55 my @a=split(/\//, $page);
56 my @b=split(/\//, $common_prefix);
58 while (@a && @b && $a[0] eq $b[0]) {
59 if (length $common_prefix) {
62 $common_prefix.=shift(@a);
68 # Common prefix should not be a page in the map.
69 while (defined $common_prefix && length $common_prefix &&
70 exists $mapitems{$common_prefix}) {
71 $common_prefix=IkiWiki::dirname($common_prefix);
79 my $map = "<div class='map'>\n";
81 if (! keys %mapitems) {
82 # return empty div for empty map
90 foreach my $item (sort keys %mapitems) {
91 my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
92 $item=~s/^\Q$common_prefix\E\///
93 if defined $common_prefix && length $common_prefix;
94 my $depth = ($item =~ tr/\//\//) + 1;
95 my $baseitem=IkiWiki::dirname($item);
96 while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
97 $parent=IkiWiki::dirname($parent);
98 last if length $addparent && $baseitem =~ /^\Q$addparent\E(\/|$)/;
106 while ($depth < $indent) {
113 my @bits=split("/", $item);
115 $p.="/".shift(@bits) for 1..$indent;
116 while ($depth > $indent) {
121 if ($depth > $indent) {
122 $p.="/".shift(@bits);
126 .htmllink($params{page}, $params{destpage},
127 "/".$common_prefix.$p, class => "mapparent",
136 $map .= "</li>\n" if $openli;
138 .htmllink($params{page}, $params{destpage},
139 "/".$common_prefix."/".$item,
141 class => "mapitem", noimageinline => 1)
146 while ($indent > 0) {
148 $map .= "</li>\n</ul>\n";