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