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