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