summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/tag.pm
blob: 1145a9f130966d4c69b1a6ce6b8373fdbaf0eafd (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 tagpage ($) {
  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 taglink ($$$;@) {
  51. my $page=shift;
  52. my $destpage=shift;
  53. my $tag=shift;
  54. my %opts=@_;
  55. return htmllink($page, $destpage, tagpage($tag), %opts);
  56. }
  57. sub gentag ($) {
  58. my $tag=shift;
  59. if ($config{tag_autocreate}) {
  60. my $tagfile = newpagefile(tagpage($tag), $config{default_pageext});
  61. $tagfile=~s/^\///;
  62. add_autofile($tagfile, sub {
  63. debug(sprintf(gettext("creating tag page %s"), $tag));
  64. my $template=template("autotag.tmpl");
  65. $template->param(tag => $tag);
  66. writefile($tagfile, $config{srcdir}, $template->output);
  67. });
  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. # hidden WikiLink
  82. add_link($page, tagpage($tag), 'tag');
  83. # add tagpage if necessary
  84. gentag($tag);
  85. }
  86. return "";
  87. }
  88. sub preprocess_taglink (@) {
  89. if (! @_) {
  90. return "";
  91. }
  92. my %params=@_;
  93. return join(" ", map {
  94. if (/(.*)\|(.*)/) {
  95. my $tag=linkpage($2);
  96. add_link($params{page}, tagpage($tag), 'tag');
  97. return taglink($params{page}, $params{destpage}, $tag,
  98. linktext => pagetitle($1));
  99. }
  100. else {
  101. my $tag=linkpage($_);
  102. add_link($params{page}, tagpage($tag), 'tag');
  103. return taglink($params{page}, $params{destpage}, $tag);
  104. }
  105. }
  106. grep {
  107. $_ ne 'page' && $_ ne 'destpage' && $_ ne 'preview'
  108. } keys %params);
  109. }
  110. sub pagetemplate (@) {
  111. my %params=@_;
  112. my $page=$params{page};
  113. my $destpage=$params{destpage};
  114. my $template=$params{template};
  115. my $tags = $typedlinks{$page}{tag};
  116. $template->param(tags => [
  117. map {
  118. link => taglink($page, $destpage, $_, rel => "tag")
  119. }, sort keys %$tags
  120. ]) if defined $tags && %$tags && $template->query(name => "tags");
  121. if ($template->query(name => "categories")) {
  122. # It's an rss/atom template. Add any categories.
  123. if (defined $tags && %$tags) {
  124. $template->param(categories => [map { category => $_ },
  125. sort keys %$tags]);
  126. }
  127. }
  128. }
  129. package IkiWiki::PageSpec;
  130. sub match_tagged ($$;@) {
  131. return match_link($_[0], IkiWiki::Plugin::tag::tagpage($_[1]), linktype => 'tag');
  132. }
  133. 1