summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 66cba027a210ec18727339c765360c808935b796 (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. # TODO: if $params{archive} eq "no", the only reason to do this
  84. # is to let the meta plugin get page title info; so stop
  85. # calling this next line then once the meta plugin can
  86. # store that accross runs (also tags plugin).
  87. $template->param(content => get_inline_content($page, $params{page}));
  88. $template->param(ctime => displaytime($pagectime{$page}));
  89. run_hooks(pagetemplate => sub {
  90. shift->(page => $page, destpage => $params{page},
  91. template => $template,);
  92. });
  93. $ret.=$template->output;
  94. $template->clear_params;
  95. }
  96. # TODO: should really add this to renderedfiles and call
  97. # check_overwrite, but currently renderedfiles
  98. # only supports listing one file per page.
  99. if ($config{rss} && $params{rss} eq "yes") {
  100. writefile(rsspage($params{page}), $config{destdir},
  101. genrss($params{page}, @list));
  102. $toping{$params{page}}=1 unless $config{rebuild};
  103. }
  104. $processing_inline=0;
  105. return $ret;
  106. } #}}}
  107. sub get_inline_content ($$) { #{{{
  108. my $page=shift;
  109. my $destpage=shift;
  110. my $file=$pagesources{$page};
  111. my $type=pagetype($file);
  112. if (defined $type) {
  113. return htmlize($type, preprocess($page, $destpage, linkify($page, $destpage, readfile(srcfile($file)))));
  114. }
  115. else {
  116. return "";
  117. }
  118. } #}}}
  119. sub date_822 ($) { #{{{
  120. my $time=shift;
  121. eval q{use POSIX};
  122. my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
  123. POSIX::setlocale(&POSIX::LC_TIME, "C");
  124. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  125. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  126. return $ret;
  127. } #}}}
  128. sub absolute_urls ($$) { #{{{
  129. # sucky sub because rss sucks
  130. my $content=shift;
  131. my $url=shift;
  132. $url=~s/[^\/]+$//;
  133. $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
  134. $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
  135. return $content;
  136. } #}}}
  137. sub rsspage ($) { #{{{
  138. my $page=shift;
  139. return $page.".rss";
  140. } #}}}
  141. sub genrss ($@) { #{{{
  142. my $page=shift;
  143. my @pages=@_;
  144. my $url=URI->new(encode_utf8("$config{url}/".htmlpage($page)));
  145. my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
  146. my $content="";
  147. foreach my $p (@pages) {
  148. next unless exists $renderedfiles{$p};
  149. my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
  150. $itemtemplate->param(
  151. title => pagetitle(basename($p)),
  152. url => $u,
  153. permalink => $u,
  154. pubdate => date_822($pagectime{$p}),
  155. content => absolute_urls(get_inline_content($p, $page), $url),
  156. );
  157. run_hooks(pagetemplate => sub {
  158. shift->(page => $p, destpage => $page,
  159. template => $itemtemplate);
  160. });
  161. $content.=$itemtemplate->output;
  162. $itemtemplate->clear_params;
  163. }
  164. my $template=template("rsspage.tmpl", blind_cache => 1);
  165. $template->param(
  166. title => $config{wikiname},
  167. wikiname => $config{wikiname},
  168. pageurl => $url,
  169. content => $content,
  170. );
  171. run_hooks(pagetemplate => sub {
  172. shift->(page => $page, destpage => $page,
  173. template => $template);
  174. });
  175. return $template->output;
  176. } #}}}
  177. sub pingurl (@) { #{{{
  178. return unless $config{pingurl} && %toping;
  179. eval q{require RPC::XML::Client};
  180. if ($@) {
  181. debug("RPC::XML::Client not found, not pinging");
  182. return;
  183. }
  184. foreach my $page (keys %toping) {
  185. my $title=pagetitle(basename($page));
  186. my $url="$config{url}/".htmlpage($page);
  187. foreach my $pingurl (@{$config{pingurl}}) {
  188. my $client = RPC::XML::Client->new($pingurl);
  189. my $req = RPC::XML::request->new('weblogUpdates.ping',
  190. $title, $url);
  191. debug("Pinging $pingurl for $page");
  192. my $res = $client->send_request($req);
  193. if (! ref $res) {
  194. debug("Did not receive response to ping");
  195. }
  196. my $r=$res->value;
  197. if (! exists $r->{flerror} || $r->{flerror}) {
  198. debug("Ping rejected: ".$r->{message});
  199. }
  200. }
  201. }
  202. } #}}}
  203. 1