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