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 (use_pagespec($params{page}, $params{pages}, deptype => $deptype)) {
40 if (exists $params{show} &&
41 exists $pagestate{$page} &&
42 exists $pagestate{$page}{meta}{$params{show}}) {
43 $mapitems{$page}=$pagestate{$page}{meta}{$params{show}};
48 # Check for a common prefix.
49 if (! defined $common_prefix) {
52 elsif (length $common_prefix &&
53 $page !~ /^\Q$common_prefix\E(\/|$)/) {
54 my @a=split(/\//, $page);
55 my @b=split(/\//, $common_prefix);
57 while (@a && @b && $a[0] eq $b[0]) {
58 if (length $common_prefix) {
61 $common_prefix.=shift(@a);
67 # Common prefix should not be a page in the map.
68 while (defined $common_prefix && length $common_prefix &&
69 exists $mapitems{$common_prefix}) {
70 $common_prefix=IkiWiki::dirname($common_prefix);
78 my $map = "<div class='map'>\n";
80 if (! keys %mapitems) {
81 # return empty div for empty map
89 foreach my $item (sort keys %mapitems) {
90 my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
91 $item=~s/^\Q$common_prefix\E\///
92 if defined $common_prefix && length $common_prefix;
93 my $depth = ($item =~ tr/\//\//) + 1;
94 my $baseitem=IkiWiki::dirname($item);
95 while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
96 $parent=IkiWiki::dirname($parent);
97 last if length $addparent && $baseitem =~ /^\Q$addparent\E(\/|$)/;
105 while ($depth < $indent) {
112 my @bits=split("/", $item);
114 $p.="/".shift(@bits) for 1..$indent;
115 while ($depth > $indent) {
120 if ($depth > $indent) {
121 $p.="/".shift(@bits);
125 .htmllink($params{page}, $params{destpage},
126 "/".$common_prefix.$p, class => "mapparent",
135 $map .= "</li>\n" if $openli;
137 .htmllink($params{page}, $params{destpage},
138 "/".$common_prefix."/".$item,
140 class => "mapitem", noimageinline => 1)
145 while ($indent > 0) {
147 $map .= "</li>\n</ul>\n";