summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: c7cafee12e2e5330e859af78b8e3f29a7f33958a (plain)
  1. #!/usr/bin/perl
  2. # Page inlining and blogging.
  3. package IkiWiki::Plugin::inline;
  4. use warnings;
  5. use strict;
  6. use IkiWiki 1.00;
  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. if ($config{rss} && $rss) {
  139. will_render($params{page}, rsspage($params{page}));
  140. writefile(rsspage($params{page}), $config{destdir},
  141. genrss($desc, $params{page}, @list));
  142. $toping{$params{page}}=1 unless $config{rebuild};
  143. $rsslinks{$params{destpage}}=qq{<link rel="alternate" type="application/rss+xml" title="RSS" href="$rssurl" />};
  144. }
  145. return $ret;
  146. } #}}}
  147. sub pagetemplate_inline (@) { #{{{
  148. my %params=@_;
  149. my $page=$params{page};
  150. my $template=$params{template};
  151. $template->param(rsslink => $rsslinks{$page})
  152. if exists $rsslinks{$page} && $template->query(name => "rsslink");
  153. } #}}}
  154. sub get_inline_content ($$) { #{{{
  155. my $page=shift;
  156. my $destpage=shift;
  157. my $file=$pagesources{$page};
  158. my $type=pagetype($file);
  159. if (defined $type) {
  160. return htmlize($page, $type,
  161. linkify($page, $destpage,
  162. preprocess($page, $destpage,
  163. filter($page,
  164. readfile(srcfile($file))))));
  165. }
  166. else {
  167. return "";
  168. }
  169. } #}}}
  170. sub date_822 ($) { #{{{
  171. my $time=shift;
  172. eval q{use POSIX};
  173. my $lc_time= POSIX::setlocale(&POSIX::LC_TIME);
  174. POSIX::setlocale(&POSIX::LC_TIME, "C");
  175. my $ret=POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  176. POSIX::setlocale(&POSIX::LC_TIME, $lc_time);
  177. return $ret;
  178. } #}}}
  179. sub absolute_urls ($$) { #{{{
  180. # sucky sub because rss sucks
  181. my $content=shift;
  182. my $url=shift;
  183. $url=~s/[^\/]+$//;
  184. $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
  185. $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
  186. return $content;
  187. } #}}}
  188. sub rsspage ($) { #{{{
  189. my $page=shift;
  190. return $page.".rss";
  191. } #}}}
  192. sub genrss ($$@) { #{{{
  193. my $desc=shift;
  194. my $page=shift;
  195. my @pages=@_;
  196. my $url=URI->new(encode_utf8($config{url}."/".htmlpage($page)));
  197. my $itemtemplate=template("rssitem.tmpl", blind_cache => 1);
  198. my $content="";
  199. foreach my $p (@pages) {
  200. my $u=URI->new(encode_utf8($config{url}."/".htmlpage($p)));
  201. $itemtemplate->param(
  202. title => pagetitle(basename($p)),
  203. url => $u,
  204. permalink => $u,
  205. pubdate => date_822($pagectime{$p}),
  206. content => absolute_urls(get_inline_content($p, $page), $url),
  207. );
  208. run_hooks(pagetemplate => sub {
  209. shift->(page => $p, destpage => $page,
  210. template => $itemtemplate);
  211. });
  212. $content.=$itemtemplate->output;
  213. $itemtemplate->clear_params;
  214. }
  215. my $template=template("rsspage.tmpl", blind_cache => 1);
  216. $template->param(
  217. title => $config{wikiname},
  218. wikiname => $config{wikiname},
  219. pageurl => $url,
  220. content => $content,
  221. rssdesc => $desc,
  222. );
  223. run_hooks(pagetemplate => sub {
  224. shift->(page => $page, destpage => $page,
  225. template => $template);
  226. });
  227. return $template->output;
  228. } #}}}
  229. sub pingurl (@) { #{{{
  230. return unless $config{pingurl} && %toping;
  231. eval q{require RPC::XML::Client};
  232. if ($@) {
  233. debug("RPC::XML::Client not found, not pinging");
  234. return;
  235. }
  236. foreach my $page (keys %toping) {
  237. my $title=pagetitle(basename($page));
  238. my $url="$config{url}/".htmlpage($page);
  239. foreach my $pingurl (@{$config{pingurl}}) {
  240. my $client = RPC::XML::Client->new($pingurl);
  241. my $req = RPC::XML::request->new('weblogUpdates.ping',
  242. $title, $url);
  243. debug("Pinging $pingurl for $page");
  244. my $res = $client->send_request($req);
  245. if (! ref $res) {
  246. debug("Did not receive response to ping");
  247. }
  248. my $r=$res->value;
  249. if (! exists $r->{flerror} || $r->{flerror}) {
  250. debug("Ping rejected: ".(exists $r->{message} ? $r->{message} : "[unknown reason]"));
  251. }
  252. }
  253. }
  254. } #}}}
  255. 1