]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/Set_arbitrary_date_to_be_used_by_calendar_plugin.mdwn
questions
[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 > Thanks for coming up with a patch.. Let me make sure I understand its
10 > rationalle.
11
12 > The meta plugin already allows modifying the page creation time,
13 > which is what the calendar plugin uses.
14
15 > So, it seems to me that the use of this patch is for recording events in
16 > the future. You'd not want a page for a future event to claim it was
17 > created in the future. I suppose you could also use it for events in the
18 > past, if you didn't want to change the creation time for some reason.
19 > (Perhaps you're doing a calendar of historical events, for example.)
20 >
21 > Accurate? --[[Joey]]
22
23     --- calendar.pm.orig  2008-06-24 22:36:09.000000000 -0400
24     +++ calendar.pm 2008-06-24 22:51:11.000000000 -0400
25     @@ -23,6 +23,7 @@
26      use IkiWiki 2.00;
27      use Time::Local;
28      use POSIX;
29     +use Date::Parse;
30   
31      my %cache;
32      my %linkcache;
33     @@ -32,6 +33,7 @@
34      sub import { #{{{
35       hook(type => "needsbuild", id => "version", call => \&needsbuild);
36       hook(type => "preprocess", id => "calendar", call => \&preprocess);
37     + hook(type => "preprocess", id => "event", call => \&preprocess_event);
38      } #}}}
39   
40      sub is_leap_year (@) { #{{{
41     @@ -304,6 +306,19 @@
42       return $calendar;
43      } #}}}
44   
45     +sub preprocess_event (@) { #{{{
46     + my %params=@_;
47     + # if now time is given, use now
48     + $params{time} = localtime            unless defined $params{time};
49     +
50     + my $timestamp = str2time($params{time});
51     + if ( defined $timestamp) {
52     +   $pagestate{$params{page}}{event}{mtime}=$timestamp;
53     + }
54     + # remove the event block entirely
55     + return "";
56     +} #}}
57     +
58      sub preprocess (@) { #{{{
59       my %params=@_;
60       $params{pages} = "*"            unless defined $params{pages};
61     @@ -355,7 +370,13 @@
62       if (! defined $cache{$pagespec}) {
63         foreach my $p (keys %pagesources) {
64           next unless pagespec_match($p, $pagespec);
65     -     my $mtime = $IkiWiki::pagectime{$p};
66     +     my $mtime;
67     +     # use time defined by event preprocessor if it's available
68     +     if (defined $pagestate{$p}{event}{mtime}) {
69     +       $mtime = $pagestate{$p}{event}{mtime};
70     +     } else {
71     +       $mtime = $IkiWiki::pagectime{$p};
72     +     }
73           my $src   = $pagesources{$p};
74           my @date  = localtime($mtime);
75           my $mday  = $date[3];