summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 06c4a3737a7d935a5c5a86e54e8067d15d21bd30 (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. my @items;
  129. foreach my $p (@pages) {
  130. push @items, {
  131. itemtitle => pagetitle(basename($p)),
  132. itemurl => "$config{url}/$renderedfiles{$p}",
  133. itempubdate => date_822($pagectime{$p}),
  134. itemcontent => absolute_urls(get_inline_content($p, $page), $url),
  135. } if exists $renderedfiles{$p};
  136. }
  137. $template->param(
  138. title => $config{wikiname},
  139. pageurl => $url,
  140. items => \@items,
  141. );
  142. return $template->output;
  143. } #}}}
  144. sub pingurl (@) { #{{{
  145. return unless $config{pingurl} && %toping;
  146. eval q{require RPC::XML::Client};
  147. if ($@) {
  148. debug("RPC::XML::Client not found, not pinging");
  149. return;
  150. }
  151. foreach my $page (keys %toping) {
  152. my $title=pagetitle(basename($page));
  153. my $url="$config{url}/".htmlpage($page);
  154. foreach my $pingurl (@{$config{pingurl}}) {
  155. my $client = RPC::XML::Client->new($pingurl);
  156. my $req = RPC::XML::request->new('weblogUpdates.ping',
  157. $title, $url);
  158. debug("Pinging $pingurl for $page");
  159. my $res = $client->send_request($req);
  160. if (! ref $res) {
  161. debug("Did not receive response to ping");
  162. }
  163. my $r=$res->value;
  164. if (! exists $r->{flerror} || $r->{flerror}) {
  165. debug("Ping rejected: ".$r->{message});
  166. }
  167. }
  168. }
  169. } #}}}
  170. 1