summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: 158657507331e2dd538f31c91a44c4cb09aec95a (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. }
  41. return $tag;
  42. } #}}}
  43. sub taglink ($$$;@) { #{{{
  44. my $page=shift;
  45. my $destpage=shift;
  46. my $tag=shift;
  47. my %opts=@_;
  48. my $link=tagpage($tag);
  49. # Force tag creation links to create the tag under /tagbase,
  50. # if there is a tagbase and this tag used it.
  51. $link="/".$link if $tag ne $link;
  52. return htmllink($page, $destpage, $link, %opts);
  53. } #}}}
  54. sub preprocess_tag (@) { #{{{
  55. if (! @_) {
  56. return "";
  57. }
  58. my %params=@_;
  59. my $page = $params{page};
  60. delete $params{page};
  61. delete $params{destpage};
  62. delete $params{preview};
  63. foreach my $tag (keys %params) {
  64. $tag=linkpage($tag);
  65. $tags{$page}{$tag}=1;
  66. # hidden WikiLink
  67. push @{$links{$page}}, tagpage($tag);
  68. }
  69. return "";
  70. } # }}}
  71. sub preprocess_taglink (@) { #{{{
  72. if (! @_) {
  73. return "";
  74. }
  75. my %params=@_;
  76. return join(" ", map {
  77. if (/(.*)\|(.*)/) {
  78. my $tag=linkpage($2);
  79. $tags{$params{page}}{$tag}=1;
  80. push @{$links{$params{page}}}, tagpage($tag);
  81. return taglink($params{page}, $params{destpage}, $tag,
  82. linktext => pagetitle($1));
  83. }
  84. else {
  85. my $tag=linkpage($_);
  86. $tags{$params{page}}{$tag}=1;
  87. push @{$links{$params{page}}}, tagpage($tag);
  88. return taglink($params{page}, $params{destpage}, $tag);
  89. }
  90. }
  91. grep {
  92. $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
  93. } keys %params);
  94. } # }}}
  95. sub pagetemplate (@) { #{{{
  96. my %params=@_;
  97. my $page=$params{page};
  98. my $destpage=$params{destpage};
  99. my $template=$params{template};
  100. $template->param(tags => [
  101. map {
  102. link => taglink($page, $destpage, $_, rel => "tag")
  103. }, sort keys %{$tags{$page}}
  104. ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
  105. if ($template->query(name => "categories")) {
  106. # It's an rss/atom template. Add any categories.
  107. if (exists $tags{$page} && %{$tags{$page}}) {
  108. $template->param(categories => [map { category => $_ },
  109. sort keys %{$tags{$page}}]);
  110. }
  111. }
  112. } # }}}
  113. 1