summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: fd5ce1e8a91488ce5d5b20709001fd3d177def97 (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. tag_autocreate => {
  34. type => "boolean",
  35. example => 1,
  36. description => "autocreate new tag pages?",
  37. safe => 1,
  38. rebuild => undef,
  39. },
  40. }
  41. sub taglink ($) {
  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. # Returns a tag name from a tag link
  51. sub tagname ($) {
  52. my $tag=shift;
  53. if (defined $config{tagbase}) {
  54. $tag =~ s!^/\Q$config{tagbase}\E/!!;
  55. } else {
  56. $tag =~ s!^\.?/!!;
  57. }
  58. return pagetitle($tag, 1);
  59. }
  60. sub htmllink_tag ($$$;@) {
  61. my $page=shift;
  62. my $destpage=shift;
  63. my $tag=shift;
  64. my %opts=@_;
  65. return htmllink($page, $destpage, taglink($tag), %opts);
  66. }
  67. sub gentag ($) {
  68. my $tag=shift;
  69. if ($config{tag_autocreate} ||
  70. ($config{tagbase} && ! defined $config{tag_autocreate})) {
  71. my $tagpage=taglink($tag);
  72. if ($tagpage=~/^\.\/(.*)/) {
  73. $tagpage=$1;
  74. }
  75. else {
  76. $tagpage=~s/^\///;
  77. }
  78. my $tagfile = newpagefile($tagpage, $config{default_pageext});
  79. add_autofile($tagfile, "tag", sub {
  80. my $message=sprintf(gettext("creating tag page %s"), $tagpage);
  81. debug($message);
  82. my $template=template("autotag.tmpl");
  83. $template->param(tagname => tagname($tag));
  84. $template->param(tag => $tag);
  85. writefile($tagfile, $config{srcdir}, $template->output);
  86. if ($config{rcs}) {
  87. IkiWiki::disable_commit_hook();
  88. IkiWiki::rcs_add($tagfile);
  89. IkiWiki::rcs_commit_staged(message => $message);
  90. IkiWiki::enable_commit_hook();
  91. }
  92. });
  93. }
  94. }
  95. sub preprocess_tag (@) {
  96. if (! @_) {
  97. return "";
  98. }
  99. my %params=@_;
  100. my $page = $params{page};
  101. delete $params{page};
  102. delete $params{destpage};
  103. delete $params{preview};
  104. foreach my $tag (keys %params) {
  105. $tag=linkpage($tag);
  106. # hidden WikiLink
  107. add_link($page, taglink($tag), 'tag');
  108. gentag($tag);
  109. }
  110. return "";
  111. }
  112. sub preprocess_taglink (@) {
  113. if (! @_) {
  114. return "";
  115. }
  116. my %params=@_;
  117. return join(" ", map {
  118. if (/(.*)\|(.*)/) {
  119. my $tag=linkpage($2);
  120. add_link($params{page}, taglink($tag), 'tag');
  121. gentag($tag);
  122. return htmllink_tag($params{page}, $params{destpage}, $tag,
  123. linktext => pagetitle($1));
  124. }
  125. else {
  126. my $tag=linkpage($_);
  127. add_link($params{page}, taglink($tag), 'tag');
  128. gentag($tag);
  129. return htmllink_tag($params{page}, $params{destpage}, $tag);
  130. }
  131. }
  132. grep {
  133. $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
  134. } keys %params);
  135. }
  136. sub pagetemplate (@) {
  137. my %params=@_;
  138. my $page=$params{page};
  139. my $destpage=$params{destpage};
  140. my $template=$params{template};
  141. my $tags = $typedlinks{$page}{tag};
  142. $template->param(tags => [
  143. map {
  144. link => htmllink_tag($page, $destpage, $_,
  145. rel => "tag", linktext => tagname($_))
  146. }, sort keys %$tags
  147. ]) if defined $tags && %$tags && $template->query(name => "tags");
  148. if ($template->query(name => "categories")) {
  149. # It's an rss/atom template. Add any categories.
  150. if (defined $tags && %$tags) {
  151. $template->param(categories => [map { category => tagname($_) },
  152. sort keys %$tags]);
  153. }
  154. }
  155. }
  156. package IkiWiki::PageSpec;
  157. sub match_tagged ($$;@) {
  158. my $page=shift;
  159. my $glob=IkiWiki::Plugin::tag::taglink(shift);
  160. return match_link($page, $glob, linktype => 'tag', @_);
  161. }
  162. 1