summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/meta.pm
blob: cfa4b84b1d9d92382a28e60ae54501683c5dff31 (plain)
  1. #!/usr/bin/perl
  2. # Ikiwiki metadata plugin.
  3. package IkiWiki::Plugin::meta;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 2.00;
  7. my %meta;
  8. my %title;
  9. my %permalink;
  10. my %author;
  11. my %authorurl;
  12. my %license;
  13. my %copyright;
  14. sub import { #{{{
  15. hook(type => "preprocess", id => "meta", call => \&preprocess, scan => 1);
  16. hook(type => "filter", id => "meta", call => \&filter);
  17. hook(type => "pagetemplate", id => "meta", call => \&pagetemplate);
  18. } # }}}
  19. sub filter (@) { #{{{
  20. my %params=@_;
  21. $meta{$params{page}}='';
  22. return $params{content};
  23. } # }}}
  24. sub scrub ($) { #{{{
  25. if (IkiWiki::Plugin::htmlscrubber->can("sanitize")) {
  26. return IkiWiki::Plugin::htmlscrubber::sanitize(content => shift);
  27. }
  28. else {
  29. return shift;
  30. }
  31. } #}}}
  32. sub preprocess (@) { #{{{
  33. if (! @_) {
  34. return "";
  35. }
  36. my %params=@_;
  37. my $key=shift;
  38. my $value=$params{$key};
  39. delete $params{$key};
  40. my $page=$params{page};
  41. delete $params{page};
  42. my $destpage=$params{destpage};
  43. delete $params{destpage};
  44. delete $params{preview};
  45. eval q{use HTML::Entities};
  46. # Always dencode, even if encoding later, since it might not be
  47. # fully encoded.
  48. $value=decode_entities($value);
  49. if ($key eq 'link') {
  50. if (%params) {
  51. $meta{$page}.=scrub("<link href=\"".encode_entities($value)."\" ".
  52. join(" ", map { encode_entities($_)."=\"".encode_entities(decode_entities($params{$_}))."\"" } keys %params).
  53. " />\n");
  54. }
  55. else {
  56. # hidden WikiLink
  57. push @{$links{$page}}, $value;
  58. }
  59. }
  60. elsif ($key eq 'title') {
  61. $title{$page}=HTML::Entities::encode_numeric($value);
  62. }
  63. elsif ($key eq 'permalink') {
  64. $permalink{$page}=$value;
  65. $meta{$page}.=scrub("<link rel=\"bookmark\" href=\"".encode_entities($value)."\" />\n");
  66. }
  67. elsif ($key eq 'date') {
  68. eval q{use Date::Parse};
  69. if (! $@) {
  70. my $time = str2time($value);
  71. $IkiWiki::pagectime{$page}=$time if defined $time;
  72. }
  73. }
  74. elsif ($key eq 'stylesheet') {
  75. my $rel=exists $params{rel} ? $params{rel} : "alternate stylesheet";
  76. my $title=exists $params{title} ? $params{title} : $value;
  77. # adding .css to the value prevents using any old web
  78. # editable page as a stylesheet
  79. my $stylesheet=bestlink($page, $value.".css");
  80. if (! length $stylesheet) {
  81. return "[[meta ".gettext("stylesheet not found")."]]";
  82. }
  83. $meta{$page}.='<link href="'.urlto($stylesheet, $page).
  84. '" rel="'.encode_entities($rel).
  85. '" title="'.encode_entities($title).
  86. "\" type=\"text/css\" />\n";
  87. }
  88. elsif ($key eq 'openid') {
  89. if (exists $params{server}) {
  90. $meta{$page}.='<link href="'.encode_entities($params{server}).
  91. "\" rel=\"openid.server\" />\n";
  92. }
  93. $meta{$page}.='<link href="'.encode_entities($value).
  94. "\" rel=\"openid.delegate\" />\n";
  95. }
  96. elsif ($key eq 'license') {
  97. $meta{$page}.="<link rel=\"license\" href=\"#page_license\" />\n";
  98. $license{$page}=$value;
  99. }
  100. elsif ($key eq 'copyright') {
  101. $meta{$page}.="<link rel=\"copyright\" href=\"#page_copyright\" />\n";
  102. $copyright{$page}=$value;
  103. }
  104. elsif ($key eq 'forward') {
  105. my $delay=0;
  106. my $dest_url;
  107. my $text;
  108. if (exists $params{delay}) {
  109. $delay=$params{delay};
  110. }
  111. # Is this a wikilink?
  112. if ($value =~ /^\[\[(.*)\]\]$/) {
  113. $text=htmllink($page, $destpage, $1);
  114. $dest_url=urlto(bestlink($page, $1), $destpage);
  115. } else {
  116. $text="<a href=\"$dest_url\">$dest_url</a>";
  117. $dest_url=$value;
  118. }
  119. # TODO. $meta{$page}.=scrub("<meta http-equiv=\"refresh\" content=\"$delay; URL=$dest_url\">");
  120. $meta{$page}.="<meta http-equiv=\"refresh\" content=\"$delay; URL=$dest_url\">";
  121. return "You are being forwarded to $text.";
  122. }
  123. else {
  124. $meta{$page}.=scrub("<meta name=\"".encode_entities($key).
  125. "\" content=\"".encode_entities($value)."\" />\n");
  126. if ($key eq 'author') {
  127. $author{$page}=$value;
  128. }
  129. elsif ($key eq 'authorurl') {
  130. $authorurl{$page}=$value;
  131. }
  132. }
  133. return "";
  134. } # }}}
  135. sub pagetemplate (@) { #{{{
  136. my %params=@_;
  137. my $page=$params{page};
  138. my $destpage=$params{destpage};
  139. my $template=$params{template};
  140. $template->param(meta => $meta{$page})
  141. if exists $meta{$page} && $template->query(name => "meta");
  142. if (exists $title{$page} && $template->query(name => "title")) {
  143. $template->param(title => $title{$page});
  144. $template->param(title_overridden => 1);
  145. }
  146. $template->param(permalink => $permalink{$page})
  147. if exists $permalink{$page} && $template->query(name => "permalink");
  148. $template->param(author => $author{$page})
  149. if exists $author{$page} && $template->query(name => "author");
  150. $template->param(authorurl => $authorurl{$page})
  151. if exists $authorurl{$page} && $template->query(name => "authorurl");
  152. if ($page ne $destpage &&
  153. ((exists $license{$page} && ! exists $license{$destpage}) ||
  154. (exists $copyright{$page} && ! exists $copyright{$destpage}))) {
  155. # Force a scan of the destpage to get its copyright/license
  156. # info. If the info is declared after an inline, it will
  157. # otherwise not be available at this point.
  158. IkiWiki::scan($pagesources{$destpage});
  159. }
  160. if (exists $license{$page} && $template->query(name => "license") &&
  161. ($page eq $destpage || ! exists $license{$destpage} ||
  162. $license{$page} ne $license{$destpage})) {
  163. $template->param(license => IkiWiki::linkify($page, $destpage, $license{$page}));
  164. }
  165. if (exists $copyright{$page} && $template->query(name => "copyright") &&
  166. ($page eq $destpage || ! exists $copyright{$destpage} ||
  167. $copyright{$page} ne $copyright{$destpage})) {
  168. $template->param(copyright => IkiWiki::linkify($page, $destpage, $copyright{$page}));
  169. }
  170. } # }}}
  171. 1