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