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