summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: c6c99ae45cd008469b9918af315c2a2b4ffda132 (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 (! add_autofile($tagfile));
  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. }
  69. }
  70. sub preprocess_tag (@) {
  71. if (! @_) {
  72. return "";
  73. }
  74. my %params=@_;
  75. my $page = $params{page};
  76. delete $params{page};
  77. delete $params{destpage};
  78. delete $params{preview};
  79. foreach my $tag (keys %params) {
  80. $tag=linkpage($tag);
  81. $tags{$page}{$tag}=1;
  82. # add tagpage if necessary
  83. gentag($tag);
  84. # hidden WikiLink
  85. add_link($page, tagpage($tag));
  86. }
  87. return "";
  88. }
  89. sub preprocess_taglink (@) {
  90. if (! @_) {
  91. return "";
  92. }
  93. my %params=@_;
  94. return join(" ", map {
  95. if (/(.*)\|(.*)/) {
  96. my $tag=linkpage($2);
  97. $tags{$params{page}}{$tag}=1;
  98. add_link($params{page}, tagpage($tag));
  99. return taglink($params{page}, $params{destpage}, $tag,
  100. linktext => pagetitle($1));
  101. }
  102. else {
  103. my $tag=linkpage($_);
  104. $tags{$params{page}}{$tag}=1;
  105. add_link($params{page}, tagpage($tag));
  106. return taglink($params{page}, $params{destpage}, $tag);
  107. }
  108. }
  109. grep {
  110. $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
  111. } keys %params);
  112. }
  113. sub pagetemplate (@) {
  114. my %params=@_;
  115. my $page=$params{page};
  116. my $destpage=$params{destpage};
  117. my $template=$params{template};
  118. $template->param(tags => [
  119. map {
  120. link => taglink($page, $destpage, $_, rel => "tag")
  121. }, sort keys %{$tags{$page}}
  122. ]) if exists $tags{$page} && %{$tags{$page}} && $template->query(name => "tags");
  123. if ($template->query(name => "categories")) {
  124. # It's an rss/atom template. Add any categories.
  125. if (exists $tags{$page} && %{$tags{$page}}) {
  126. $template->param(categories => [map { category => $_ },
  127. sort keys %{$tags{$page}}]);
  128. }
  129. }
  130. }
  131. package IkiWiki::PageSpec;
  132. sub match_tagged ($$;@) {
  133. my $page = shift;
  134. my $glob = shift;
  135. return match_link($page, IkiWiki::Plugin::tag::tagpage($glob));
  136. }
  137. 1