summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: 733d76f4eb16bf888c680f6456f9a8711fd42fab (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. delete $params{preview};
  36. foreach my $tag (keys %params) {
  37. $tag=IkiWiki::linkpage($tag);
  38. $tags{$page}{$tag}=1;
  39. # hidden WikiLink
  40. push @{$links{$page}}, tagpage($tag);
  41. }
  42. return "";
  43. } # }}}
  44. sub pagetemplate (@) { #{{{
  45. my %params=@_;
  46. my $page=$params{page};
  47. my $destpage=$params{destpage};
  48. my $template=$params{template};
  49. $template->param(tags => [
  50. map {
  51. link => htmllink($page, $destpage, tagpage($_))
  52. }, sort keys %{$tags{$page}}
  53. ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
  54. if ($template->query(name => "categories")) {
  55. # It's an rss/atom template. Add any categories.
  56. if (exists $tags{$page} && %{$tags{$page}}) {
  57. $template->param(categories => [map { category => $_ },
  58. sort keys %{$tags{$page}}]);
  59. }
  60. }
  61. } # }}}
  62. 1