summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pedigree.pm
blob: 1bc9ee14db9c00bd642cadeb941a291e274438ab (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. is_root => ($i eq 0),
  26. is_second_ancestor => ($i eq 1),
  27. is_grand_mother => ($i eq ($pagedepth - 2)),
  28. is_mother => ($i eq ($pagedepth - 1)),
  29. };
  30. $path.="/".$dir;
  31. $title=IkiWiki::pagetitle($dir);
  32. $i++;
  33. }
  34. return @ret;
  35. } #}}}
  36. sub forget_oldest ($@) { #{{{
  37. my $offset=shift;
  38. my @pedigree=@_;
  39. my @ret;
  40. my $parent;
  41. unless ($offset ge scalar(@pedigree)) {
  42. for (my $i=0; $i < $offset; $i++) {
  43. shift @pedigree;
  44. }
  45. while (@pedigree) {
  46. # Doing so does not modify the original @pedigree, we've
  47. # got our own copy of its "content" (i.e. a pile of
  48. # references to hashes)...
  49. $parent=shift @pedigree;
  50. # ... but we have no copy of the referenced hashes, so we
  51. # actually are modifying them in-place, which
  52. # means the second (and following) calls to
  53. # this function overwrite the previous one's
  54. # reldepth values => known bug if PEDIGREE_BUT_ROOT and
  55. # PEDIGREE_BUT_TWO_OLDEST are used in the same template
  56. $parent->{reldepth}=($parent->{absdepth} - $offset);
  57. push @ret, $parent;
  58. }
  59. }
  60. return @ret;
  61. } #}}}
  62. sub pagetemplate (@) { #{{{
  63. my %params=@_;
  64. my $page=$params{page};
  65. my $template=$params{template};
  66. my @pedigree=pedigree($page)
  67. if ($template->query(name => "pedigree")
  68. or $template->query(name => "pedigree_but_root")
  69. or $template->query(name => "pedigree_but_two_oldest")
  70. );
  71. $template->param(pedigree => \@pedigree)
  72. if ($template->query(name => "pedigree"));
  73. $template->param(pedigree_but_root => [forget_oldest(1, @pedigree)])
  74. if ($template->query(name => "pedigree_but_root"));
  75. $template->param(pedigree_but_two_oldest => [forget_oldest(2, @pedigree)])
  76. if ($template->query(name => "pedigree_but_two_oldest"));
  77. } # }}}
  78. 1