summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/inline.pm
blob: 61b4a8523f2dfaf9156238b0ab54adedc9f77b42 (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. } # }}}
  11. # Back to ikiwiki namespace for the rest, this code is very much
  12. # internal to ikiwiki even though it's separated into a plugin.
  13. package IkiWiki;
  14. sub preprocess_inline (@) { #{{{
  15. my %params=@_;
  16. if (! exists $params{pages}) {
  17. return "";
  18. }
  19. if (! exists $params{archive}) {
  20. $params{archive}="no";
  21. }
  22. if (! exists $params{show} && $params{archive} eq "no") {
  23. $params{show}=10;
  24. }
  25. add_depends($params{page}, $params{pages});
  26. my $ret="";
  27. if (exists $params{rootpage}) {
  28. # Add a blog post form, with a rss link button.
  29. my $formtemplate=HTML::Template->new(blind_cache => 1,
  30. filename => "$config{templatedir}/blogpost.tmpl");
  31. $formtemplate->param(cgiurl => $config{cgiurl});
  32. $formtemplate->param(rootpage => $params{rootpage});
  33. if ($config{rss}) {
  34. $formtemplate->param(rssurl => rsspage(basename($params{page})));
  35. }
  36. $ret.=$formtemplate->output;
  37. }
  38. elsif ($config{rss}) {
  39. # Add a rss link button.
  40. my $linktemplate=HTML::Template->new(blind_cache => 1,
  41. filename => "$config{templatedir}/rsslink.tmpl");
  42. $linktemplate->param(rssurl => rsspage(basename($params{page})));
  43. $ret.=$linktemplate->output;
  44. }
  45. my $template=HTML::Template->new(blind_cache => 1,
  46. filename => (($params{archive} eq "no")
  47. ? "$config{templatedir}/inlinepage.tmpl"
  48. : "$config{templatedir}/inlinepagetitle.tmpl"));
  49. my @pages;
  50. foreach my $page (blog_list($params{pages}, $params{show})) {
  51. next if $page eq $params{page};
  52. push @pages, $page;
  53. $template->param(pagelink => htmllink($params{page}, $page));
  54. $template->param(content => get_inline_content($params{page}, $page))
  55. if $params{archive} eq "no";
  56. $template->param(ctime => scalar(gmtime($pagectime{$page})));
  57. $ret.=$template->output;
  58. }
  59. # TODO: should really add this to renderedfiles and call
  60. # check_overwrite, but currently renderedfiles
  61. # only supports listing one file per page.
  62. if ($config{rss}) {
  63. writefile(rsspage($params{page}), $config{destdir},
  64. genrss($params{page}, @pages));
  65. }
  66. return $ret;
  67. } #}}}
  68. sub blog_list ($$) { #{{{
  69. my $globlist=shift;
  70. my $maxitems=shift;
  71. my @list;
  72. foreach my $page (keys %pagesources) {
  73. if (globlist_match($page, $globlist)) {
  74. push @list, $page;
  75. }
  76. }
  77. @list=sort { $pagectime{$b} <=> $pagectime{$a} } @list;
  78. return @list if ! $maxitems || @list <= $maxitems;
  79. return @list[0..$maxitems - 1];
  80. } #}}}
  81. sub get_inline_content ($$) { #{{{
  82. my $parentpage=shift;
  83. my $page=shift;
  84. my $file=$pagesources{$page};
  85. my $type=pagetype($file);
  86. if ($type ne 'unknown') {
  87. return htmlize($type, linkify(readfile(srcfile($file)), $parentpage));
  88. }
  89. else {
  90. return "";
  91. }
  92. } #}}}
  93. sub date_822 ($) { #{{{
  94. my $time=shift;
  95. eval q{use POSIX};
  96. return POSIX::strftime("%a, %d %b %Y %H:%M:%S %z", localtime($time));
  97. } #}}}
  98. sub absolute_urls ($$) { #{{{
  99. # sucky sub because rss sucks
  100. my $content=shift;
  101. my $url=shift;
  102. $url=~s/[^\/]+$//;
  103. $content=~s/<a\s+href="(?!http:\/\/)([^"]+)"/<a href="$url$1"/ig;
  104. $content=~s/<img\s+src="(?!http:\/\/)([^"]+)"/<img src="$url$1"/ig;
  105. return $content;
  106. } #}}}
  107. sub rsspage ($) { #{{{
  108. my $page=shift;
  109. return $page.".rss";
  110. } #}}}
  111. sub genrss ($@) { #{{{
  112. my $page=shift;
  113. my @pages=@_;
  114. my $url="$config{url}/".htmlpage($page);
  115. my $template=HTML::Template->new(blind_cache => 1,
  116. filename => "$config{templatedir}/rsspage.tmpl");
  117. my @items;
  118. foreach my $p (@pages) {
  119. push @items, {
  120. itemtitle => pagetitle(basename($p)),
  121. itemurl => "$config{url}/$renderedfiles{$p}",
  122. itempubdate => date_822($pagectime{$p}),
  123. itemcontent => absolute_urls(get_inline_content($page, $p), $url),
  124. } if exists $renderedfiles{$p};
  125. }
  126. $template->param(
  127. title => $config{wikiname},
  128. pageurl => $url,
  129. items => \@items,
  130. );
  131. return $template->output;
  132. } #}}}
  133. 1