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