web commit by https://id.mayfirst.org/jamie/
[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 >> Thanks for the feedback. Thinking about what you said ... I suspect my patch 
24 >> doesn't belong in the calendar plugin, which does a very specific thing 
25 >> (create a calendar to show when blog posts were created). I'm really angling 
26 >> toward an event calendar (as mentioned on [[todo/plugin]]). I'd like to preserve 
27 >> the page creation time - which is useful and important information in its own right 
28 >> - and be able to generate a calendar with links to particular posts that will show 
29 >> up on the calendar based on an arbitrary date. Perhaps this should be re-considered 
30 >> as a separate plugin? --[[Jamie]]
31
32 >>> I think it makes sense to have only one calendar, if possible.
33 >>> I think your event stuff is fine, the only thing we might want to add
34 >>> is a config option for the calendar, to control whether it looks at the
35 >>> event date, or the creation date. --[[Joey]]
36
37 >>>> Ok - I can work on that. One question - the existing calendar module has it's own 
38 >>>> functions for building an html display of a calendar. HTML::CalendarMonth seems to 
39 >>>> provide that functionality. My instincts are to rip out the code in the calendar plugin
40 >>>> and use the existing module. On the other hand, that creates added dependencies. 
41 >>>> Suggestions anyone? --[[Jamie]]
42
43
44     --- calendar.pm.orig  2008-06-24 22:36:09.000000000 -0400
45     +++ calendar.pm 2008-06-24 22:51:11.000000000 -0400
46     @@ -23,6 +23,7 @@
47      use IkiWiki 2.00;
48      use Time::Local;
49      use POSIX;
50     +use Date::Parse;
51   
52      my %cache;
53      my %linkcache;
54     @@ -32,6 +33,7 @@
55      sub import { #{{{
56       hook(type => "needsbuild", id => "version", call => \&needsbuild);
57       hook(type => "preprocess", id => "calendar", call => \&preprocess);
58     + hook(type => "preprocess", id => "event", call => \&preprocess_event);
59      } #}}}
60   
61      sub is_leap_year (@) { #{{{
62     @@ -304,6 +306,19 @@
63       return $calendar;
64      } #}}}
65   
66     +sub preprocess_event (@) { #{{{
67     + my %params=@_;
68     + # if now time is given, use now
69     + $params{time} = localtime            unless defined $params{time};
70     +
71     + my $timestamp = str2time($params{time});
72     + if ( defined $timestamp) {
73     +   $pagestate{$params{page}}{event}{mtime}=$timestamp;
74     + }
75     + # remove the event block entirely
76     + return "";
77     +} #}}
78     +
79      sub preprocess (@) { #{{{
80       my %params=@_;
81       $params{pages} = "*"            unless defined $params{pages};
82     @@ -355,7 +370,13 @@
83       if (! defined $cache{$pagespec}) {
84         foreach my $p (keys %pagesources) {
85           next unless pagespec_match($p, $pagespec);
86     -     my $mtime = $IkiWiki::pagectime{$p};
87     +     my $mtime;
88     +     # use time defined by event preprocessor if it's available
89     +     if (defined $pagestate{$p}{event}{mtime}) {
90     +       $mtime = $pagestate{$p}{event}{mtime};
91     +     } else {
92     +       $mtime = $IkiWiki::pagectime{$p};
93     +     }
94           my $src   = $pagesources{$p};
95           my @date  = localtime($mtime);
96           my $mday  = $date[3];