summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 5c2aeb6c7ccecde1ec6df7c0a32ec3986cdbe2de (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,
  114. preprocess($page, $destpage,
  115. linkify($page, $destpage,
  116. filter($page,
  117. readfile(srcfile($file))))));
  118. }
  119. else {
  120. return "";
  121. }
  122. } #}}}
  123. sub date_822 ($) { #{{{
  124. my $time=shift;
  125. eval q{use POSIX};
  126. my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
  127. POSIX::setlocale(&POSIX::LC_TIME, "C");
  128. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  129. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  130. return $ret;
  131. } #}}}
  132. sub absolute_urls ($$) { #{{{
  133. # sucky sub because rss sucks
  134. my $content=shift;
  135. my $url=shift;
  136. $url=~s/[^\/]+$//;
  137. $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
  138. $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
  139. return $content;
  140. } #}}}
  141. sub rsspage ($) { #{{{
  142. my $page=shift;
  143. return $page.".rss";
  144. } #}}}
  145. sub genrss ($@) { #{{{
  146. my $page=shift;
  147. my @pages=@_;
  148. my $url=URI->new(encode_utf8("$config{url}/".htmlpage($page)));
  149. my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
  150. my $content="";
  151. foreach my $p (@pages) {
  152. next unless exists $renderedfiles{$p};
  153. my $u=URI->new(encode_utf8("$config{url}/$renderedfiles{$p}"));
  154. $itemtemplate->param(
  155. title => pagetitle(basename($p)),
  156. url => $u,
  157. permalink => $u,
  158. pubdate => date_822($pagectime{$p}),
  159. content => absolute_urls(get_inline_content($p, $page), $url),
  160. );
  161. run_hooks(pagetemplate => sub {
  162. shift->(page => $p, destpage => $page,
  163. template => $itemtemplate);
  164. });
  165. $content.=$itemtemplate->output;
  166. $itemtemplate->clear_params;
  167. }
  168. my $template=template("rsspage.tmpl", blind_cache => 1);
  169. $template->param(
  170. title => $config{wikiname},
  171. wikiname => $config{wikiname},
  172. pageurl => $url,
  173. content => $content,
  174. );
  175. run_hooks(pagetemplate => sub {
  176. shift->(page => $page, destpage => $page,
  177. template => $template);
  178. });
  179. return $template->output;
  180. } #}}}
  181. sub pingurl (@) { #{{{
  182. return unless $config{pingurl} && %toping;
  183. eval q{require RPC::XML::Client};
  184. if ($@) {
  185. debug("RPC::XML::Client not found, not pinging");
  186. return;
  187. }
  188. foreach my $page (keys %toping) {
  189. my $title=pagetitle(basename($page));
  190. my $url="$config{url}/".htmlpage($page);
  191. foreach my $pingurl (@{$config{pingurl}}) {
  192. my $client = RPC::XML::Client->new($pingurl);
  193. my $req = RPC::XML::request->new('weblogUpdates.ping',
  194. $title, $url);
  195. debug("Pinging $pingurl for $page");
  196. my $res = $client->send_request($req);
  197. if (! ref $res) {
  198. debug("Did not receive response to ping");
  199. }
  200. my $r=$res->value;
  201. if (! exists $r->{flerror} || $r->{flerror}) {
  202. debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  203. }
  204. }
  205. }
  206. } #}}}
  207. 1