]> sipb.mit.edu Git - ikiwiki.git/blob - doc/todo/Set_arbitrary_date_to_be_used_by_calendar_plugin.mdwn
note that the master branch also ahs this
[ikiwiki.git] / doc / todo / Set_arbitrary_date_to_be_used_by_calendar_plugin.mdwn
1 [[!tag patch plugins/calendar]]
2
3 Here's my next version of the patch - still a work in progress.
4
5   Note:I partially updated part of this patch to work on Ikiwiki v3 - see [here](http://ikiwiki.info/forum/Calendar:_listing_multiple_entries_per_day/) -- Matt Ford
6
7 It provides the following new features. The features are designed to preserve the behavior of the existing plugin by default.
8
9  * If you specify an event preprocessor in a post, such as:
10
11     [[!event time="2008-06-24"]]
12
13  That date will be used instead of the post creation time when displaying the calendar.
14
15  * When specifying a calendar preprocessor, you can now add the following new parameters:
16
17   * time_src: by default it is set to auto. auto means that when determining the date to use for a given post, take the date given in the event preprocessor directive if it exists and if not, use the creation time of the post. The other option is event which means: take the date given in the event preprocessor directive. If there is not event preprocessor in a post, don't include the post.
18   * detail: by default it is set to 0. If set to 0, display the monthly calendar as a series of days. Each day is a link to the post on that day. If there is more than one post on a day, only one is linked to. If set to 1 - then display the title of each post on the day as a link to the post. Is 0 and 1 the best values here? On/Off? Yes/no?
19
20 The following changes are in the works:
21
22  * Switch to using HTML::CalendarMonth to create the html output
23
24  * Display the start time when detail is set to 1. 
25
26 Longer term plans:
27
28  * Support recurring events
29  
30  * Support for end time on events (including end time that is on a different day than the start time)
31
32  * Convincing the world to switch to base 10 calendar system.
33
34
35                 --- calendar.pm.orig    2008-06-24 22:36:09.000000000 -0400
36                 +++ calendar.pm 2008-06-28 22:02:15.000000000 -0400
37                 @@ -23,6 +23,8 @@
38                  use IkiWiki 2.00;
39                  use Time::Local;
40                  use POSIX;
41                 +use Date::Parse;
42                 +use Data::Dumper;
43                  
44                  my %cache;
45                  my %linkcache;
46                 @@ -32,6 +34,7 @@
47                  sub import {
48                         hook(type => "needsbuild", id => "version", call => \&needsbuild);
49                         hook(type => "preprocess", id => "calendar", call => \&preprocess);
50                 +       hook(type => "preprocess", id => "event", call => \&preprocess_event);
51                  }
52                  
53                  sub is_leap_year (@) {
54                 @@ -58,6 +61,7 @@
55                         my $nmonth   = $params{nmonth};
56                         my $pyear    = $params{pyear};
57                         my $nyear    = $params{nyear};
58                 +       my $detail   = $params{detail};
59                  
60                         my @list;
61                         my $calendar="\n";
62                 @@ -153,33 +157,58 @@
63                                 }
64                                 
65                                 my $tag;
66                 +               my $display_day;
67                                 my $mtag = sprintf("%02d", $month);
68                 -               if (defined $cache{$pagespec}{"$year/$mtag/$day"}) {
69                 -                       if ($day == $today) {
70                 +               if ($day == $today) {
71                                                 $tag='month-calendar-day-this-day';
72                 -                       }
73                 -                       else {
74                 -                               $tag='month-calendar-day-link';
75                 -                       }
76                 -                       $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
77                 -                       $calendar.=htmllink($params{page}, $params{destpage}, 
78                 -                                           pagename($linkcache{"$year/$mtag/$day"}),
79                 -                                           "linktext" => "$day");
80                 -                       push @list, pagename($linkcache{"$year/$mtag/$day"});
81                 -                       $calendar.=qq{</td>\n};
82                 +               }
83                 +               elsif ($day >= $future_dom) {
84                 +                       $tag='month-calendar-day-future';
85                 +               }
86                 +               elsif($params{detail} == 0 && 
87                 +                       !defined $cache{$pagespec}{"$year/$mtag/$day"}) {
88                 +                       $tag='month-calendar-day-nolink';
89                 +               } 
90                 +               elsif($params{detail} == 0 && 
91                 +                       defined $cache{$pagespec}{"$year/$mtag/$day"}) {
92                 +                       $tag='month-calendar-day-link';
93                                 }
94                                 else {
95                 -                       if ($day == $today) {
96                 -                               $tag='month-calendar-day-this-day';
97                 -                       }
98                 -                       elsif ($day == $future_dom) {
99                 -                               $tag='month-calendar-day-future';
100                 +                       $tag='month-calendar-day';
101                 +               }
102                 +
103                 +               $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
104                 +               my $day_label = qq{<span class="month-calendar-day-label">$day</span>}; 
105                 +               if (defined $cache{$pagespec}{"$year/$mtag/$day"}) {
106                 +                       my $srcpage; my $destpage;
107                 +                       if($params{detail} == 0) {
108                 +                               # pull off the first page
109                 +                               (($srcpage,$destpage) = each(%{$linkcache{"$year/$mtag/$day"}}));
110                 +                               $calendar.=htmllink($params{page}, $params{destpage}, 
111                 +                                           pagename($destpage),
112                 +                                           "linktext" => "$day_label");
113                 +                               push @list, pagename($linkcache{"$year/$mtag/$day"});
114                                         }
115                                         else {
116                 -                               $tag='month-calendar-day-nolink';
117                 +                               $calendar.=qq{$day_label\n};
118                 +                               while(($srcpage,$destpage) = each(%{$linkcache{"$year/$mtag/$day"}})) {
119                 +                                       my $title = IkiWiki::basename(pagename($srcpage));
120                 +                                       if (exists $pagestate{$srcpage}{meta}{title} ) {
121                 +                                               $title = $pagestate{$srcpage}{meta}{title};
122                 +                                       }
123                 +                                       $calendar.=qq{\t\t<div class="$tag $downame{$wday}">};
124                 +                                       $calendar.=htmllink($params{page}, $params{destpage}, 
125                 +                                                                                                                       pagename($destpage),
126                 +                                                                                                                       "linktext" => $title);
127                 +                                       push @list, pagename($linkcache{"$year/$mtag/$day"}{"$srcpage"});
128                 +                                       $calendar.=qq{\t\t</div>};
129                 +                               }
130                                         }
131                 -                       $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
132                                 }
133                 +               else {
134                 +                       $calendar.=qq{$day_label\n};
135                 +               }
136                 +               $calendar.=qq{</td>\n};
137                         }
138                  
139                         # finish off the week
140                 @@ -304,6 +333,18 @@
141                         return $calendar;
142                  }
143                  
144                 +sub preprocess_event (@) {
145                 +       my %params=@_;
146                 +       # if now time is given, use now
147                 +       $params{begin} = localtime($time)            unless defined $params{begin};
148                 +
149                 +       my $timestamp = str2time($params{begin});
150                 +       if ( defined $timestamp) {
151                 +               $pagestate{$params{page}}{event}{begin}=$timestamp;
152                 +       }
153                 +       return "<!-- $params{begin} -->";
154                 +} #}}
155                 +
156                  sub preprocess (@) {
157                         my %params=@_;
158                         $params{pages} = "*"            unless defined $params{pages};
159                 @@ -311,6 +352,8 @@
160                         $params{month} = sprintf("%02d", $params{month}) if defined  $params{month};
161                         $params{week_start_day} = 0     unless defined $params{week_start_day};
162                         $params{months_per_row} = 3     unless defined $params{months_per_row};
163                 +       $params{time_src} = 'auto'      unless defined $params{time_src};
164                 +       $params{detail} = 0                                   unless defined $params{detail};
165                  
166                         if (! defined $params{year} || ! defined $params{month}) {
167                                 # Record that the calendar next changes at midnight.
168                 @@ -355,19 +398,29 @@
169                         if (! defined $cache{$pagespec}) {
170                                 foreach my $p (keys %pagesources) {
171                                         next unless pagespec_match($p, $pagespec);
172                 -                       my $mtime = $IkiWiki::pagectime{$p};
173                 -                       my $src   = $pagesources{$p};
174                 -                       my @date  = localtime($mtime);
175                 -                       my $mday  = $date[3];
176                 -                       my $month = $date[4] + 1;
177                 -                       my $year  = $date[5] + 1900;
178                 -                       my $mtag  = sprintf("%02d", $month);
179                 -
180                 -                       # Only one posting per day is being linked to.
181                 -                       $linkcache{"$year/$mtag/$mday"} = "$src";
182                 -                       $cache{$pagespec}{"$year"}++;
183                 -                       $cache{$pagespec}{"$year/$mtag"}++;
184                 -                       $cache{$pagespec}{"$year/$mtag/$mday"}++;
185                 +                       my $begin = '';
186                 +                       # use time defined by event preprocessor if it's available
187                 +                       if (defined $pagestate{$p}{event}{begin}) {
188                 +                               $begin = $pagestate{$p}{event}{begin};
189                 +                       # fall back on ctime if time_src is set to auto
190                 +                       # set time_src to 'event' to skip posts that don't
191                 +                       # have the event preprocessor
192                 +                       } elsif ($params{time_src} eq 'auto') {
193                 +                               $begin = $IkiWiki::pagectime{$p};
194                 +                       }
195                 +                       if($begin ne '') {
196                 +                               my $dest   = $pagesources{$p};
197                 +                               my @date  = localtime($begin);
198                 +                               my $mday  = $date[3];
199                 +                               my $month = $date[4] + 1;
200                 +                               my $year  = $date[5] + 1900;
201                 +                               my $mtag  = sprintf("%02d", $month);
202                 +
203                 +                               $linkcache{"$year/$mtag/$mday"}{$p} = "$dest";
204                 +                               $cache{$pagespec}{"$year"}++;
205                 +                               $cache{$pagespec}{"$year/$mtag"}++;
206                 +                               $cache{$pagespec}{"$year/$mtag/$mday"}++;
207                 +                       }
208                                 }
209                         }
210