summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/parentlinks.pm
blob: 203ea13b649e288fe4935d2743469721c15eb1e1 (plain)
  1. #!/usr/bin/perl
  2. # Ikiwiki parentlinks plugin.
  3. package IkiWiki::Plugin::parentlinks;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 3.00;
  7. sub import {
  8. hook(type => "parentlinks", id => "parentlinks", call => \&parentlinks);
  9. hook(type => "pagetemplate", id => "parentlinks", call => \&pagetemplate);
  10. hook(type => "getsetup", id => "parentlinks", call => \&getsetup);
  11. }
  12. sub getsetup () {
  13. return
  14. plugin => {
  15. safe => 1,
  16. rebuild => 1,
  17. section => "core",
  18. },
  19. }
  20. sub parentlinks ($) {
  21. my $page=shift;
  22. if (! length $page) {
  23. # dynamic page
  24. return {
  25. url => IkiWiki::baseurl(undef),
  26. page => $config{wikiname},
  27. };
  28. }
  29. my @ret;
  30. my $path="";
  31. my $title=$config{wikiname};
  32. my $i=0;
  33. my $depth=0;
  34. my $height=0;
  35. my @pagepath=(split("/", $page));
  36. my $pagedepth=@pagepath;
  37. foreach my $dir (@pagepath) {
  38. next if $dir eq 'index';
  39. $depth=$i;
  40. $height=($pagedepth - $depth);
  41. push @ret, {
  42. url => urlto(bestlink($page, $path), $page),
  43. page => $title,
  44. depth => $depth,
  45. height => $height,
  46. "depth_$depth" => 1,
  47. "height_$height" => 1,
  48. };
  49. $path.="/".$dir;
  50. $title=pagetitle($dir);
  51. $i++;
  52. }
  53. return @ret;
  54. }
  55. sub pagetemplate (@) {
  56. my %params=@_;
  57. my $template=$params{template};
  58. if ($template->query(name => "parentlinks") ||
  59. $template->query(name => "has_parentlinks")) {
  60. my @links=parentlinks($params{page});
  61. $template->param(parentlinks => \@links);
  62. $template->param(has_parentlinks => (@links > 0));
  63. }
  64. }
  65. 1