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