summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: b06eadd13d055bb31fda8567581caa8278a76ab9 (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. foreach my $tag (keys %params) {
  36. $tags{$page}{$tag}=1;
  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. }, sort keys %{$tags{$page}}
  51. ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
  52. if ($template->query(name => "categories")) {
  53. # It's an rss/atom template. Add any categories.
  54. if (exists $tags{$page} && %{$tags{$page}}) {
  55. $template->param(categories => [map { category => $_ },
  56. sort keys %{$tags{$page}}]);
  57. }
  58. }
  59. } # }}}
  60. 1