summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/parentlinks.pm
blob: 432613ddfe92ff1b25d2069db246e49ed72a2222 (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. }
  11. sub getsetup () {
  12. return
  13. plugin => {
  14. safe => 1,
  15. rebuild => 1,
  16. section => "core",
  17. },
  18. }
  19. sub parentlinks ($) {
  20. my $page=shift;
  21. if (! length $page) {
  22. # dynamic page
  23. return {
  24. url => $config{url},
  25. page => $config{wikiname},
  26. };
  27. }
  28. my @ret;
  29. my $path="";
  30. my $title=$config{wikiname};
  31. my $i=0;
  32. my $depth=0;
  33. my $height=0;
  34. my @pagepath=(split("/", $page));
  35. my $pagedepth=@pagepath;
  36. foreach my $dir (@pagepath) {
  37. next if $dir eq 'index';
  38. $depth=$i;
  39. $height=($pagedepth - $depth);
  40. push @ret, {
  41. url => urlto(bestlink($page, $path), $page),
  42. page => $title,
  43. depth => $depth,
  44. height => $height,
  45. "depth_$depth" => 1,
  46. "height_$height" => 1,
  47. };
  48. $path.="/".$dir;
  49. $title=pagetitle($dir);
  50. $i++;
  51. }
  52. return @ret;
  53. }
  54. sub pagetemplate (@) {
  55. my %params=@_;
  56. my $template=$params{template};
  57. if ($template->query(name => "parentlinks") ||
  58. $template->query(name => "has_parentlinks")) {
  59. my @links=parentlinks($params{page});
  60. $template->param(parentlinks => \@links);
  61. $template->param(has_parentlinks => (@links > 0));
  62. }
  63. }
  64. 1