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