summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: b0a0e53be7cec3c73147ce61fe353aaa9b8a3d69 (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 ($tag !~ m{^\.?/} &&
  23. exists $config{tagbase} &&
  24. defined $config{tagbase}) {
  25. $tag=$config{tagbase}."/".$tag;
  26. }
  27. return $tag;
  28. } #}}}
  29. sub preprocess_tag (@) { #{{{
  30. if (! @_) {
  31. return "";
  32. }
  33. my %params=@_;
  34. my $page = $params{page};
  35. delete $params{page};
  36. delete $params{destpage};
  37. delete $params{preview};
  38. foreach my $tag (keys %params) {
  39. $tag=IkiWiki::linkpage($tag);
  40. $tags{$page}{$tag}=1;
  41. # hidden WikiLink
  42. push @{$links{$page}}, tagpage($tag);
  43. }
  44. return "";
  45. } # }}}
  46. sub preprocess_taglink (@) { #{{{
  47. if (! @_) {
  48. return "";
  49. }
  50. my %params=@_;
  51. return join(" ", map {
  52. if (/(.*)\|(.*)/) {
  53. my $tag=IkiWiki::linkpage($2);
  54. $tags{$params{page}}{$tag}=1;
  55. push @{$links{$params{page}}}, tagpage($tag);
  56. return htmllink($params{page}, $params{destpage},
  57. tagpage($tag),
  58. linktext => IkiWiki::pagetitle($1));
  59. }
  60. else {
  61. my $tag=IkiWiki::linkpage($_);
  62. $tags{$params{page}}{$tag}=1;
  63. push @{$links{$params{page}}}, tagpage($tag);
  64. return htmllink($params{page}, $params{destpage},
  65. tagpage($tag));
  66. }
  67. }
  68. grep {
  69. $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
  70. } keys %params);
  71. } # }}}
  72. sub pagetemplate (@) { #{{{
  73. my %params=@_;
  74. my $page=$params{page};
  75. my $destpage=$params{destpage};
  76. my $template=$params{template};
  77. $template->param(tags => [
  78. map {
  79. link => htmllink($page, $destpage, tagpage($_),
  80. rel => "tag")
  81. }, sort keys %{$tags{$page}}
  82. ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
  83. if ($template->query(name => "categories")) {
  84. # It's an rss/atom template. Add any categories.
  85. if (exists $tags{$page} && %{$tags{$page}}) {
  86. $template->param(categories => [map { category => $_ },
  87. sort keys %{$tags{$page}}]);
  88. }
  89. }
  90. } # }}}
  91. 1