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