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