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