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