summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: c0b7feb23ac7c34fdd0e2ebb1ba405c97005e644 (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. my %tags;
  8. sub import {
  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. }
  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 => 0,
  37. description => "Autocreate new tag pages",
  38. safe => 1,
  39. rebuild => 1,
  40. },
  41. }
  42. sub tagpage ($) {
  43. my $tag=shift;
  44. if ($tag !~ m{^\.?/} &&
  45. defined $config{tagbase}) {
  46. $tag="/".$config{tagbase}."/".$tag;
  47. $tag=~y#/#/#s; # squash dups
  48. }
  49. return $tag;
  50. }
  51. sub taglink ($$$;@) {
  52. my $page=shift;
  53. my $destpage=shift;
  54. my $tag=shift;
  55. my %opts=@_;
  56. return htmllink($page, $destpage, tagpage($tag), %opts);
  57. }
  58. sub gentag ($) {
  59. my $tag=shift;
  60. if (defined $config{tag_autocreate} && $config{tag_autocreate}) {
  61. my $tagfile = newpagefile(tagpage($tag), $config{default_pageext});
  62. $tagfile=~s/^\///;
  63. return if (srcfile($tagfile,1));
  64. debug(sprintf(gettext("creating tag page %s"), $tag));
  65. my $template=template("autotag.tmpl");
  66. $template->param(tag => $tag);
  67. writefile($tagfile, $config{srcdir}, $template->output);
  68. add_autofile("$config{srcdir}/$tagfile");
  69. }
  70. }
  71. sub preprocess_tag (@) {
  72. if (! @_) {
  73. return "";
  74. }
  75. my %params=@_;
  76. my $page = $params{page};
  77. delete $params{page};
  78. delete $params{destpage};
  79. delete $params{preview};
  80. foreach my $tag (keys %params) {
  81. $tag=linkpage($tag);
  82. $tags{$page}{$tag}=1;
  83. # add tagpage if necessary
  84. gentag($tag);
  85. # hidden WikiLink
  86. add_link($page, tagpage($tag));
  87. }
  88. return "";
  89. }
  90. sub preprocess_taglink (@) {
  91. if (! @_) {
  92. return "";
  93. }
  94. my %params=@_;
  95. return join(" ", map {
  96. if (/(.*)\|(.*)/) {
  97. my $tag=linkpage($2);
  98. $tags{$params{page}}{$tag}=1;
  99. add_link($params{page}, tagpage($tag));
  100. return taglink($params{page}, $params{destpage}, $tag,
  101. linktext => pagetitle($1));
  102. }
  103. else {
  104. my $tag=linkpage($_);
  105. $tags{$params{page}}{$tag}=1;
  106. add_link($params{page}, tagpage($tag));
  107. return taglink($params{page}, $params{destpage}, $tag);
  108. }
  109. }
  110. grep {
  111. $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
  112. } keys %params);
  113. }
  114. sub pagetemplate (@) {
  115. my %params=@_;
  116. my $page=$params{page};
  117. my $destpage=$params{destpage};
  118. my $template=$params{template};
  119. $template->param(tags => [
  120. map {
  121. link => taglink($page, $destpage, $_, rel => "tag")
  122. }, sort keys %{$tags{$page}}
  123. ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
  124. if ($template->query(name => "categories")) {
  125. # It's an rss/atom template. Add any categories.
  126. if (exists $tags{$page} && %{$tags{$page}}) {
  127. $template->param(categories => [map { category => $_ },
  128. sort keys %{$tags{$page}}]);
  129. }
  130. }
  131. }
  132. package IkiWiki::PageSpec;
  133. sub match_tagged ($$;@) {
  134. my $page = shift;
  135. my $glob = shift;
  136. return match_link($page, IkiWiki::Plugin::tag::tagpage($glob));
  137. }
  138. 1