summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 0f54d9a7d30649a461f25ecc226b4d61a132abc3 (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=0;
  22. sub preprocess_inline (@) { #{{{
  23. my %params=@_;
  24. if (! exists $params{pages}) {
  25. return "";
  26. }
  27. if (! exists $params{archive}) {
  28. $params{archive}="no";
  29. }
  30. if (! exists $params{show} && $params{archive} eq "no") {
  31. $params{show}=10;
  32. }
  33. if (! exists $params{rss}) {
  34. $params{rss}="yes";
  35. }
  36. # Avoid nested inlines, to avoid loops etc.
  37. if ($processing_inline) {
  38. return "";
  39. }
  40. $processing_inline=1;
  41. my @list;
  42. foreach my $page (keys %pagesources) {
  43. next if $page eq $params{page};
  44. if (pagespec_match($page, $params{pages})) {
  45. push @list, $page;
  46. }
  47. }
  48. @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
  49. if ($params{show} && @list > $params{show}) {
  50. @list=@list[0..$params{show} - 1];
  51. }
  52. add_depends($params{page}, $params{pages});
  53. my $ret="";
  54. if (exists $params{rootpage} && $config{cgiurl}) {
  55. # Add a blog post form, with a rss link button.
  56. my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
  57. $formtemplate->param(cgiurl => $config{cgiurl});
  58. $formtemplate->param(rootpage => $params{rootpage});
  59. if ($config{rss}) {
  60. $formtemplate->param(rssurl => rsspage(basename($params{page})));
  61. }
  62. $ret.=$formtemplate->output;
  63. }
  64. elsif ($config{rss} && $params{rss} eq "yes") {
  65. # Add a rss link button.
  66. my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
  67. $linktemplate->param(rssurl => rsspage(basename($params{page})));
  68. $ret.=$linktemplate->output;
  69. }
  70. my $template=template(
  71. (($params{archive} eq "no")
  72. ? "inlinepage.tmpl"
  73. : "inlinepagetitle.tmpl"),
  74. blind_cache => 1,
  75. );
  76. foreach my $page (@list) {
  77. # Don't use htmllink because this way the title is separate
  78. # and can be overridden by other plugins.
  79. my $link=htmlpage(bestlink($params{page}, $page));
  80. $link=abs2rel($link, dirname($params{page}));
  81. $template->param(pageurl => $link);
  82. $template->param(title => pagetitle(basename($page)));
  83. $template->param(content => get_inline_content($page, $params{page}))
  84. if $params{archive} eq "no";
  85. $template->param(ctime => displaytime($pagectime{$page}));
  86. run_hooks(pagetemplate => sub {
  87. shift->(page => $page, destpage => $params{page},
  88. template => $template,);
  89. });
  90. $ret.=$template->output;
  91. $template->clear_params;
  92. }
  93. # TODO: should really add this to renderedfiles and call
  94. # check_overwrite, but currently renderedfiles
  95. # only supports listing one file per page.
  96. if ($config{rss} && $params{rss} eq "yes") {
  97. writefile(rsspage($params{page}), $config{destdir},
  98. genrss($params{page}, @list));
  99. $toping{$params{page}}=1 unless $config{rebuild};
  100. }
  101. $processing_inline=0;
  102. return $ret;
  103. } #}}}
  104. sub get_inline_content ($$) { #{{{
  105. my $page=shift;
  106. my $destpage=shift;
  107. my $file=$pagesources{$page};
  108. my $type=pagetype($file);
  109. if (defined $type) {
  110. return htmlize($type, preprocess($page, $destpage, linkify($page, $destpage, readfile(srcfile($file)))));
  111. }
  112. else {
  113. return "";
  114. }
  115. } #}}}
  116. sub date_822 ($) { #{{{
  117. my $time=shift;
  118. eval q{use POSIX};
  119. return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  120. } #}}}
  121. sub absolute_urls ($$) { #{{{
  122. # sucky sub because rss sucks
  123. my $content=shift;
  124. my $url=shift;
  125. $url=~s/[^\/]+$//;
  126. $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
  127. $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
  128. return $content;
  129. } #}}}
  130. sub rsspage ($) { #{{{
  131. my $page=shift;
  132. return $page.".rss";
  133. } #}}}
  134. sub genrss ($@) { #{{{
  135. my $page=shift;
  136. my @pages=@_;
  137. my $url=URI->new("$config{url}/".htmlpage($page));
  138. my $itemtemplate=template("rssitem.tmpl", blind_cache => 1,
  139. die_on_bad_params => 0);
  140. my $content="";
  141. foreach my $p (@pages) {
  142. next unless exists $renderedfiles{$p};
  143. my $u=URI->new("$config{url}/$renderedfiles{$p}");
  144. $itemtemplate->param(
  145. title => pagetitle(basename($p)),
  146. url => $u,
  147. permalink => $u,
  148. pubdate => date_822($pagectime{$p}),
  149. content => absolute_urls(get_inline_content($p, $page), $url),
  150. );
  151. run_hooks(pagetemplate => sub {
  152. shift->(page => $p, destpage => $page,
  153. template => $itemtemplate);
  154. });
  155. $content.=$itemtemplate->output;
  156. $itemtemplate->clear_params;
  157. }
  158. my $template=template("rsspage.tmpl", blind_cache => 1);
  159. $template->param(
  160. title => $config{wikiname},
  161. wikiname => $config{wikiname},
  162. pageurl => $url,
  163. content => $content,
  164. );
  165. run_hooks(pagetemplate => sub {
  166. shift->(page => $page, destpage => $page,
  167. template => $template);
  168. });
  169. return $template->output;
  170. } #}}}
  171. sub pingurl (@) { #{{{
  172. return unless $config{pingurl} && %toping;
  173. eval q{require RPC::XML::Client};
  174. if ($@) {
  175. debug("RPC::XML::Client not found, not pinging");
  176. return;
  177. }
  178. foreach my $page (keys %toping) {
  179. my $title=pagetitle(basename($page));
  180. my $url="$config{url}/".htmlpage($page);
  181. foreach my $pingurl (@{$config{pingurl}}) {
  182. my $client = RPC::XML::Client->new($pingurl);
  183. my $req = RPC::XML::request->new('weblogUpdates.ping',
  184. $title, $url);
  185. debug("Pinging $pingurl for $page");
  186. my $res = $client->send_request($req);
  187. if (! ref $res) {
  188. debug("Did not receive response to ping");
  189. }
  190. my $r=$res->value;
  191. if (! exists $r->{flerror} || $r->{flerror}) {
  192. debug("Ping rejected: ".$r->{message});
  193. }
  194. }
  195. }
  196. } #}}}
  197. 1