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