clear sandbox/test commit
[ikiwiki.git] / IkiWiki / Plugin / map.pm
blob38f090ff782f8799edbe074b9778477809f9d045
1 #!/usr/bin/perl
3 # Produce a hierarchical map of links.
5 # by Alessandro Dotti Contra <alessandro@hyboria.org>
7 # Revision: 0.2
8 package IkiWiki::Plugin::map;
10 use warnings;
11 use strict;
12 use IkiWiki 3.00;
14 sub import {
15 hook(type => "getsetup", id => "map", call => \&getsetup);
16 hook(type => "preprocess", id => "map", call => \&preprocess);
19 sub getsetup () {
20 return
21 plugin => {
22 safe => 1,
23 rebuild => undef,
24 section => "widget",
28 sub preprocess (@) {
29 my %params=@_;
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");
36 my $common_prefix;
38 # Get all the items to map.
39 my %mapitems;
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}};
47 else {
48 $mapitems{$page}='';
50 # Check for a common prefix.
51 if (! defined $common_prefix) {
52 $common_prefix=$page;
54 elsif (length $common_prefix &&
55 $page !~ /^\Q$common_prefix\E(\/|$)/) {
56 my @a=split(/\//, $page);
57 my @b=split(/\//, $common_prefix);
58 $common_prefix="";
59 while (@a && @b && $a[0] eq $b[0]) {
60 if (length $common_prefix) {
61 $common_prefix.="/";
63 $common_prefix.=shift(@a);
64 shift @b;
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 # Create the map.
76 my $parent="";
77 my $indent=0;
78 my $openli=0;
79 my $addparent="";
80 my $map = "<div class='map'>\n";
82 if (! keys %mapitems) {
83 # return empty div for empty map
84 $map .= "</div>\n";
85 return $map;
87 else {
88 $map .= "<ul>\n";
91 foreach my $item (sort keys %mapitems) {
92 my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
93 $item=~s/^\Q$common_prefix\E\///
94 if defined $common_prefix && length $common_prefix;
95 my $depth = ($item =~ tr/\//\//) + 1;
96 my $baseitem=IkiWiki::dirname($item);
97 my $parentbase=IkiWiki::dirname($parent);
98 while (length $parentbase && length $baseitem && $baseitem !~ /^\Q$parentbase\E(\/|$)/) {
99 $parentbase=IkiWiki::dirname($parentbase);
100 last if length $addparent && $baseitem =~ /^\Q$addparent\E(\/|$)/;
101 $addparent="";
102 $indent--;
103 $map .= "</li>\n";
104 if ($indent > 0) {
105 $map .= "</ul>\n";
108 while ($depth < $indent) {
109 $indent--;
110 $map .= "</li>\n";
111 if ($indent > 0) {
112 $map .= "</ul>\n";
115 my @bits=split("/", $item);
116 my $p="";
117 $indent++ unless length $parent;
118 $p.="/".shift(@bits) for 1..$indent;
119 while ($depth > $indent) {
120 if (@bits && !(length $parent && "/$parent" eq $p)) {
121 $addparent=$p;
122 $addparent=~s/^\///;
123 $map .= "<li>"
124 .htmllink($params{page}, $params{destpage},
125 "/".$common_prefix.$p, class => "mapparent",
126 noimageinline => 1)
127 ."\n";
128 $openli=1;
130 else {
131 $openli=0;
133 $indent++;
134 $p.="/".shift(@bits) if @bits;
135 if ($indent > 1) {
136 $map .= "<ul>\n";
139 $map .= "</li>\n" if $openli;
140 $map .= "<li>"
141 .htmllink($params{page}, $params{destpage},
142 "/".$common_prefix."/".$item,
143 @linktext,
144 class => "mapitem", noimageinline => 1)
145 ."\n";
146 $openli=1;
147 $parent=$item;
149 while ($indent > 0) {
150 $indent--;
151 $map .= "</li>\n</ul>\n";
153 $map .= "</div>\n";
154 return $map;