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