web commit by https://id.mayfirst.org/jamie/: Moving discussion to discussion page.
[ikiwiki.git] / doc / todo / Set_arbitrary_date_to_be_used_by_calendar_plugin.mdwn
1 [[tag patch]]
2
3 Here's a patch to the calendar plugin. If you specify an event preprocessor in a post, such as:
4
5     [[event time="2008-06-24"]]
6
7 That date will be used instead of the post creation time when displaying the calendar.
8
9     --- calendar.pm.orig  2008-06-24 22:36:09.000000000 -0400
10     +++ calendar.pm 2008-06-24 22:51:11.000000000 -0400
11     @@ -23,6 +23,7 @@
12      use IkiWiki 2.00;
13      use Time::Local;
14      use POSIX;
15     +use Date::Parse;
16   
17      my %cache;
18      my %linkcache;
19     @@ -32,6 +33,7 @@
20      sub import { #{{{
21       hook(type => "needsbuild", id => "version", call => \&needsbuild);
22       hook(type => "preprocess", id => "calendar", call => \&preprocess);
23     + hook(type => "preprocess", id => "event", call => \&preprocess_event);
24      } #}}}
25   
26      sub is_leap_year (@) { #{{{
27     @@ -304,6 +306,19 @@
28       return $calendar;
29      } #}}}
30   
31     +sub preprocess_event (@) { #{{{
32     + my %params=@_;
33     + # if now time is given, use now
34     + $params{time} = localtime            unless defined $params{time};
35     +
36     + my $timestamp = str2time($params{time});
37     + if ( defined $timestamp) {
38     +   $pagestate{$params{page}}{event}{mtime}=$timestamp;
39     + }
40     + # remove the event block entirely
41     + return "";
42     +} #}}
43     +
44      sub preprocess (@) { #{{{
45       my %params=@_;
46       $params{pages} = "*"            unless defined $params{pages};
47     @@ -355,7 +370,13 @@
48       if (! defined $cache{$pagespec}) {
49         foreach my $p (keys %pagesources) {
50           next unless pagespec_match($p, $pagespec);
51     -     my $mtime = $IkiWiki::pagectime{$p};
52     +     my $mtime;
53     +     # use time defined by event preprocessor if it's available
54     +     if (defined $pagestate{$p}{event}{mtime}) {
55     +       $mtime = $pagestate{$p}{event}{mtime};
56     +     } else {
57     +       $mtime = $IkiWiki::pagectime{$p};
58     +     }
59           my $src   = $pagesources{$p};
60           my @date  = localtime($mtime);
61           my $mday  = $date[3];