]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/parentlinks.pm
attachment: Fix utf-8 display bug.
[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         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,
55                 };
56                 $path.="/".$dir;
57                 $title=pagetitle($dir);
58                 $i++;
59         }
60         return @ret;
61 }
62
63 sub pagetemplate (@) {
64         my %params=@_;
65         my $template=$params{template};
66
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));
72         }
73 }
74
75 1