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