summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: 2aa70d4068a81e769dd0a1491d4f9dc50873b8bc (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 => "items")) {
  56. # It's an rss template. Modify each item in the feed,
  57. # adding any categories based on the page for that item.
  58. foreach my $item (@{$template->param("items")}) {
  59. my $p=$item->{page};
  60. if (exists $tags{$p} && @{$tags{$p}}) {
  61. $item->{categories}=[];
  62. foreach my $tag (@{$tags{$p}}) {
  63. push @{$item->{categories}}, {
  64. category => $tag,
  65. };
  66. }
  67. }
  68. }
  69. }
  70. } # }}}
  71. 1