summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: 6d22c49fd441183f420878884b072c686d5d1dc3 (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. hook(type => "getopt", id => "tag", call => \&getopt);
  10. hook(type => "preprocess", id => "tag", call => \&preprocess, scan => 1);
  11. hook(type => "pagetemplate", id => "tag", call => \&pagetemplate);
  12. } # }}}
  13. sub getopt () { #{{{
  14. eval q{use Getopt::Long};
  15. Getopt::Long::Configure('pass_through');
  16. GetOptions("tagbase=s" => \$config{tagbase});
  17. } #}}}
  18. sub tagpage ($) { #{{{
  19. my $tag=shift;
  20. if (exists $config{tagbase} &&
  21. defined $config{tagbase}) {
  22. $tag=$config{tagbase}."/".$tag;
  23. }
  24. return $tag;
  25. } #}}}
  26. sub preprocess (@) { #{{{
  27. if (! @_) {
  28. return "";
  29. }
  30. my %params=@_;
  31. my $page = $params{page};
  32. delete $params{page};
  33. delete $params{destpage};
  34. $tags{$page} = [];
  35. foreach my $tag (keys %params) {
  36. push @{$tags{$page}}, $tag;
  37. # hidden WikiLink
  38. push @{$links{$page}}, tagpage($tag);
  39. }
  40. return "";
  41. } # }}}
  42. sub pagetemplate (@) { #{{{
  43. my %params=@_;
  44. my $page=$params{page};
  45. my $destpage=$params{destpage};
  46. my $template=$params{template};
  47. $template->param(tags => [
  48. map {
  49. link => htmllink($page, $destpage, tagpage($_))
  50. }, @{$tags{$page}}
  51. ]) if exists $tags{$page} && @{$tags{$page}} && $template->query(name => "tags");
  52. if ($template->query(name => "pubdate")) {
  53. # It's an rss template. Add any categories.
  54. if (exists $tags{$page} && @{$tags{$page}}) {
  55. $template->param(categories => [map { category => $_ }, @{$tags{$page}}]);
  56. }
  57. }
  58. } # }}}
  59. 1