summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: ca74fef9021f660a3420903a1715bf4cf0867808 (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. if (exists $IkiWiki::pagecase{lc $tagpage}) {
  94. $tagpage=$IkiWiki::pagecase{lc $tagpage}
  95. }
  96. my $tagfile = newpagefile($tagpage, $config{default_pageext});
  97. add_autofile($tagfile, "tag", sub {
  98. my $message=sprintf(gettext("creating tag page %s"), $tagpage);
  99. debug($message);
  100. my $template=template("autotag.tmpl");
  101. $template->param(tagname => tagname($tag));
  102. $template->param(tag => $tag);
  103. my $dir = $config{srcdir};
  104. if (! $config{tag_autocreate_commit}) {
  105. $dir = $IkiWiki::Plugin::transient::transientdir;
  106. }
  107. writefile($tagfile, $dir, $template->output);
  108. if ($config{rcs} && $config{tag_autocreate_commit}) {
  109. IkiWiki::disable_commit_hook();
  110. IkiWiki::rcs_add($tagfile);
  111. IkiWiki::rcs_commit_staged(message => $message);
  112. IkiWiki::enable_commit_hook();
  113. }
  114. });
  115. }
  116. }
  117. sub preprocess_tag (@) {
  118. if (! @_) {
  119. return "";
  120. }
  121. my %params=@_;
  122. my $page = $params{page};
  123. delete $params{page};
  124. delete $params{destpage};
  125. delete $params{preview};
  126. foreach my $tag (keys %params) {
  127. $tag=linkpage($tag);
  128. # hidden WikiLink
  129. add_link($page, taglink($tag), 'tag');
  130. gentag($tag);
  131. }
  132. return "";
  133. }
  134. sub preprocess_taglink (@) {
  135. if (! @_) {
  136. return "";
  137. }
  138. my %params=@_;
  139. return join(" ", map {
  140. if (/(.*)\|(.*)/) {
  141. my $tag=linkpage($2);
  142. add_link($params{page}, taglink($tag), 'tag');
  143. gentag($tag);
  144. return htmllink_tag($params{page}, $params{destpage}, $tag,
  145. linktext => pagetitle($1));
  146. }
  147. else {
  148. my $tag=linkpage($_);
  149. add_link($params{page}, taglink($tag), 'tag');
  150. gentag($tag);
  151. return htmllink_tag($params{page}, $params{destpage}, $tag);
  152. }
  153. }
  154. grep {
  155. $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
  156. } keys %params);
  157. }
  158. sub pagetemplate (@) {
  159. my %params=@_;
  160. my $page=$params{page};
  161. my $destpage=$params{destpage};
  162. my $template=$params{template};
  163. my $tags = $typedlinks{$page}{tag};
  164. $template->param(tags => [
  165. map {
  166. link => htmllink_tag($page, $destpage, $_,
  167. rel => "tag", linktext => tagname($_))
  168. }, sort keys %$tags
  169. ]) if defined $tags && %$tags && $template->query(name => "tags");
  170. if ($template->query(name => "categories")) {
  171. # It's an rss/atom template. Add any categories.
  172. if (defined $tags && %$tags) {
  173. $template->param(categories => [map { category => tagname($_) },
  174. sort keys %$tags]);
  175. }
  176. }
  177. }
  178. package IkiWiki::PageSpec;
  179. sub match_tagged ($$;@) {
  180. my $page=shift;
  181. my $glob=IkiWiki::Plugin::tag::taglink(shift);
  182. return match_link($page, $glob, linktype => 'tag', @_);
  183. }
  184. 1