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