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