summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: bae8e14327d5e2761e01a8aeddd33566e0741c04 (plain)
  1. #!/usr/bin/perl
  2. # Ikiwiki tag plugin.
  3. package IkiWiki::Plugin::tag;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 2.00;
  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. rel => "tag")
  53. }, sort keys %{$tags{$page}}
  54. ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
  55. if ($template->query(name => "categories")) {
  56. # It's an rss/atom template. Add any categories.
  57. if (exists $tags{$page} && %{$tags{$page}}) {
  58. $template->param(categories => [map { category => $_ },
  59. sort keys %{$tags{$page}}]);
  60. }
  61. }
  62. } # }}}
  63. 1