summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 2cc17e32568037b27bec822fa926b0bfa70a586c (plain)
  1. #!/usr/bin/perl
  2. # Page inlining and blogging.
  3. package IkiWiki::Plugin::inline;
  4. use warnings;
  5. use strict;
  6. use IkiWiki;
  7. use URI;
  8. sub import { #{{{
  9. IkiWiki::hook(type => "preprocess", id => "inline",
  10. call => \&IkiWiki::preprocess_inline);
  11. # Hook to change to do pinging since it's called late.
  12. # This ensures each page only pings once and prevents slow
  13. # pings interrupting page builds.
  14. IkiWiki::hook(type => "change", id => "inline",
  15. call => \&IkiWiki::pingurl);
  16. } # }}}
  17. # Back to ikiwiki namespace for the rest, this code is very much
  18. # internal to ikiwiki even though it's separated into a plugin.
  19. package IkiWiki;
  20. my %toping;
  21. my @processing_inline;
  22. sub yesno ($) { #{{{
  23. my $val=shift;
  24. return (defined $val && lc($val) eq "yes");
  25. } #}}}
  26. sub preprocess_inline (@) { #{{{
  27. my %params=@_;
  28. # Avoid nested inlines, to avoid loops etc.
  29. return "" if grep { $_ eq $params{page} } @processing_inline;
  30. push @processing_inline, $params{page};
  31. if (! exists $params{pages}) {
  32. return "";
  33. }
  34. my $raw=yesno($params{raw});
  35. my $archive=yesno($params{archive});
  36. my $rss=exists $params{rss} ? yesno($params{rss}) : 1;
  37. if (! exists $params{show} && ! $archive) {
  38. $params{show}=10;
  39. }
  40. my @list;
  41. foreach my $page (keys %pagesources) {
  42. next if $page eq $params{page};
  43. if (pagespec_match($page, $params{pages})) {
  44. push @list, $page;
  45. }
  46. }
  47. @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
  48. if ($params{show} && @list > $params{show}) {
  49. @list=@list[0..$params{show} - 1];
  50. }
  51. add_depends($params{page}, $params{pages});
  52. my $ret="";
  53. if (exists $params{rootpage} && $config{cgiurl}) {
  54. # Add a blog post form, with a rss link button.
  55. my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
  56. $formtemplate->param(cgiurl => $config{cgiurl});
  57. $formtemplate->param(rootpage => $params{rootpage});
  58. if ($config{rss}) {
  59. $formtemplate->param(rssurl => rsspage(basename($params{page})));
  60. }
  61. $ret.=$formtemplate->output;
  62. }
  63. elsif ($config{rss} && $rss) {
  64. # Add a rss link button.
  65. my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
  66. $linktemplate->param(rssurl => rsspage(basename($params{page})));
  67. $ret.=$linktemplate->output;
  68. }
  69. my $template=template(
  70. ($archive ? "inlinepagetitle.tmpl" : "inlinepage.tmpl"),
  71. blind_cache => 1,
  72. ) unless $raw;
  73. foreach my $page (@list) {
  74. if (! $raw) {
  75. # Don't use htmllink because this way the title is separate
  76. # and can be overridden by other plugins.
  77. my $link=htmlpage(bestlink($params{page}, $page));
  78. $link=abs2rel($link, dirname($params{page}));
  79. $template->param(pageurl => $link);
  80. $template->param(title => pagetitle(basename($page)));
  81. # TODO: if $archive=1, the only reason to do this
  82. # is to let the meta plugin get page title info; so stop
  83. # calling this next line then once the meta plugin can
  84. # store that accross runs (also tags plugin).
  85. $template->param(content => get_inline_content($page, $params{page}));
  86. $template->param(ctime => displaytime($pagectime{$page}));
  87. run_hooks(pagetemplate => sub {
  88. shift->(page => $page, destpage => $params{page},
  89. template => $template,);
  90. });
  91. $ret.=$template->output;
  92. $template->clear_params;
  93. }
  94. else {
  95. my $file=$pagesources{$page};
  96. my $type=pagetype($file);
  97. if (defined $type) {
  98. $ret.="\n".
  99. preprocess($page, $params{page},
  100. linkify($page, $params{page},
  101. filter($page,
  102. readfile(srcfile($file)))));
  103. }
  104. }
  105. }
  106. # TODO: should really add this to renderedfiles and call
  107. # check_overwrite, but currently renderedfiles
  108. # only supports listing one file per page.
  109. if ($config{rss} && $rss) {
  110. writefile(rsspage($params{page}), $config{destdir},
  111. genrss($params{page}, @list));
  112. $toping{$params{page}}=1 unless $config{rebuild};
  113. }
  114. pop @processing_inline;
  115. return $ret;
  116. } #}}}
  117. sub get_inline_content ($$) { #{{{
  118. my $page=shift;
  119. my $destpage=shift;
  120. my $file=$pagesources{$page};
  121. my $type=pagetype($file);
  122. if (defined $type) {
  123. return htmlize($type,
  124. preprocess($page, $destpage,
  125. linkify($page, $destpage,
  126. filter($page,
  127. readfile(srcfile($file))))));
  128. }
  129. else {
  130. return "";
  131. }
  132. } #}}}
  133. sub date_822 ($) { #{{{
  134. my $time=shift;
  135. eval q{use POSIX};
  136. my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
  137. POSIX::setlocale(&POSIX::LC_TIME, "C");
  138. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  139. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  140. return $ret;
  141. } #}}}
  142. sub absolute_urls ($$) { #{{{
  143. # sucky sub because rss sucks
  144. my $content=shift;
  145. my $url=shift;
  146. $url=~s/[^\/]+$//;
  147. $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
  148. $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
  149. return $content;
  150. } #}}}
  151. sub rsspage ($) { #{{{
  152. my $page=shift;
  153. return $page.".rss";
  154. } #}}}
  155. sub genrss ($@) { #{{{
  156. my $page=shift;
  157. my @pages=@_;
  158. my $url=URI->new(encode_utf8("$config{url}/".htmlpage($page)));
  159. my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
  160. my $content="";
  161. foreach my $p (@pages) {
  162. next unless exists $renderedfiles{$p};
  163. my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
  164. $itemtemplate->param(
  165. title => pagetitle(basename($p)),
  166. url => $u,
  167. permalink => $u,
  168. pubdate => date_822($pagectime{$p}),
  169. content => absolute_urls(get_inline_content($p, $page), $url),
  170. );
  171. run_hooks(pagetemplate => sub {
  172. shift->(page => $p, destpage => $page,
  173. template => $itemtemplate);
  174. });
  175. $content.=$itemtemplate->output;
  176. $itemtemplate->clear_params;
  177. }
  178. my $template=template("rsspage.tmpl", blind_cache => 1);
  179. $template->param(
  180. title => $config{wikiname},
  181. wikiname => $config{wikiname},
  182. pageurl => $url,
  183. content => $content,
  184. );
  185. run_hooks(pagetemplate => sub {
  186. shift->(page => $page, destpage => $page,
  187. template => $template);
  188. });
  189. return $template->output;
  190. } #}}}
  191. sub pingurl (@) { #{{{
  192. return unless $config{pingurl} && %toping;
  193. eval q{require RPC::XML::Client};
  194. if ($@) {
  195. debug("RPC::XML::Client not found, not pinging");
  196. return;
  197. }
  198. foreach my $page (keys %toping) {
  199. my $title=pagetitle(basename($page));
  200. my $url="$config{url}/".htmlpage($page);
  201. foreach my $pingurl (@{$config{pingurl}}) {
  202. my $client = RPC::XML::Client->new($pingurl);
  203. my $req = RPC::XML::request->new('weblogUpdates.ping',
  204. $title, $url);
  205. debug("Pinging $pingurl for $page");
  206. my $res = $client->send_request($req);
  207. if (! ref $res) {
  208. debug("Did not receive response to ping");
  209. }
  210. my $r=$res->value;
  211. if (! exists $r->{flerror} || $r->{flerror}) {
  212. debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  213. }
  214. }
  215. }
  216. } #}}}
  217. 1