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} &&
30 exists $pagestate{$page}{meta}{$params{show}}) {
31 $mapitems{$page}=$pagestate{$page}{meta}{$params{show}};
36 # Check for a common prefix.
37 if (! defined $common_prefix) {
40 elsif (length $common_prefix &&
41 $page !~ /^\Q$common_prefix\E(\/|$)/) {
42 my @a=split(/\//, $page);
43 my @b=split(/\//, $common_prefix);
45 while (@a && @b && $a[0] eq $b[0]) {
46 if (length $common_prefix) {
49 $common_prefix.=shift(@a);
56 # Common prefix should not be a page in the map.
57 while (defined $common_prefix && length $common_prefix &&
58 exists $mapitems{$common_prefix}) {
59 $common_prefix=IkiWiki::dirname($common_prefix);
62 # Needs to update whenever a page is added or removed (or in some
63 # cases, when its content changes, if show=title), so register a
65 add_depends($params{page}, $params{pages});
66 # Explicitly add all currently shown pages, to detect when pages
68 add_depends($params{page}, join(" or ", keys %mapitems));
75 my $map = "<div class='map'>\n<ul>\n";
76 foreach my $item (sort keys %mapitems) {
77 my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
78 $item=~s/^\Q$common_prefix\E\///
79 if defined $common_prefix && length $common_prefix;
80 my $depth = ($item =~ tr/\//\//) + 1;
81 my $baseitem=IkiWiki::dirname($item);
82 while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
83 $parent=IkiWiki::dirname($parent);
84 last if !$dummy && length $parent && $baseitem =~ /^\Q$parent\E(\/|$)/;
92 while ($depth < $indent) {
99 my @bits=split("/", $item);
101 $p.="/".shift(@bits) for 1..$indent;
102 while ($depth > $indent) {
107 if ($depth > $indent) {
109 $p.="/".shift(@bits);
111 .htmllink($params{page}, $params{destpage},
112 $p, class => "mapparent",
121 $map .= "</li>\n" if $openli;
123 .htmllink($params{page}, $params{destpage},
124 "/".$common_prefix."/".$item,
126 class => "mapitem", noimageinline => 1)
131 while ($indent > 0) {
133 $map .= "</li>\n</ul>\n";