]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/calendar.pm
0266612ae4f64758861f17cb2fdf09e5f17f6448
[ikiwiki.git] / IkiWiki / Plugin / calendar.pm
1 #! /usr/bin/perl
2 # Copyright (c) 2006, 2007 Manoj Srivastava <srivasta@debian.org>
3 #
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # the Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
18 require 5.002;
19 package IkiWiki::Plugin::calendar;
20
21 use warnings;
22 use strict;
23 use IkiWiki 3.00;
24 use Time::Local;
25 use POSIX;
26
27 my $time=time;
28 my @now=localtime($time);
29
30 sub import {
31         hook(type => "getsetup", id => "calendar", call => \&getsetup);
32         hook(type => "needsbuild", id => "calendar", call => \&needsbuild);
33         hook(type => "preprocess", id => "calendar", call => \&preprocess);
34 }
35
36 sub getsetup () {
37         return
38                 plugin => {
39                         safe => 1,
40                         rebuild => undef,
41                 },
42                 archivebase => {
43                         type => "string",
44                         example => "archives",
45                         description => "base of the archives hierarchy",
46                         safe => 1,
47                         rebuild => 1,
48                 },
49 }
50
51 sub is_leap_year (@) {
52         my %params=@_;
53         return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
54 }
55
56 sub month_days {
57         my %params=@_;
58         my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
59         if ($params{month} == 2 && is_leap_year(%params)) {
60                 $days_in_month++;
61         }
62         return $days_in_month;
63 }
64
65 sub format_month (@) {
66         my %params=@_;
67
68         my %linkcache;
69         foreach my $p (pagespec_match_list($params{page}, $params{pages},
70                                 # add presence dependencies to update
71                                 # month calendar when pages are added/removed
72                                 deptype => deptype("presence"))) {
73                 my $mtime = $IkiWiki::pagectime{$p};
74                 my @date  = localtime($mtime);
75                 my $mday  = $date[3];
76                 my $month = $date[4] + 1;
77                 my $year  = $date[5] + 1900;
78                 my $mtag  = sprintf("%02d", $month);
79
80                 # Only one posting per day is being linked to.
81                 $linkcache{"$year/$mtag/$mday"} = $p;
82         }
83                 
84         my $pmonth = $params{month} - 1;
85         my $nmonth = $params{month} + 1;
86         my $pyear  = $params{year};
87         my $nyear  = $params{year};
88
89         # Adjust for January and December
90         if ($params{month} == 1) {
91                 $pmonth = 12;
92                 $pyear--;
93         }
94         if ($params{month} == 12) {
95                 $nmonth = 1;
96                 $nyear++;
97         }
98
99         my $calendar="\n";
100
101         # When did this month start?
102         my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
103
104         my $future_dom = 0;
105         my $today      = 0;
106         if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
107                 $future_dom = $now[3]+1;
108                 $today      = $now[3];
109         }
110
111         # Find out month names for this, next, and previous months
112         my $monthname=POSIX::strftime("%B", @monthstart);
113         my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
114         my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
115
116         my $archivebase = 'archives';
117         $archivebase = $config{archivebase} if defined $config{archivebase};
118         $archivebase = $params{archivebase} if defined $params{archivebase};
119   
120         # Calculate URL's for monthly archives.
121         my ($url, $purl, $nurl)=("$monthname",'','');
122         if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
123                 $url = htmllink($params{page}, $params{destpage}, 
124                         "$archivebase/$params{year}/".sprintf("%02d", $params{month}),
125                         linktext => " $monthname ");
126         }
127         add_depends($params{page}, "$archivebase/$params{year}/".sprintf("%02d", $params{month}),
128                 deptype("presence"));
129         if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
130                 $purl = htmllink($params{page}, $params{destpage}, 
131                         "$archivebase/$pyear/" . sprintf("%02d", $pmonth),
132                         linktext => " $pmonthname ");
133         }
134         add_depends($params{page}, "$archivebase/$pyear/".sprintf("%02d", $pmonth),
135                 deptype("presence"));
136         if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
137                 $nurl = htmllink($params{page}, $params{destpage}, 
138                         "$archivebase/$nyear/" . sprintf("%02d", $nmonth),
139                         linktext => " $nmonthname ");
140         }
141         add_depends($params{page}, "$archivebase/$nyear/".sprintf("%02d", $nmonth),
142                 deptype("presence"));
143
144         # Start producing the month calendar
145         $calendar=<<EOF;
146 <table class="month-calendar">
147         <caption class="month-calendar-head">
148         $purl
149         $url
150         $nurl
151         </caption>
152         <tr>
153 EOF
154
155         # Suppose we want to start the week with day $week_start_day
156         # If $monthstart[6] == 1
157         my $week_start_day = $params{week_start_day};
158
159         my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
160         my %downame;
161         my %dowabbr;
162         for my $dow ($week_start_day..$week_start_day+6) {
163                 my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
164                 my $downame = POSIX::strftime("%A", @day);
165                 my $dowabbr = POSIX::strftime("%a", @day);
166                 $downame{$dow % 7}=$downame;
167                 $dowabbr{$dow % 7}=$dowabbr;
168                 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame">$dowabbr</th>\n};
169         }
170
171         $calendar.=<<EOF;
172         </tr>
173 EOF
174
175         my $wday;
176         # we start with a week_start_day, and skip until we get to the first
177         for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
178                 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
179                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
180         }
181
182         # At this point, either the first is a week_start_day, in which case
183         # nothing has been printed, or else we are in the middle of a row.
184         for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
185              $day++, $wday++, $wday %= 7) {
186                 # At tihs point, on a week_start_day, we close out a row,
187                 # and start a new one -- unless it is week_start_day on the
188                 # first, where we do not close a row -- since none was started.
189                 if ($wday == $week_start_day) {
190                         $calendar.=qq{\t</tr>\n} unless $day == 1;
191                         $calendar.=qq{\t<tr>\n};
192                 }
193                 
194                 my $tag;
195                 my $mtag = sprintf("%02d", $params{month});
196                 if (defined $linkcache{"$params{year}/$mtag/$day"}) {
197                         if ($day == $today) {
198                                 $tag='month-calendar-day-this-day';
199                         }
200                         else {
201                                 $tag='month-calendar-day-link';
202                         }
203                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
204                         $calendar.=htmllink($params{page}, $params{destpage}, 
205                                             $linkcache{"$params{year}/$mtag/$day"},
206                                             "linktext" => "$day");
207                         $calendar.=qq{</td>\n};
208                 }
209                 else {
210                         if ($day == $today) {
211                                 $tag='month-calendar-day-this-day';
212                         }
213                         elsif ($day == $future_dom) {
214                                 $tag='month-calendar-day-future';
215                         }
216                         else {
217                                 $tag='month-calendar-day-nolink';
218                         }
219                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
220                 }
221         }
222
223         # finish off the week
224         for (; $wday != $week_start_day; $wday++, $wday %= 7) {
225                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
226         }
227         $calendar.=<<EOF;
228         </tr>
229 </table>
230 EOF
231
232         return $calendar;
233 }
234
235 sub format_year (@) {
236         my %params=@_;
237                 
238         my $calendar="\n";
239         
240         my $pyear = $params{year}  - 1;
241         my $nyear = $params{year}  + 1;
242
243         my $future_month = 0;
244         $future_month = $now[4]+1 if ($params{year} == $now[5]+1900);
245
246         my $archivebase = 'archives';
247         $archivebase = $config{archivebase} if defined $config{archivebase};
248         $archivebase = $params{archivebase} if defined $params{archivebase};
249
250         # calculate URL's for previous and next years
251         my ($url, $purl, $nurl)=("$params{year}",'','');
252         if (exists $pagesources{"$archivebase/$params{year}"}) {
253                 $url = htmllink($params{page}, $params{destpage}, 
254                         "$archivebase/$params{year}",
255                         linktext => "$params{year}");
256         }
257         add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
258         if (exists $pagesources{"$archivebase/$pyear"}) {
259                 $purl = htmllink($params{page}, $params{destpage}, 
260                         "$archivebase/$pyear",
261                         linktext => "\&larr;");
262         }
263         add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
264         if (exists $pagesources{"$archivebase/$nyear"}) {
265                 $nurl = htmllink($params{page}, $params{destpage}, 
266                         "$archivebase/$nyear",
267                         linktext => "\&rarr;");
268         }
269         add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
270
271         # Start producing the year calendar
272         $calendar=<<EOF;
273 <table class="year-calendar">
274         <caption class="year-calendar-head">
275         $purl
276         $url
277         $nurl
278         </caption>
279         <tr>
280                 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
281         </tr>
282 EOF
283
284         for (my $month = 1; $month <= 12; $month++) {
285                 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
286                 my $murl;
287                 my $monthname = POSIX::strftime("%B", @day);
288                 my $monthabbr = POSIX::strftime("%b", @day);
289                 $calendar.=qq{\t<tr>\n}  if ($month % $params{months_per_row} == 1);
290                 my $tag;
291                 my $mtag=sprintf("%02d", $month);
292                 if ($month == $params{month}) {
293                         if ($pagesources{"$archivebase/$params{year}/$mtag"}) {
294                                 $tag = 'this_month_link';
295                         }
296                         else {
297                                 $tag = 'this_month_nolink';
298                         }
299                 }
300                 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
301                         $tag = 'month_link';
302                 } 
303                 elsif ($future_month && $month >= $future_month) {
304                         $tag = 'month_future';
305                 } 
306                 else {
307                         $tag = 'month_nolink';
308                 }
309
310                 if ($pagesources{"$archivebase/$params{year}/$mtag"}) {
311                         $murl = htmllink($params{page}, $params{destpage}, 
312                                 "$archivebase/$params{year}/$mtag",
313                                 linktext => "$monthabbr");
314                         $calendar.=qq{\t<td class="$tag">};
315                         $calendar.=$murl;
316                         $calendar.=qq{\t</td>\n};
317                 }
318                 else {
319                         $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
320                 }
321                 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
322                         deptype("presence"));
323
324                 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
325         }
326
327         $calendar.=<<EOF;
328 </table>
329 EOF
330
331         return $calendar;
332 }
333
334 sub preprocess (@) {
335         my %params=@_;
336
337         my $thisyear=1900 + $now[5];
338         my $thismonth=1 + $now[4];
339
340         $params{pages} = "*"            unless defined $params{pages};
341         $params{type}  = "month"        unless defined $params{type};
342         $params{week_start_day} = 0     unless defined $params{week_start_day};
343         $params{months_per_row} = 3     unless defined $params{months_per_row};
344         $params{year}  = $thisyear      unless defined $params{year};
345         $params{month} = $thismonth     unless defined $params{month};
346
347         $params{month} = sprintf("%02d", $params{month});
348                         
349         if ($params{type} eq 'month' && $params{year} == $thisyear
350             && $params{month} == $thismonth) {
351                 # calendar for current month, updates next midnight
352                 $pagestate{$params{destpage}}{calendar}{nextchange}=($time
353                         + (60 - $now[0])                # seconds
354                         + (59 - $now[1]) * 60           # minutes
355                         + (23 - $now[2]) * 60 * 60      # hours
356                 );
357         }
358         elsif ($params{type} eq 'month' &&
359                (($params{year} == $thisyear && $params{month} > $thismonth) ||
360                 $params{year} > $thisyear)) {
361                 # calendar for upcoming month, updates 1st of that month
362                 $pagestate{$params{destpage}}{calendar}{nextchange}=
363                         timelocal(0, 0, 0, 1, $params{month}-1, $params{year});
364         }
365         elsif ($params{type} eq 'year' && $params{year} == $thisyear) {
366                 # calendar for current year, updates 1st of next month
367                 $pagestate{$params{destpage}}{calendar}{nextchange}=
368                         timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year});
369         }
370         elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
371                 # calendar for upcoming year, updates 1st of that year
372                 $pagestate{$params{destpage}}{calendar}{nextchange}=
373                         timelocal(0, 0, 0, 1, 1-1, $params{year});
374         }
375         else {
376                 # calendar for past month or year, does not need
377                 # to update any more
378                 delete $pagestate{$params{destpage}}{calendar};
379         }
380
381         # Calculate month names for next month, and previous months
382         my $calendar="";
383         if ($params{type} eq 'month') {
384                 $calendar=format_month(%params);
385         }
386         elsif ($params{type} eq 'year') {
387                 $calendar=format_year(%params);
388         }
389
390         return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
391 } #}}
392
393 sub needsbuild (@) {
394         my $needsbuild=shift;
395         foreach my $page (keys %pagestate) {
396                 if (exists $pagestate{$page}{calendar}{nextchange}) {
397                         if ($pagestate{$page}{calendar}{nextchange} <= $time) {
398                                 # force a rebuild so the calendar shows
399                                 # the current day
400                                 push @$needsbuild, $pagesources{$page};
401                         }
402                         if (exists $pagesources{$page} && 
403                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
404                                 # remove state, will be re-added if
405                                 # the calendar is still there during the
406                                 # rebuild
407                                 delete $pagestate{$page}{calendar};
408                         }
409                 }
410         }
411 }
412
413 1