]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/pedigree.pm
pedigree: fix RELDEPTH in PEDIGREE_BUT_ROOT...
[ikiwiki.git] / IkiWiki / Plugin / pedigree.pm
1 #!/usr/bin/perl
2 # -*- cperl-indent-level: 8; -*-
3 # Ikiwiki pedigree plugin.
4 package IkiWiki::Plugin::pedigree;
5
6 use warnings;
7 use strict;
8 use IkiWiki 2.00;
9
10 sub import { #{{{
11         hook(type => "pagetemplate", id => "pedigree", call => \&pagetemplate);
12 } # }}}
13
14 sub pedigree ($) { #{{{
15         my $page=shift;
16
17         my @ret;
18         my $path="";
19         my $title=$config{wikiname};
20         my $i=0;
21
22         my @pagepath=(split("/", $page));
23         my $pagedepth=@pagepath;
24         foreach my $dir (@pagepath) {
25                 next if $dir eq 'index';
26                 push @ret, {
27                             url => urlto($path, $page),
28                             page => $title,
29                             absdepth => $i,
30                             is_root => ($i eq 0),
31                             is_second_ancestor => ($i eq 1),
32                             is_grand_mother => ($i eq ($pagedepth - 2)),
33                             is_mother => ($i eq ($pagedepth - 1)),
34                            };
35                 $path.="/".$dir;
36                 $title=IkiWiki::pagetitle($dir);
37                 $i++;
38         }
39         return @ret;
40 } #}}}
41
42 sub forget_oldest ($@) { #{{{
43         my $offset=shift;
44         my @pedigree=@_;
45         my @ret;
46         my $parent;
47         unless ($offset ge scalar(@pedigree)) {
48                 for (my $i=0; $i < $offset; $i++) {
49                         shift @pedigree;
50                 }
51                 while (@pedigree) {
52                         # Doing so does not modify the original @pedigree, we've
53                         # got our own copy of its "content" (i.e. a pile of
54                         # references to hashes)...
55                         $parent=shift @pedigree;
56                         # ... but we have no copy of the referenced hashes, so we
57                         # actually are modifying them in-place, which
58                         # means the second (and following) calls to
59                         # this function overwrite the previous one's
60                         # reldepth values => known bug if PEDIGREE_BUT_ROOT and
61                         # PEDIGREE_BUT_TWO_OLDEST are used in the same template
62                         $parent->{reldepth}=($parent->{absdepth} - $offset);
63                         push @ret, $parent;
64                 }
65         }
66         return @ret;
67 } #}}}
68
69 sub pagetemplate (@) { #{{{
70         my %params=@_;
71         my $page=$params{page};
72         my $template=$params{template};
73
74         my @pedigree=pedigree($page)
75           if ($template->query(name => "pedigree")
76               or $template->query(name => "pedigree_but_root")
77               or $template->query(name => "pedigree_but_two_oldest")
78              );
79
80         $template->param(pedigree => \@pedigree)
81           if ($template->query(name => "pedigree"));
82         $template->param(pedigree_but_root => [forget_oldest(1, @pedigree)])
83           if ($template->query(name => "pedigree_but_root"));
84         $template->param(pedigree_but_two_oldest => [forget_oldest(2, @pedigree)])
85           if ($template->query(name => "pedigree_but_two_oldest"));
86
87 } # }}}
88
89 1