summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 71e71c02e4b4cb3aeccf2ec42247e9efa08dde96 (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. sub import { #{{{
  8. IkiWiki::hook(type => "preprocess", id => "inline",
  9. call => \&IkiWiki::preprocess_inline);
  10. # Hook to change to do pinging since it's called late.
  11. # This ensures each page only pings once and prevents slow
  12. # pings interrupting page builds.
  13. IkiWiki::hook(type => "change", id => "inline",
  14. call => \&IkiWiki::pingurl);
  15. } # }}}
  16. # Back to ikiwiki namespace for the rest, this code is very much
  17. # internal to ikiwiki even though it's separated into a plugin.
  18. package IkiWiki;
  19. my %toping;
  20. my $processing_inline=0;
  21. sub preprocess_inline (@) { #{{{
  22. my %params=@_;
  23. if (! exists $params{pages}) {
  24. return "";
  25. }
  26. if (! exists $params{archive}) {
  27. $params{archive}="no";
  28. }
  29. if (! exists $params{show} && $params{archive} eq "no") {
  30. $params{show}=10;
  31. }
  32. if (! exists $params{rss}) {
  33. $params{rss}="yes";
  34. }
  35. # Avoid nested inlines, to avoid loops etc.
  36. if ($processing_inline) {
  37. return "";
  38. }
  39. $processing_inline=1;
  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} && $params{rss} eq "yes") {
  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. (($params{archive} eq "no")
  71. ? "inlinepage.tmpl"
  72. : "inlinepagetitle.tmpl"),
  73. blind_cache => 1,
  74. );
  75. foreach my $page (@list) {
  76. # Don't use htmllink because this way the title is separate
  77. # and can be overridden by other plugins.
  78. my $link=htmlpage(bestlink($params{page}, $page));
  79. $link=abs2rel($link, dirname($params{page}));
  80. $template->param(pageurl => $link);
  81. $template->param(title => pagetitle(basename($page)));
  82. $template->param(content => get_inline_content($page, $params{page}))
  83. if $params{archive} eq "no";
  84. $template->param(ctime => displaytime($pagectime{$page}));
  85. run_hooks(pagetemplate => sub {
  86. shift->(page => $page, destpage => $params{page},
  87. template => $template,);
  88. });
  89. $ret.=$template->output;
  90. $template->clear_params;
  91. }
  92. # TODO: should really add this to renderedfiles and call
  93. # check_overwrite, but currently renderedfiles
  94. # only supports listing one file per page.
  95. if ($config{rss} && $params{rss} eq "yes") {
  96. writefile(rsspage($params{page}), $config{destdir},
  97. genrss($params{page}, @list));
  98. $toping{$params{page}}=1 unless $config{rebuild};
  99. }
  100. $processing_inline=0;
  101. return $ret;
  102. } #}}}
  103. sub get_inline_content ($$) { #{{{
  104. my $page=shift;
  105. my $destpage=shift;
  106. my $file=$pagesources{$page};
  107. my $type=pagetype($file);
  108. if (defined $type) {
  109. return htmlize($type, preprocess($page, $destpage, linkify($page, $destpage, readfile(srcfile($file)))));
  110. }
  111. else {
  112. return "";
  113. }
  114. } #}}}
  115. sub date_822 ($) { #{{{
  116. my $time=shift;
  117. eval q{use POSIX};
  118. return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  119. } #}}}
  120. sub absolute_urls ($$) { #{{{
  121. # sucky sub because rss sucks
  122. my $content=shift;
  123. my $url=shift;
  124. $url=~s/[^\/]+$//;
  125. $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
  126. $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
  127. return $content;
  128. } #}}}
  129. sub rsspage ($) { #{{{
  130. my $page=shift;
  131. return $page.".rss";
  132. } #}}}
  133. sub genrss ($@) { #{{{
  134. my $page=shift;
  135. my @pages=@_;
  136. my $url="$config{url}/".htmlpage($page);
  137. my $itemtemplate=template("rssitem.tmpl", blind_cache => 1,
  138. die_on_bad_params => 0);
  139. my $content="";
  140. foreach my $p (@pages) {
  141. next unless exists $renderedfiles{$p};
  142. $itemtemplate->param(
  143. title => pagetitle(basename($p)),
  144. url => "$config{url}/$renderedfiles{$p}",
  145. permalink => "$config{url}/$renderedfiles{$p}",
  146. pubdate => date_822($pagectime{$p}),
  147. content => absolute_urls(get_inline_content($p, $page), $url),
  148. );
  149. run_hooks(pagetemplate => sub {
  150. shift->(page => $p, destpage => $page,
  151. template => $itemtemplate);
  152. });
  153. $content.=$itemtemplate->output;
  154. $itemtemplate->clear_params;
  155. }
  156. my $template=template("rsspage.tmpl", blind_cache => 1);
  157. $template->param(
  158. title => $config{wikiname},
  159. wikiname => $config{wikiname},
  160. pageurl => $url,
  161. content => $content,
  162. );
  163. run_hooks(pagetemplate => sub {
  164. shift->(page => $page, destpage => $page,
  165. template => $template);
  166. });
  167. return $template->output;
  168. } #}}}
  169. sub pingurl (@) { #{{{
  170. return unless $config{pingurl} && %toping;
  171. eval q{require RPC::XML::Client};
  172. if ($@) {
  173. debug("RPC::XML::Client not found, not pinging");
  174. return;
  175. }
  176. foreach my $page (keys %toping) {
  177. my $title=pagetitle(basename($page));
  178. my $url="$config{url}/".htmlpage($page);
  179. foreach my $pingurl (@{$config{pingurl}}) {
  180. my $client = RPC::XML::Client->new($pingurl);
  181. my $req = RPC::XML::request->new('weblogUpdates.ping',
  182. $title, $url);
  183. debug("Pinging $pingurl for $page");
  184. my $res = $client->send_request($req);
  185. if (! ref $res) {
  186. debug("Did not receive response to ping");
  187. }
  188. my $r=$res->value;
  189. if (! exists $r->{flerror} || $r->{flerror}) {
  190. debug("Ping rejected: ".$r->{message});
  191. }
  192. }
  193. }
  194. } #}}}
  195. 1