summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: d4cbb67059a2ed0cd38db658cad288341a979a4a (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 => "getsetup", id => "tag", call => \&getsetup);
  11. hook(type => "preprocess", id => "tag", call => \&preprocess_tag, scan => 1);
  12. hook(type => "preprocess", id => "taglink", call => \&preprocess_taglink, scan => 1);
  13. hook(type => "pagetemplate", id => "tag", call => \&pagetemplate);
  14. } # }}}
  15. sub getopt () { #{{{
  16. eval q{use Getopt::Long};
  17. error($@) if $@;
  18. Getopt::Long::Configure('pass_through');
  19. GetOptions("tagbase=s" => \$config{tagbase});
  20. } #}}}
  21. sub getsetup () { #{{{
  22. return
  23. tagbase => {
  24. type => "string",
  25. default => "",
  26. example => "tag",
  27. description => "parent page tags are located under",
  28. safe => 1,
  29. rebuild => 1,
  30. },
  31. } #}}}
  32. sub tagpage ($) { #{{{
  33. my $tag=shift;
  34. if ($tag !~ m{^\.?/} &&
  35. exists $config{tagbase} &&
  36. defined $config{tagbase}) {
  37. $tag=$config{tagbase}."/".$tag;
  38. }
  39. return $tag;
  40. } #}}}
  41. sub preprocess_tag (@) { #{{{
  42. if (! @_) {
  43. return "";
  44. }
  45. my %params=@_;
  46. my $page = $params{page};
  47. delete $params{page};
  48. delete $params{destpage};
  49. delete $params{preview};
  50. foreach my $tag (keys %params) {
  51. $tag=IkiWiki::linkpage($tag);
  52. $tags{$page}{$tag}=1;
  53. # hidden WikiLink
  54. push @{$links{$page}}, tagpage($tag);
  55. }
  56. return "";
  57. } # }}}
  58. sub preprocess_taglink (@) { #{{{
  59. if (! @_) {
  60. return "";
  61. }
  62. my %params=@_;
  63. return join(" ", map {
  64. if (/(.*)\|(.*)/) {
  65. my $tag=IkiWiki::linkpage($2);
  66. $tags{$params{page}}{$tag}=1;
  67. push @{$links{$params{page}}}, tagpage($tag);
  68. return htmllink($params{page}, $params{destpage},
  69. tagpage($tag),
  70. linktext => IkiWiki::pagetitle($1));
  71. }
  72. else {
  73. my $tag=IkiWiki::linkpage($_);
  74. $tags{$params{page}}{$tag}=1;
  75. push @{$links{$params{page}}}, tagpage($tag);
  76. return htmllink($params{page}, $params{destpage},
  77. tagpage($tag));
  78. }
  79. }
  80. grep {
  81. $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
  82. } keys %params);
  83. } # }}}
  84. sub pagetemplate (@) { #{{{
  85. my %params=@_;
  86. my $page=$params{page};
  87. my $destpage=$params{destpage};
  88. my $template=$params{template};
  89. $template->param(tags => [
  90. map {
  91. link => htmllink($page, $destpage, tagpage($_),
  92. rel => "tag")
  93. }, sort keys %{$tags{$page}}
  94. ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
  95. if ($template->query(name => "categories")) {
  96. # It's an rss/atom template. Add any categories.
  97. if (exists $tags{$page} && %{$tags{$page}}) {
  98. $template->param(categories => [map { category => $_ },
  99. sort keys %{$tags{$page}}]);
  100. }
  101. }
  102. } # }}}
  103. 1