summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pedigree.pm
blob: 3d1e3764d9f8fd110e0a9d06044e8c90c8864409 (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, but we don't care
  52. # here since reldepth has to be computed everytime anyway.
  53. $parent->{reldepth}=($parent->{absdepth} - $offset);
  54. push @ret, $parent;
  55. }
  56. }
  57. return @ret;
  58. } #}}}
  59. sub pagetemplate (@) { #{{{
  60. my %params=@_;
  61. my $page=$params{page};
  62. my $template=$params{template};
  63. if ($template->query(name => "pedigree")
  64. or $template->query(name => "pedigree_but_root")
  65. or $template->query(name => "pedigree_but_two_oldest")
  66. )
  67. {
  68. my @pedigree=pedigree($page);
  69. $template->param(pedigree => \@pedigree);
  70. $template->param(pedigree_but_root => [forget_oldest(1, @pedigree)]);
  71. $template->param(pedigree_but_two_oldest => [forget_oldest(2, @pedigree)]);
  72. }
  73. } # }}}
  74. 1