summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 05563b9464fc1cf21b65710d541d40bb124169cb (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=HTML::Template->new(blind_cache => 1,
  36. filename => "$config{templatedir}/blogpost.tmpl");
  37. $formtemplate->param(cgiurl => $config{cgiurl});
  38. $formtemplate->param(rootpage => $params{rootpage});
  39. if ($config{rss}) {
  40. $formtemplate->param(rssurl => rsspage(basename($params{page})));
  41. }
  42. $ret.=$formtemplate->output;
  43. }
  44. elsif ($config{rss}) {
  45. # Add a rss link button.
  46. my $linktemplate=HTML::Template->new(blind_cache => 1,
  47. filename => "$config{templatedir}/rsslink.tmpl");
  48. $linktemplate->param(rssurl => rsspage(basename($params{page})));
  49. $ret.=$linktemplate->output;
  50. }
  51. my $template=HTML::Template->new(blind_cache => 1,
  52. filename => (($params{archive} eq "no")
  53. ? "$config{templatedir}/inlinepage.tmpl"
  54. : "$config{templatedir}/inlinepagetitle.tmpl"));
  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=HTML::Template->new(blind_cache => 1,
  123. filename => "$config{templatedir}/rsspage.tmpl");
  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