Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[ikiwiki.git] / IkiWiki / Plugin / parentlinks.pm
blob9f16dd08296a5743084fc390ddde362e3a6a5fe9
1 #!/usr/bin/perl
2 # Ikiwiki parentlinks plugin.
3 package IkiWiki::Plugin::parentlinks;
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
9 sub import {
10 hook(type => "parentlinks", id => "parentlinks", call => \&parentlinks);
11 hook(type => "pagetemplate", id => "parentlinks", call => \&pagetemplate);
12 hook(type => "getsetup", id => "parentlinks", call => \&getsetup);
15 sub getsetup () {
16 return
17 plugin => {
18 safe => 1,
19 rebuild => 1,
20 section => "core",
24 sub parentlinks ($) {
25 my $page=shift;
27 if (! length $page) {
28 # dynamic page
29 return {
30 url => IkiWiki::baseurl(undef),
31 page => $config{wikiname},
35 my @ret;
36 my $path="";
37 my $title=$config{wikiname};
38 my $i=0;
39 my $depth=0;
40 my $height=0;
42 my @pagepath=(split("/", $page));
43 my $pagedepth=@pagepath;
44 foreach my $dir (@pagepath) {
45 next if $dir eq 'index';
46 $depth=$i;
47 $height=($pagedepth - $depth);
48 push @ret, {
49 url => urlto(bestlink($page, $path), $page),
50 page => $title,
51 depth => $depth,
52 height => $height,
53 "depth_$depth" => 1,
54 "height_$height" => 1,
56 $path.="/".$dir;
57 $title=pagetitle($dir);
58 $i++;
60 return @ret;
63 sub pagetemplate (@) {
64 my %params=@_;
65 my $template=$params{template};
67 if ($template->query(name => "parentlinks") ||
68 $template->query(name => "has_parentlinks")) {
69 my @links=parentlinks($params{page});
70 $template->param(parentlinks => \@links);
71 $template->param(has_parentlinks => (@links > 0));