summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 427c25a96d75f8ffdd760a487ad1ae704ee80364 (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}) {
  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. if (exists $hooks{pagetemplate}) {
  78. foreach my $id (keys %{$hooks{pagetemplate}}) {
  79. $hooks{pagetemplate}{$id}{call}->(
  80. page => $page,
  81. destpage => $params{page},
  82. template => $template,
  83. );
  84. }
  85. }
  86. $ret.=$template->output;
  87. $template->clear_params;
  88. }
  89. # TODO: should really add this to renderedfiles and call
  90. # check_overwrite, but currently renderedfiles
  91. # only supports listing one file per page.
  92. if ($config{rss}) {
  93. writefile(rsspage($params{page}), $config{destdir},
  94. genrss($params{page}, @list));
  95. $toping{$params{page}}=1 unless $config{rebuild};
  96. }
  97. $processing_inline=0;
  98. return $ret;
  99. } #}}}
  100. sub get_inline_content ($$) { #{{{
  101. my $page=shift;
  102. my $destpage=shift;
  103. my $file=$pagesources{$page};
  104. my $type=pagetype($file);
  105. if (defined $type) {
  106. return htmlize($type, preprocess($page, $destpage, linkify($page, $destpage, readfile(srcfile($file)))));
  107. }
  108. else {
  109. return "";
  110. }
  111. } #}}}
  112. sub date_822 ($) { #{{{
  113. my $time=shift;
  114. eval q{use POSIX};
  115. return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  116. } #}}}
  117. sub absolute_urls ($$) { #{{{
  118. # sucky sub because rss sucks
  119. my $content=shift;
  120. my $url=shift;
  121. $url=~s/[^\/]+$//;
  122. $content=~s/<a\s+href="(?![^:]+:\/\/)([^"]+)"/<a href="$url$1"/ig;
  123. $content=~s/<img\s+src="(?![^:]+:\/\/)([^"]+)"/<img src="$url$1"/ig;
  124. return $content;
  125. } #}}}
  126. sub rsspage ($) { #{{{
  127. my $page=shift;
  128. return $page.".rss";
  129. } #}}}
  130. sub genrss ($@) { #{{{
  131. my $page=shift;
  132. my @pages=@_;
  133. my $url="$config{url}/".htmlpage($page);
  134. my $template=template("rsspage.tmpl", blind_cache => 1,
  135. die_on_bad_params => 0);
  136. my @items;
  137. foreach my $p (@pages) {
  138. push @items, {
  139. itemtitle => pagetitle(basename($p)),
  140. itemurl => "$config{url}/$renderedfiles{$p}",
  141. itempubdate => date_822($pagectime{$p}),
  142. itemcontent => absolute_urls(get_inline_content($p, $page), $url),
  143. page => $p, # used by category adding code in tag plugin
  144. } if exists $renderedfiles{$p};
  145. }
  146. $template->param(
  147. title => $config{wikiname},
  148. pageurl => $url,
  149. items => \@items,
  150. );
  151. foreach my $id (keys %{$hooks{pagetemplate}}) {
  152. $hooks{pagetemplate}{$id}{call}->(
  153. page => $page,
  154. destpage => $page,
  155. template => $template,
  156. );
  157. }
  158. return $template->output;
  159. } #}}}
  160. sub pingurl (@) { #{{{
  161. return unless $config{pingurl} && %toping;
  162. eval q{require RPC::XML::Client};
  163. if ($@) {
  164. debug("RPC::XML::Client not found, not pinging");
  165. return;
  166. }
  167. foreach my $page (keys %toping) {
  168. my $title=pagetitle(basename($page));
  169. my $url="$config{url}/".htmlpage($page);
  170. foreach my $pingurl (@{$config{pingurl}}) {
  171. my $client = RPC::XML::Client->new($pingurl);
  172. my $req = RPC::XML::request->new('weblogUpdates.ping',
  173. $title, $url);
  174. debug("Pinging $pingurl for $page");
  175. my $res = $client->send_request($req);
  176. if (! ref $res) {
  177. debug("Did not receive response to ping");
  178. }
  179. my $r=$res->value;
  180. if (! exists $r->{flerror} || $r->{flerror}) {
  181. debug("Ping rejected: ".$r->{message});
  182. }
  183. }
  184. }
  185. } #}}}
  186. 1