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