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