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
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub import {
|
2008-08-03 22:40:12 +02:00
|
|
|
hook(type => "getsetup", id => "map", call => \&getsetup);
|
2006-09-10 00:50:27 +02:00
|
|
|
hook(type => "preprocess", id => "map", call => \&preprocess);
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-08-18 18:18:45 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub getsetup () {
|
2008-08-03 22:40:12 +02:00
|
|
|
return
|
|
|
|
plugin => {
|
|
|
|
safe => 1,
|
|
|
|
rebuild => undef,
|
|
|
|
},
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2008-08-03 22:40:12 +02:00
|
|
|
|
2008-12-17 21:22:16 +01:00
|
|
|
sub preprocess (@) {
|
2006-08-18 18:18:45 +02:00
|
|
|
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})) {
|
2008-06-15 22:11:11 +02:00
|
|
|
if (exists $params{show} &&
|
2008-06-16 00:10:47 +02:00
|
|
|
exists $pagestate{$page} &&
|
2008-06-15 22:11:11 +02:00
|
|
|
exists $pagestate{$page}{meta}{$params{show}}) {
|
|
|
|
$mapitems{$page}=$pagestate{$page}{meta}{$params{show}};
|
|
|
|
}
|
|
|
|
else {
|
2008-06-16 01:08:24 +02:00
|
|
|
$mapitems{$page}='';
|
2008-06-15 22:11:11 +02:00
|
|
|
}
|
2007-09-22 18:32:24 +02:00
|
|
|
# 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]) {
|
2007-12-28 23:22:17 +01:00
|
|
|
if (length $common_prefix) {
|
|
|
|
$common_prefix.="/";
|
|
|
|
}
|
2007-09-22 18:32:24 +02:00
|
|
|
$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.
|
2007-09-22 19:55:32 +02:00
|
|
|
while (defined $common_prefix && length $common_prefix &&
|
|
|
|
exists $mapitems{$common_prefix}) {
|
2007-09-22 18:32:24 +02:00
|
|
|
$common_prefix=IkiWiki::dirname($common_prefix);
|
|
|
|
}
|
2006-08-18 18:18:45 +02:00
|
|
|
|
2008-06-16 00:09:46 +02:00
|
|
|
# Needs to update whenever a page is added or removed (or in some
|
|
|
|
# cases, when its content changes, if show=title), so register a
|
|
|
|
# dependency.
|
2007-07-25 03:04:56 +02:00
|
|
|
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;
|
2008-08-23 19:10:49 +02:00
|
|
|
my $addparent="";
|
2007-09-22 18:32:24 +02:00
|
|
|
my $map = "<div class='map'>\n<ul>\n";
|
2008-06-16 01:08:24 +02:00
|
|
|
foreach my $item (sort keys %mapitems) {
|
|
|
|
my @linktext = (length $mapitems{$item} ? (linktext => $mapitems{$item}) : ());
|
2007-09-22 19:55:32 +02:00
|
|
|
$item=~s/^\Q$common_prefix\E\///
|
|
|
|
if defined $common_prefix && length $common_prefix;
|
2007-09-22 18:32:24 +02:00
|
|
|
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);
|
2008-08-23 19:10:49 +02:00
|
|
|
last if length $addparent && $baseitem =~ /^\Q$addparent\E(\/|$)/;
|
|
|
|
$addparent="";
|
2007-09-21 19:54:58 +02:00
|
|
|
$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);
|
2008-08-23 18:48:01 +02:00
|
|
|
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) {
|
2008-08-23 18:48:01 +02:00
|
|
|
$p.="/".shift(@bits);
|
2008-08-23 19:10:49 +02:00
|
|
|
$addparent=$p;
|
|
|
|
$addparent=~s/^\///;
|
2007-09-22 18:32:24 +02:00
|
|
|
$map .= "<li>"
|
2007-12-16 22:00:13 +01:00
|
|
|
.htmllink($params{page}, $params{destpage},
|
2008-08-23 18:48:01 +02:00
|
|
|
"/".$common_prefix.$p, class => "mapparent",
|
2007-12-16 22:00:13 +01:00
|
|
|
noimageinline => 1)
|
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},
|
2007-12-16 22:00:13 +01:00
|
|
|
"/".$common_prefix."/".$item,
|
2008-06-16 01:08:24 +02:00
|
|
|
@linktext,
|
2007-12-16 22:00:13 +01:00
|
|
|
class => "mapitem", noimageinline => 1)
|
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;
|
2008-12-17 21:22:16 +01:00
|
|
|
}
|
2006-08-18 18:18:45 +02:00
|
|
|
|
|
|
|
1
|