summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: 56bf17e2c1042ae367c1cd9afa63012ff33723ab (plain)
  1. #!/usr/bin/perl
  2. # Ikiwiki tag plugin.
  3. package IkiWiki::Plugin::tag;
  4. use warnings;
  5. use strict;
  6. use IkiWiki;
  7. my %tags;
  8. sub import { #{{{
  9. IkiWiki::hook(type => "getopt", id => "tag",
  10. call => \&getopt);
  11. IkiWiki::hook(type => "preprocess", id => "tag",
  12. call => \&preprocess);
  13. IkiWiki::hook(type => "pagetemplate", id => "tag",
  14. call => \&pagetemplate);
  15. } # }}}
  16. sub getopt () { #{{{
  17. eval q{use Getopt::Long};
  18. Getopt::Long::Configure('pass_through');
  19. GetOptions("tagbase=s" => \$IkiWiki::config{tagbase});
  20. } #}}}
  21. sub preprocess (@) { #{{{
  22. if (! @_) {
  23. return "";
  24. }
  25. my %params=@_;
  26. my $page = $params{page};
  27. delete $params{page};
  28. delete $params{destpage};
  29. $tags{$page} = [];
  30. foreach my $tag (keys %params) {
  31. if (exists $IkiWiki::config{tagbase}) {
  32. $tag=$IkiWiki::config{tagbase}."/".$tag;
  33. }
  34. push @{$tags{$page}}, $tag;
  35. # hidden WikiLink
  36. push @{$IkiWiki::links{$page}}, $tag;
  37. }
  38. return "";
  39. } # }}}
  40. sub pagetemplate (@) { #{{{
  41. my %params=@_;
  42. my $page=$params{page};
  43. my $destpage=$params{destpage};
  44. my $template=$params{template};
  45. $template->param(tags => [
  46. map { link => IkiWiki::htmllink($page, $destpage, $_) },
  47. @{$tags{$page}}
  48. ]) if exists $tags{$page} && @{$tags{$page}} && $template->query(name => "tags");
  49. } # }}}
  50. 1