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