]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/parentlinks.pm
Merge commit 'f8880cb670b0d1169559d04602fc543b1a6a061e' 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         if (! length $page) {
27                 # dynamic page
28                 return {
29                         url => $config{url},
30                         page => $config{wikiname},
31                 };
32         }
33
34         my @ret;
35         my $path="";
36         my $title=$config{wikiname};
37         my $i=0;
38         my $depth=0;
39         my $height=0;
40
41         my @pagepath=(split("/", $page));
42         my $pagedepth=@pagepath;
43
44         # The last element in @pagepath is the page itself, so punt that
45         # (These are /parent/ links, after all.)
46         pop @pagepath;
47
48         foreach my $dir (@pagepath) {
49                 next if $dir eq 'index';
50                 $depth=$i;
51                 $height=($pagedepth - $depth);
52                 $path.="/".$dir;
53                 $title=pagetitle($dir);
54                 push @ret, {
55                         url => urlto(bestlink($page, $path), $page),
56                         page => $title,
57                         depth => $depth,
58                         height => $height,
59                         "depth_$depth" => 1,
60                         "height_$height" => 1,
61                 };
62                 $i++;
63         }
64         return @ret;
65 }
66
67 sub pagetemplate (@) {
68         my %params=@_;
69         my $template=$params{template};
70
71         if ($template->query(name => "parentlinks") ||
72            $template->query(name => "has_parentlinks")) {
73                 my @links=parentlinks($params{page});
74                 $template->param(parentlinks => \@links);
75                 $template->param(has_parentlinks => (@links > 0));
76         }
77 }
78
79 1