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