]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/parentlinks.pm
let's allow comments of "0"
[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         foreach my $dir (@pagepath) {
44                 next if $dir eq 'index';
45                 $depth=$i;
46                 $height=($pagedepth - $depth);
47                 push @ret, {
48                         url => urlto(bestlink($page, $path), $page),
49                         page => $title,
50                         depth => $depth,
51                         height => $height,
52                         "depth_$depth" => 1,
53                         "height_$height" => 1,
54                 };
55                 $path.="/".$dir;
56                 $title=pagetitle($dir);
57                 $i++;
58         }
59         return @ret;
60 }
61
62 sub pagetemplate (@) {
63         my %params=@_;
64         my $template=$params{template};
65
66         if ($template->query(name => "parentlinks") ||
67            $template->query(name => "has_parentlinks")) {
68                 my @links=parentlinks($params{page});
69                 $template->param(parentlinks => \@links);
70                 $template->param(has_parentlinks => (@links > 0));
71         }
72 }
73
74 1