]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/parentlinks.pm
Merge commit 'd69f6057d1e1787fa3c19cc13c40eb3e9b822283' into sipb
[ikiwiki.git] / IkiWiki / Plugin / parentlinks.pm
1 #!/usr/bin/perl
2 # Ikiwiki parentlinks plugin.
3 package IkiWiki::Plugin::parentlinks;
4
5 use warnings;
6 use strict;
7 use IkiWiki 3.00;
8
9 sub import {
10         hook(type => "parentlinks", id => "parentlinks", call => \&parentlinks);
11         hook(type => "pagetemplate", id => "parentlinks", call => \&pagetemplate);
12 }
13
14 sub getsetup () {
15         return 
16                 plugin => {
17                         safe => 1,
18                         rebuild => 1,
19                         section => "core",
20                 },
21 }
22
23 sub parentlinks ($) {
24         my $page=shift;
25
26         my @ret;
27         my $path="";
28         my $title=$config{wikiname};
29         my $i=0;
30         my $depth=0;
31         my $height=0;
32
33         my @pagepath=(split("/", $page));
34         my $pagedepth=@pagepath;
35
36         # The last element in @pagepath is the page itself, so punt that
37         # (These are /parent/ links, after all.)
38         pop @pagepath;
39
40         foreach my $dir (@pagepath) {
41                 next if $dir eq 'index';
42                 $depth=$i;
43                 $height=($pagedepth - $depth);
44                 $path.="/".$dir;
45                 $title=pagetitle($dir);
46                 push @ret, {
47                         url => urlto(bestlink($page, $path), $page),
48                         page => $title,
49                         depth => $depth,
50                         height => $height,
51                         "depth_$depth" => 1,
52                         "height_$height" => 1,
53                 };
54                 $i++;
55         }
56         return @ret;
57 }
58
59 sub pagetemplate (@) {
60         my %params=@_;
61         my $page=$params{page};
62         my $template=$params{template};
63
64         if ($template->query(name => "parentlinks") ||
65            $template->query(name => "has_parentlinks")) {
66                 my @links=parentlinks($page);
67                 $template->param(parentlinks => \@links);
68                 $template->param(has_parentlinks => (@links > 0));
69         }
70 }
71
72 1