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);
30 $params{pages}="*" unless defined $params{pages};
32 # Needs to update whenever a page is added or removed (or in some
33 # cases, when its content changes, if show= is specified).
34 my $deptype=deptype(exists $params{show} ? "content" : "presence");
38 # Get all the items to map.
40 foreach my $page (pagespec_match_list($params{page}, $params{pages},
41 deptype => $deptype)) {
42 if (exists $params{show} &&
43 exists $pagestate{$page} &&
44 exists $pagestate{$page}{meta}{$params{show}}) {
45 $mapitems{$page}=$pagestate{$page}{meta}{$params{show}};
50 # Check for a common prefix.
51 if (! defined $common_prefix) {
54 elsif (length $common_prefix &&
55 $page !~ /^\Q$common_prefix\E(\/|$)/) {
56 my @a=split(/\//, $page);
57 my @b=split(/\//, $common_prefix);
59 while (@a && @b && $a[0] eq $b[0]) {
60 if (length $common_prefix) {
63 $common_prefix.=shift(@a);
69 # Common prefix should not be a page in the map.
70 while (defined $common_prefix && length $common_prefix &&
71 exists $mapitems{$common_prefix}) {
72 $common_prefix=IkiWiki::dirname($common_prefix);
75 # Set this to 1 or more spaces to pretty-print maps for debugging
83 my $map = "<div class='map'>\n";
85 if (! keys %mapitems) {
86 # return empty div for empty map
94 foreach my $item (sort keys %mapitems) {
95 my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
96 $item=~s/^\Q$common_prefix\E\///
97 if defined $common_prefix && length $common_prefix;
98 my $depth = ($item =~ tr/\//\//) + 1;
99 my $baseitem=IkiWiki::dirname($item);
100 while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
101 $parent=IkiWiki::dirname($parent);
102 last if length $addparent && $baseitem =~ /^\Q$addparent\E(\/|$)/;
104 $map .= ($spaces x $indent) . "</li>\n";
106 $map .= ($spaces x $indent) . "</ul><map:collapse>\n";
110 while ($depth < $indent) {
111 $map .= ($spaces x $indent) . "</li>\n";
113 $map .= ($spaces x $indent) . "</ul>\n";
117 my @bits=split("/", $item);
119 $p.="/".shift(@bits) for 1..$indent;
120 while ($depth > $indent) {
123 $map .= ($spaces x $indent) . "<ul><map:collapse>\n";
125 if ($depth > $indent) {
126 $p.="/".shift(@bits);
129 $map .= ($spaces x $indent) . "<li>\n";
130 $map .= ($spaces x $indent)
131 .htmllink($params{page}, $params{destpage},
132 "/".$common_prefix.$p, class => "mapparent",
141 $map .= ($spaces x $indent) . "</li>\n" if $openli;
142 $map .= ($spaces x $indent) . "<li>\n";
143 $map .= ($spaces x $indent)
144 .htmllink($params{page}, $params{destpage},
145 "/".$common_prefix."/".$item,
147 class => "mapitem", noimageinline => 1)
152 while ($indent > 0) {
153 $map .= ($spaces x $indent) . "</li>\n";
155 $map .= ($spaces x $indent) . "</ul>\n";
157 $map =~ s{\n *</ul><map:collapse>\n *<ul><map:collapse>\n}{\n}gs;
158 $map =~ s{<map:collapse>}{}g;