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;
|
2008-12-23 22:34:19 +01:00
|
|
|
use IkiWiki 3.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,
|
2010-02-12 12:35:52 +01:00
|
|
|
section => "widget",
|
2008-08-03 22:40:12 +02:00
|
|
|
},
|
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};
|
|
|
|
|
2009-10-08 23:52:20 +02:00
|
|
|
# Needs to update whenever a page is added or removed (or in some
|
|
|
|
# cases, when its content changes, if show= is specified).
|
|
|
|
my $deptype=deptype(exists $params{show} ? "content" : "presence");
|
|
|
|
|
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;
|
2009-10-09 05:51:06 +02:00
|
|
|
foreach my $page (pagespec_match_list($params{page}, $params{pages},
|
|
|
|
deptype => $deptype)) {
|
2009-04-23 21:45:30 +02:00
|
|
|
if (exists $params{show} &&
|
|
|
|
exists $pagestate{$page} &&
|
|
|
|
exists $pagestate{$page}{meta}{$params{show}}) {
|
|
|
|
$mapitems{$page}=$pagestate{$page}{meta}{$params{show}};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$mapitems{$page}='';
|
|
|
|
}
|
|
|
|
# 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]) {
|
|
|
|
if (length $common_prefix) {
|
|
|
|
$common_prefix.="/";
|
2007-09-22 18:32:24 +02:00
|
|
|
}
|
2009-04-23 21:45:30 +02:00
|
|
|
$common_prefix.=shift(@a);
|
|
|
|
shift @b;
|
2007-09-22 18:32:24 +02:00
|
|
|
}
|
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
|
|
|
|
|
|
|
# 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="";
|
2009-07-25 21:16:11 +02:00
|
|
|
my $map = "<div class='map'>\n";
|
|
|
|
|
2009-10-08 23:52:20 +02:00
|
|
|
if (! keys %mapitems) {
|
|
|
|
# return empty div for empty map
|
2009-07-25 21:16:11 +02:00
|
|
|
$map .= "</div>\n";
|
|
|
|
return $map;
|
|
|
|
}
|
2009-10-08 23:52:20 +02:00
|
|
|
else {
|
2009-07-25 21:16:11 +02:00
|
|
|
$map .= "<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);
|
2011-01-30 23:02:46 +01:00
|
|
|
my $parentbase=IkiWiki::dirname($parent);
|
|
|
|
while (length $parentbase && length $baseitem && $baseitem !~ /^\Q$parentbase\E(\/|$)/) {
|
|
|
|
$parentbase=IkiWiki::dirname($parentbase);
|
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="";
|
2011-01-30 23:02:46 +01:00
|
|
|
$indent++ unless length $parent;
|
2008-08-23 18:48:01 +02:00
|
|
|
$p.="/".shift(@bits) for 1..$indent;
|
2006-08-18 18:18:45 +02:00
|
|
|
while ($depth > $indent) {
|
2011-01-30 23:02:46 +01:00
|
|
|
if (@bits && !(length $parent && "/$parent" eq $p)) {
|
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;
|
|
|
|
}
|
2011-01-30 23:02:46 +01:00
|
|
|
$indent++;
|
|
|
|
$p.="/".shift(@bits) if @bits;
|
|
|
|
if ($indent > 1) {
|
|
|
|
$map .= "<ul>\n";
|
|
|
|
}
|
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
|