2006-08-18 18:18:45 +02:00
|
|
|
#!/usr/bin/perl
|
|
|
|
#
|
2006-09-07 05:45:28 +02:00
|
|
|
# Produce a hierarchical map of links.
|
2006-08-18 18:18:45 +02:00
|
|
|
#
|
2006-08-19 07:05:02 +02:00
|
|
|
# by Alessandro Dotti Contra <alessandro@hyboria.org>
|
2006-08-18 18:18:45 +02:00
|
|
|
#
|
2006-08-19 00:22:47 +02:00
|
|
|
# Revision: 0.2
|
2006-08-18 18:18:45 +02:00
|
|
|
package IkiWiki::Plugin::map;
|
|
|
|
|
|
|
|
use warnings;
|
|
|
|
use strict;
|
2007-04-27 04:55:52 +02:00
|
|
|
use IkiWiki 2.00;
|
2006-08-18 18:18:45 +02:00
|
|
|
|
|
|
|
sub import { #{{{
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "preprocess", id => "map", call => \&preprocess);
|
2006-08-18 18:18:45 +02:00
|
|
|
} # }}}
|
|
|
|
|
|
|
|
sub preprocess (@) { #{{{
|
|
|
|
my %params=@_;
|
|
|
|
$params{pages}="*" unless defined $params{pages};
|
|
|
|
|
2007-09-22 18:32:24 +02:00
|
|
|
my $common_prefix;
|
|
|
|
|
2006-08-18 18:18:45 +02:00
|
|
|
# Get all the items to map.
|
2007-09-22 18:32:24 +02:00
|
|
|
my %mapitems;
|
2007-07-25 03:04:56 +02:00
|
|
|
foreach my $page (keys %pagesources) {
|
2007-04-27 04:55:52 +02:00
|
|
|
if (pagespec_match($page, $params{pages}, location => $params{page})) {
|
2007-09-22 18:32:24 +02:00
|
|
|
$mapitems{$page}=1;
|
|
|
|
|
|
|
|
# Check for a common prefix.
|
|
|
|
if (! defined $common_prefix) {
|
|
|
|
$common_prefix=$page;
|
|
|
|
}
|
|
|
|
elsif (length $common_prefix &&
|
|
|
|
$page !~ /^\Q$common_prefix\E(\/|$)/) {
|
|
|
|
my @a=split(/\//, $page);
|
|
|
|
my @b=split(/\//, $common_prefix);
|
|
|
|
$common_prefix="";
|
|
|
|
while (@a && @b && $a[0] eq $b[0]) {
|
|
|
|
$common_prefix.=shift(@a);
|
|
|
|
shift @b;
|
|
|
|
}
|
|
|
|
}
|
2006-08-18 18:18:45 +02:00
|
|
|
}
|
|
|
|
}
|
2007-09-22 18:32:24 +02:00
|
|
|
|
|
|
|
# Common prefix should not be a page in the map.
|
|
|
|
while (length $common_prefix && exists $mapitems{$common_prefix}) {
|
|
|
|
$common_prefix=IkiWiki::dirname($common_prefix);
|
|
|
|
}
|
2006-08-18 18:18:45 +02:00
|
|
|
|
2007-07-25 03:04:56 +02:00
|
|
|
# Needs to update whenever a page is added or removed, so
|
|
|
|
# register a dependency.
|
|
|
|
add_depends($params{page}, $params{pages});
|
|
|
|
# Explicitly add all currently shown pages, to detect when pages
|
|
|
|
# are removed.
|
2007-09-22 18:32:24 +02:00
|
|
|
add_depends($params{page}, join(" or ", keys %mapitems));
|
2007-07-25 03:04:56 +02:00
|
|
|
|
2006-08-18 18:18:45 +02:00
|
|
|
# Create the map.
|
2007-09-21 19:54:58 +02:00
|
|
|
my $parent="";
|
2006-08-18 18:18:45 +02:00
|
|
|
my $indent=0;
|
2006-08-19 00:22:47 +02:00
|
|
|
my $openli=0;
|
2007-09-22 18:32:24 +02:00
|
|
|
my $map = "<div class='map'>\n<ul>\n";
|
|
|
|
foreach my $item (sort keys %mapitems) {
|
|
|
|
$item=~s/^\Q$common_prefix\E\/// if length $common_prefix;
|
|
|
|
my $depth = ($item =~ tr/\//\//) + 1;
|
2007-09-21 19:54:58 +02:00
|
|
|
my $baseitem=IkiWiki::dirname($item);
|
2007-09-22 18:32:24 +02:00
|
|
|
while (length $parent && length $baseitem && $baseitem !~ /^\Q$parent\E(\/|$)/) {
|
2007-09-21 19:54:58 +02:00
|
|
|
$parent=IkiWiki::dirname($parent);
|
|
|
|
$indent--;
|
2007-09-22 18:32:24 +02:00
|
|
|
$map .= "</li>\n";
|
|
|
|
if ($indent > 0) {
|
|
|
|
$map .= "</ul>\n";
|
|
|
|
}
|
2007-09-21 19:54:58 +02:00
|
|
|
}
|
2006-08-18 18:18:45 +02:00
|
|
|
while ($depth < $indent) {
|
|
|
|
$indent--;
|
2007-09-22 18:32:24 +02:00
|
|
|
$map .= "</li>\n";
|
|
|
|
if ($indent > 0) {
|
|
|
|
$map .= "</ul>\n";
|
|
|
|
}
|
2006-08-18 18:18:45 +02:00
|
|
|
}
|
2007-09-22 18:32:24 +02:00
|
|
|
my @bits=split("/", $item);
|
|
|
|
my $p="";
|
|
|
|
$p.="/".shift(@bits) for 1..$indent;
|
2006-08-18 18:18:45 +02:00
|
|
|
while ($depth > $indent) {
|
|
|
|
$indent++;
|
2007-09-22 18:32:24 +02:00
|
|
|
if ($indent > 1) {
|
|
|
|
$map .= "<ul>\n";
|
|
|
|
}
|
2007-09-21 19:54:58 +02:00
|
|
|
if ($depth > $indent) {
|
2007-09-22 18:32:24 +02:00
|
|
|
$p.="/".shift(@bits);
|
|
|
|
$map .= "<li>"
|
|
|
|
.htmllink($params{page}, $params{destpage}, $p, class => "mapparent")
|
2007-09-22 19:15:36 +02:00
|
|
|
."\n";
|
2007-09-21 19:54:58 +02:00
|
|
|
$openli=1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$openli=0;
|
|
|
|
}
|
2006-08-18 18:18:45 +02:00
|
|
|
}
|
2006-08-19 00:22:47 +02:00
|
|
|
$map .= "</li>\n" if $openli;
|
2006-08-18 18:18:45 +02:00
|
|
|
$map .= "<li>"
|
2007-09-22 18:32:24 +02:00
|
|
|
.htmllink($params{page}, $params{destpage},
|
|
|
|
"/".$common_prefix."/".$item, class => "mapitem")
|
2007-09-22 19:15:36 +02:00
|
|
|
."\n";
|
2006-08-19 00:22:47 +02:00
|
|
|
$openli=1;
|
2007-09-21 19:54:58 +02:00
|
|
|
$parent=$item;
|
2006-08-18 18:18:45 +02:00
|
|
|
}
|
|
|
|
while ($indent > 0) {
|
|
|
|
$indent--;
|
2007-09-22 18:32:24 +02:00
|
|
|
$map .= "</li>\n</ul>\n";
|
2006-08-18 18:18:45 +02:00
|
|
|
}
|
|
|
|
$map .= "</div>\n";
|
|
|
|
return $map;
|
|
|
|
} # }}}
|
|
|
|
|
|
|
|
1
|