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