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