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