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