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