summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: f0e3c223c5523a3c40cb47909099bfc2972e9b76 (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. my %params=@_;
  50. return join(" ", map {
  51. if (/(.*)\|(.*)/) {
  52. my $tag=IkiWiki::linkpage($2);
  53. $tags{$params{page}}{$tag}=1;
  54. push @{$links{$params{page}}}, tagpage($tag);
  55. return htmllink($params{page}, $params{destpage},
  56. tagpage($tag),
  57. linktext => IkiWiki::pagetitle($1));
  58. }
  59. else {
  60. my $tag=IkiWiki::linkpage($_);
  61. $tags{$params{page}}{$tag}=1;
  62. push @{$links{$params{page}}}, tagpage($tag);
  63. return htmllink($params{page}, $params{destpage},
  64. tagpage($tag));
  65. }
  66. }
  67. grep {
  68. $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
  69. } keys %params);
  70. } # }}}
  71. sub pagetemplate (@) { #{{{
  72. my %params=@_;
  73. my $page=$params{page};
  74. my $destpage=$params{destpage};
  75. my $template=$params{template};
  76. $template->param(tags => [
  77. map {
  78. link => htmllink($page, $destpage, tagpage($_),
  79. rel => "tag")
  80. }, sort keys %{$tags{$page}}
  81. ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
  82. if ($template->query(name => "categories")) {
  83. # It's an rss/atom template. Add any categories.
  84. if (exists $tags{$page} && %{$tags{$page}}) {
  85. $template->param(categories => [map { category => $_ },
  86. sort keys %{$tags{$page}}]);
  87. }
  88. }
  89. } # }}}
  90. 1