summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/recentchanges.pm
blob: 6c9848ba356213d27b8886fa716395743654bfa0 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::recentchanges;
  3. use warnings;
  4. use strict;
  5. use IkiWiki 2.00;
  6. sub import { #{{{
  7. hook(type => "needsbuild", id => "recentchanges",
  8. call => \&needsbuild);
  9. hook(type => "preprocess", id => "recentchanges",
  10. call => \&preprocess);
  11. hook(type => "htmlize", id => "_change",
  12. call => \&htmlize);
  13. } #}}}
  14. sub needsbuild () { #{{{
  15. my @changes=IkiWiki::rcs_recentchanges(100);
  16. updatechanges("*", "recentchanges", \@changes);
  17. } #}}}
  18. sub preprocess (@) { #{{{
  19. my %params=@_;
  20. # TODO
  21. return "";
  22. } #}}}
  23. # Pages with extension _change have plain html markup, pass through.
  24. sub htmlize (@) { #{{{
  25. my %params=@_;
  26. return $params{content};
  27. } #}}}
  28. sub store ($$) { #{{{
  29. my $change=shift;
  30. my $subdir=shift;
  31. my $page="$subdir/change_".IkiWiki::titlepage($change->{rev});
  32. # Optimisation to avoid re-writing pages. Assumes commits never
  33. # change (or that any changes are not important).
  34. return if exists $pagesources{$page} && ! $config{rebuild};
  35. # Limit pages to first 10, and add links to the changed pages.
  36. my $is_excess = exists $change->{pages}[10];
  37. delete @{$change->{pages}}[10 .. @{$change->{pages}}] if $is_excess;
  38. $change->{pages} = [
  39. map {
  40. if (length $config{url}) {
  41. $_->{link} = "<a href=\"$config{url}/".
  42. urlto($_->{page},"")."\">".
  43. IkiWiki::pagetitle($_->{page})."</a>";
  44. }
  45. else {
  46. $_->{link} = IkiWiki::pagetitle($_->{page});
  47. }
  48. $_;
  49. } @{$change->{pages}}
  50. ];
  51. push @{$change->{pages}}, { link => '...' } if $is_excess;
  52. # Take the first line of the commit message as a summary.
  53. #my $m=shift @{$change->{message}};
  54. #$change->{summary}=$m->{line};
  55. #delete $change->{message} unless @{$change->{message}};
  56. # See if the committer is an openid.
  57. my $oiduser=IkiWiki::openiduser($change->{user});
  58. if (defined $oiduser) {
  59. $change->{authorurl}=$change->{user};
  60. $change->{user}=$oiduser;
  61. }
  62. elsif (length $config{url}) {
  63. $change->{authorurl}="$config{url}/".
  64. (length $config{userdir} ? "$config{userdir}/" : "").
  65. $change->{user};
  66. }
  67. # Fill out a template with the change info.
  68. my $template=template("change.tmpl", blind_cache => 1);
  69. $template->param(
  70. %$change,
  71. commitdate => displaytime($change->{when}, "%X %x"),
  72. wikiname => $config{wikiname},
  73. );
  74. $template->param(baseurl => "$config{url}/") if length $config{url};
  75. IkiWiki::run_hooks(pagetemplate => sub {
  76. shift->(page => $page, destpage => $page, template => $template);
  77. });
  78. my $html=$template->output;
  79. # escape wikilinks and preprocessor stuff
  80. $html=~s/(?<!\\)\[\[/\\\[\[/g;
  81. writefile($page."._change", $config{srcdir}, $html);
  82. utime $change->{when}, $change->{when}, "$config{srcdir}/$page._change";
  83. } #}}}
  84. sub updatechanges ($$) { #{{{
  85. my $pagespec=shift;
  86. my $subdir=shift;
  87. my @changes=@{shift()};
  88. foreach my $change (@changes) {
  89. store($change, $subdir);
  90. }
  91. # TODO: delete old
  92. } #}}}
  93. 1