summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: af4bff1bc7cfba4c9e6bea452ac8ca90ac55db2c (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. tagged_is_strict => {
  34. type => "boolean",
  35. default => 0,
  36. description => "if 1, tagged() doesn't match normal WikiLinks to tag pages",
  37. safe => 1,
  38. rebuild => 1,
  39. },
  40. }
  41. sub tagpage ($) {
  42. my $tag=shift;
  43. if ($tag !~ m{^\.?/} &&
  44. defined $config{tagbase}) {
  45. $tag="/".$config{tagbase}."/".$tag;
  46. $tag=~y#/#/#s; # squash dups
  47. }
  48. return $tag;
  49. }
  50. sub taglink ($$$;@) {
  51. my $page=shift;
  52. my $destpage=shift;
  53. my $tag=shift;
  54. my %opts=@_;
  55. return htmllink($page, $destpage, tagpage($tag), %opts);
  56. }
  57. sub preprocess_tag (@) {
  58. if (! @_) {
  59. return "";
  60. }
  61. my %params=@_;
  62. my $page = $params{page};
  63. delete $params{page};
  64. delete $params{destpage};
  65. delete $params{preview};
  66. foreach my $tag (keys %params) {
  67. $tag=linkpage($tag);
  68. # hidden WikiLink
  69. add_link($page, tagpage($tag), 'tag');
  70. }
  71. return "";
  72. }
  73. sub preprocess_taglink (@) {
  74. if (! @_) {
  75. return "";
  76. }
  77. my %params=@_;
  78. return join(" ", map {
  79. if (/(.*)\|(.*)/) {
  80. my $tag=linkpage($2);
  81. add_link($params{page}, tagpage($tag), 'tag');
  82. return taglink($params{page}, $params{destpage}, $tag,
  83. linktext => pagetitle($1));
  84. }
  85. else {
  86. my $tag=linkpage($_);
  87. add_link($params{page}, tagpage($tag), '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. my $tags = $typedlinks{$page}{tag};
  101. $template->param(tags => [
  102. map {
  103. link => taglink($page, $destpage, $_, rel => "tag")
  104. }, sort keys %$tags
  105. ]) if defined $tags && %$tags && $template->query(name => "tags");
  106. if ($template->query(name => "categories")) {
  107. # It's an rss/atom template. Add any categories.
  108. if (defined $tags && %$tags) {
  109. $template->param(categories => [map { category => $_ },
  110. sort keys %$tags]);
  111. }
  112. }
  113. }
  114. package IkiWiki::PageSpec;
  115. sub match_tagged ($$;@) {
  116. my $page = shift;
  117. my $glob = shift;
  118. if ($IkiWiki::config{tagged_is_strict}) {
  119. return match_link($page, IkiWiki::Plugin::tag::tagpage($glob), linktype => 'tag');
  120. }
  121. else {
  122. return match_link($page, IkiWiki::Plugin::tag::tagpage($glob));
  123. }
  124. }
  125. 1