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