summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 7bb71a436783fe3ca57515fab67e42e39170622a (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. sub import { #{{{
  8. IkiWiki::hook(type => "preprocess", id => "inline",
  9. call => \&IkiWiki::preprocess_inline);
  10. # Hook to change to do pinging since it's called late.
  11. # This ensures each page only pings once and prevents slow
  12. # pings interrupting page builds.
  13. IkiWiki::hook(type => "change", id => "inline",
  14. call => \&IkiWiki::pingurl);
  15. } # }}}
  16. # Back to ikiwiki namespace for the rest, this code is very much
  17. # internal to ikiwiki even though it's separated into a plugin.
  18. package IkiWiki;
  19. my %toping;
  20. my $processing_inline=0;
  21. sub preprocess_inline (@) { #{{{
  22. my %params=@_;
  23. if (! exists $params{pages}) {
  24. return "";
  25. }
  26. if (! exists $params{archive}) {
  27. $params{archive}="no";
  28. }
  29. if (! exists $params{show} && $params{archive} eq "no") {
  30. $params{show}=10;
  31. }
  32. # Avoid nested inlines, to avoid loops etc.
  33. if ($processing_inline) {
  34. return "";
  35. }
  36. $processing_inline=1;
  37. my @list;
  38. foreach my $page (keys %pagesources) {
  39. next if $page eq $params{page};
  40. if (globlist_match($page, $params{pages})) {
  41. push @list, $page;
  42. }
  43. }
  44. @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
  45. if ($params{show} && @list > $params{show}) {
  46. @list=@list[0..$params{show} - 1];
  47. }
  48. add_depends($params{page}, $params{pages});
  49. my $ret="";
  50. if (exists $params{rootpage} && $config{cgiurl}) {
  51. # Add a blog post form, with a rss link button.
  52. my $formtemplate=template("blogpost.tmpl", blind_cache => 1);
  53. $formtemplate->param(cgiurl => $config{cgiurl});
  54. $formtemplate->param(rootpage => $params{rootpage});
  55. if ($config{rss}) {
  56. $formtemplate->param(rssurl => rsspage(basename($params{page})));
  57. }
  58. $ret.=$formtemplate->output;
  59. }
  60. elsif ($config{rss}) {
  61. # Add a rss link button.
  62. my $linktemplate=template("rsslink.tmpl", blind_cache => 1);
  63. $linktemplate->param(rssurl => rsspage(basename($params{page})));
  64. $ret.=$linktemplate->output;
  65. }
  66. my $template=template(
  67. (($params{archive} eq "no")
  68. ? "inlinepage.tmpl"
  69. : "inlinepagetitle.tmpl"),
  70. blind_cache => 1,
  71. );
  72. foreach my $page (@list) {
  73. $template->param(pagelink => htmllink($params{page}, $params{page}, $page));
  74. $template->param(content => get_inline_content($page, $params{page}))
  75. if $params{archive} eq "no";
  76. $template->param(ctime => displaytime($pagectime{$page}));
  77. run_hooks(pagetemplate => sub {
  78. shift->(page => $page, destpage => $params{page},
  79. template => $template,);
  80. });
  81. $ret.=$template->output;
  82. $template->clear_params;
  83. }
  84. # TODO: should really add this to renderedfiles and call
  85. # check_overwrite, but currently renderedfiles
  86. # only supports listing one file per page.
  87. if ($config{rss}) {
  88. writefile(rsspage($params{page}), $config{destdir},
  89. genrss($params{page}, @list));
  90. $toping{$params{page}}=1 unless $config{rebuild};
  91. }
  92. $processing_inline=0;
  93. return $ret;
  94. } #}}}
  95. sub get_inline_content ($$) { #{{{
  96. my $page=shift;
  97. my $destpage=shift;
  98. my $file=$pagesources{$page};
  99. my $type=pagetype($file);
  100. if (defined $type) {
  101. return htmlize($type, preprocess($page, $destpage, linkify($page, $destpage, readfile(srcfile($file)))));
  102. }
  103. else {
  104. return "";
  105. }
  106. } #}}}
  107. sub date_822 ($) { #{{{
  108. my $time=shift;
  109. eval q{use POSIX};
  110. return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  111. } #}}}
  112. sub absolute_urls ($$) { #{{{
  113. # sucky sub because rss sucks
  114. my $content=shift;
  115. my $url=shift;
  116. $url=~s/[^\/]+$//;
  117. $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
  118. $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
  119. return $content;
  120. } #}}}
  121. sub rsspage ($) { #{{{
  122. my $page=shift;
  123. return $page.".rss";
  124. } #}}}
  125. sub genrss ($@) { #{{{
  126. my $page=shift;
  127. my @pages=@_;
  128. my $url="$config{url}/".htmlpage($page);
  129. my $template=template("rsspage.tmpl", blind_cache => 1,
  130. die_on_bad_params => 0);
  131. my @items;
  132. foreach my $p (@pages) {
  133. push @items, {
  134. itemtitle => pagetitle(basename($p)),
  135. itemurl => "$config{url}/$renderedfiles{$p}",
  136. itempubdate => date_822($pagectime{$p}),
  137. itemcontent => absolute_urls(get_inline_content($p, $page), $url),
  138. page => $p, # used by category adding code in tag plugin
  139. } if exists $renderedfiles{$p};
  140. }
  141. $template->param(
  142. title => $config{wikiname},
  143. pageurl => $url,
  144. items => \@items,
  145. );
  146. run_hooks(pagetemplate => sub {
  147. shift->(page => $page, destpage => $page,
  148. template => $template);
  149. });
  150. return $template->output;
  151. } #}}}
  152. sub pingurl (@) { #{{{
  153. return unless $config{pingurl} && %toping;
  154. eval q{require RPC::XML::Client};
  155. if ($@) {
  156. debug("RPC::XML::Client not found, not pinging");
  157. return;
  158. }
  159. foreach my $page (keys %toping) {
  160. my $title=pagetitle(basename($page));
  161. my $url="$config{url}/".htmlpage($page);
  162. foreach my $pingurl (@{$config{pingurl}}) {
  163. my $client = RPC::XML::Client->new($pingurl);
  164. my $req = RPC::XML::request->new('weblogUpdates.ping',
  165. $title, $url);
  166. debug("Pinging $pingurl for $page");
  167. my $res = $client->send_request($req);
  168. if (! ref $res) {
  169. debug("Did not receive response to ping");
  170. }
  171. my $r=$res->value;
  172. if (! exists $r->{flerror} || $r->{flerror}) {
  173. debug("Ping rejected: ".$r->{message});
  174. }
  175. }
  176. }
  177. } #}}}
  178. 1