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);
19 sub getsetup () { #{{{
27 sub preprocess (@) { #{{{
29 $params{pages}="*" unless defined $params{pages};
33 # Get all the items to map.
35 foreach my $page (keys %pagesources) {
36 if (pagespec_match($page, $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);
65 # Common prefix should not be a page in the map.
66 while (defined $common_prefix && length $common_prefix &&
67 exists $mapitems{$common_prefix}) {
68 $common_prefix=IkiWiki::dirname($common_prefix);
71 # Needs to update whenever a page is added or removed (or in some
72 # cases, when its content changes, if show=title), so register a
74 add_depends($params{page}, $params{pages});
75 # Explicitly add all currently shown pages, to detect when pages
77 add_depends($params{page}, join(" or ", keys %mapitems));
84 my $map = "<div class='map'>\n<ul>\n";
85 foreach my $item (sort keys %mapitems) {
86 my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
87 $item=~s/^\Q$common_prefix\E\///
88 if defined $common_prefix && length $common_prefix;
89 my $depth = ($item =~ tr/\//\//) + 1;
90 my $baseitem=IkiWiki::dirname($item);
91 print STDERR "!! parent: $parent baseitem: $baseitem\n";
92 while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
93 $parent=IkiWiki::dirname($parent);
94 last if !$dummy && length $parent && $baseitem =~ /^\Q$parent\E(\/|$)/;
102 while ($depth < $indent) {
109 my @bits=split("/", $item);
111 $p.="/".shift(@bits) for 1..$indent;
112 while ($depth > $indent) {
117 if ($depth > $indent) {
118 $p.="/".shift(@bits);
121 .htmllink($params{page}, $params{destpage},
122 "/".$common_prefix.$p, class => "mapparent",
132 $map .= "</li>\n" if $openli;
134 .htmllink($params{page}, $params{destpage},
135 "/".$common_prefix."/".$item,
137 class => "mapitem", noimageinline => 1)
142 while ($indent > 0) {
144 $map .= "</li>\n</ul>\n";