* Add a calendar plugin, contributed by Manoj Srivastava.
[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 2.00;
24 use Time::Local;
25
26 my %cache;
27 my %linkcache;
28 my @now=localtime();
29
30 sub import { #{{{
31         hook(type => "preprocess", id => "calendar", call => \&preprocess);
32 } #}}}
33
34 sub is_leap_year (@) { #{{
35         my %params=@_;
36         return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
37 } #}}}
38
39 sub month_days { #{{{
40         my %params=@_;
41         my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
42         if ($params{month} == 2 && is_leap_year(%params)) {
43                 $days_in_month++;
44         }
45         return $days_in_month;
46 } #}}}
47
48 sub format_month (@) { #{{{
49         my %params=@_;
50
51         my $pagespec = $params{pages};
52         my $year     = $params{year};
53         my $month    = $params{month};
54         my $pmonth   = $params{pmonth};
55         my $nmonth   = $params{nmonth};
56         my $pyear    = $params{pyear};
57         my $nyear    = $params{nyear};
58
59         my @list;
60         my $calendar="\n";
61
62         # When did this month start?
63         my @monthstart = localtime(timelocal(0,0,0,1,$month-1,$year-1900));
64
65         my $future_dom = 0;
66         my $today      = 0;
67         if ($year == $now[5]+1900 && $month == $now[4]+1) {
68                 $future_dom = $now[3]+1;
69                 $today      = $now[3];
70         }
71
72         # Find out month names for this, next, and previous months
73         my $monthname=POSIX::strftime("%B", @monthstart);
74         my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
75         my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
76
77         my $archivebase = 'archives';
78         $archivebase = $config{archivebase} if defined $config{archivebase};
79         $archivebase = $params{archivebase} if defined $params{archivebase};
80   
81         # Calculate URL's for monthly archives.
82         my ($url, $purl, $nurl)=("$monthname",'','');
83         if (exists $cache{$pagespec}{"$year/$month"}) {
84                 $url = htmllink($params{page}, $params{destpage}, 
85                         "$archivebase/$year/".sprintf("%02d", $month),
86                         linktext => " $monthname ");
87         }
88         add_depends($params{page}, "$archivebase/$year/".sprintf("%02d", $month));
89         if (exists $cache{$pagespec}{"$pyear/$pmonth"}) {
90                 $purl = htmllink($params{page}, $params{destpage}, 
91                         "$archivebase/$pyear/" . sprintf("%02d", $pmonth),
92                         linktext => " $pmonthname ");
93         }
94         add_depends($params{page}, "$archivebase/$pyear/".sprintf("%02d", $pmonth));
95         if (exists $cache{$pagespec}{"$nyear/$nmonth"}) {
96                 $nurl = htmllink($params{page}, $params{destpage}, 
97                         "$archivebase/$nyear/" . sprintf("%02d", $nmonth),
98                         linktext => " $nmonthname ");
99         }
100         add_depends($params{page}, "$archivebase/$nyear/".sprintf("%02d", $nmonth));
101
102         # Start producing the month calendar
103         $calendar=<<EOF;
104 <table class="month-calendar">
105         <caption class="month-calendar-head">
106         $purl
107         $url
108         $nurl
109         </caption>
110         <tr>
111 EOF
112
113         # Suppose we want to start the week with day $week_start_day
114         # If $monthstart[6] == 1
115         my $week_start_day = $params{week_start_day};
116
117         my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
118         my %downame;
119         my %dowabbr;
120         for my $dow ($week_start_day..$week_start_day+6) {
121                 my @day=localtime(timelocal(0,0,0,$start_day++,$month-1,$year-1900));
122                 my $downame = POSIX::strftime("%A", @day);
123                 my $dowabbr = POSIX::strftime("%a", @day);
124                 $downame{$dow % 7}=$downame;
125                 $dowabbr{$dow % 7}=$dowabbr;
126                 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame">$dowabbr</th>\n};
127         }
128
129         $calendar.=<<EOF;
130         </tr>
131 EOF
132
133         my $wday;
134         # we start with a week_start_day, and skip until we get to the first
135         for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
136                 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
137                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
138         }
139
140         # At this point, either the first is a week_start_day, in which case
141         # nothing has been printed, or else we are in the middle of a row.
142         for (my $day = 1; $day <= month_days(year => $year, month => $month);
143              $day++, $wday++, $wday %= 7) {
144                 # At tihs point, on a week_start_day, we close out a row,
145                 # and start a new one -- unless it is week_start_day on the
146                 # first, where we do not close a row -- since none was started.
147                 if ($wday == $week_start_day) {
148                         $calendar.=qq{\t</tr>\n} unless $day == 1;
149                         $calendar.=qq{\t<tr>\n};
150                 }
151                 
152                 my $tag;
153                 my $mtag = sprintf("%02d", $month);
154                 if (defined $cache{$pagespec}{"$year/$mtag/$day"}) {
155                         if ($day == $today) {
156                                 $tag='month-calendar-day-this-day';
157                         }
158                         else {
159                                 $tag='month-calendar-day-link';
160                         }
161                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
162                         $calendar.=htmllink($params{page}, $params{destpage}, 
163                                             pagename($linkcache{"$year/$mtag/$day"}),
164                                             "linktext" => "$day");
165                         push @list, pagename($linkcache{"$year/$mtag/$day"});
166                         $calendar.=qq{</td>\n};
167                 }
168                 else {
169                         if ($day == $today) {
170                                 $tag='month-calendar-day-this-day';
171                         }
172                         elsif ($day == $future_dom) {
173                                 $tag='month-calendar-day-future';
174                         }
175                         else {
176                                 $tag='month-calendar-day-nolink';
177                         }
178                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
179                 }
180         }
181
182         # finish off the week
183         for (; $wday != $week_start_day; $wday++, $wday %= 7) {
184                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
185         }
186         $calendar.=<<EOF;
187         </tr>
188 </table>
189 EOF
190
191         # Add dependencies to update the calendar whenever pages
192         # matching the pagespec are added or removed.
193         add_depends($params{page}, $params{pages});
194         # Explicitly add all currently linked pages as dependencies, so
195         # that if they are removed, the calendar will be sure to be updated.
196         add_depends($params{page}, join(" or ", @list));
197
198         return $calendar;
199 }
200
201 sub format_year (@) {
202         my %params=@_;
203
204         my $pagespec = $params{pages};
205         my $year     = $params{year};
206         my $month    = $params{month};
207         my $pmonth   = $params{pmonth};
208         my $nmonth   = $params{nmonth};
209         my $pyear    = $params{pyear};
210         my $nyear    = $params{nyear};
211
212         my $calendar="\n";
213
214         my $future_month = 0;
215         $future_month = $now[4]+1 if ($year == $now[5]+1900);
216
217         my $archivebase = 'archives';
218         $archivebase = $config{archivebase} if defined $config{archivebase};
219         $archivebase = $params{archivebase} if defined $params{archivebase};
220
221         # calculate URL's for previous and next years
222         my ($url, $purl, $nurl)=("$year",'','');
223         if (exists $cache{$pagespec}{"$year"}) {
224                 $url = htmllink($params{page}, $params{destpage}, 
225                         "$archivebase/$year",
226                         linktext => "$year");
227         }
228         add_depends($params{page}, "$archivebase/$year");
229         if (exists $cache{$pagespec}{"$pyear"}) {
230                 $purl = htmllink($params{page}, $params{destpage}, 
231                         "$archivebase/$pyear",
232                         linktext => "\&larr;");
233         }
234         add_depends($params{page}, "$archivebase/$pyear");
235         if (exists $cache{$pagespec}{"$nyear"}) {
236                 $nurl = htmllink($params{page}, $params{destpage}, 
237                         "$archivebase/$nyear",
238                         linktext => "\&rarr;");
239         }
240         add_depends($params{page}, "$archivebase/$nyear");
241
242         # Start producing the year calendar
243         $calendar=<<EOF;
244 <table class="year-calendar">
245         <caption class="year-calendar-head">
246         $purl
247         $url
248         $nurl
249         </caption>
250         <tr>
251                 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
252         </tr>
253 EOF
254
255         for ($month = 1; $month <= 12; $month++) {
256                 my @day=localtime(timelocal(0,0,0,15,$month-1,$year-1900));
257                 my $murl;
258                 my $monthname = POSIX::strftime("%B", @day);
259                 my $monthabbr = POSIX::strftime("%b", @day);
260                 $calendar.=qq{\t<tr>\n}  if ($month % $params{months_per_row} == 1);
261                 my $tag;
262                 my $mtag=sprintf("%02d", $month);
263                 if ($month == $params{month}) {
264                         if ($cache{$pagespec}{"$year/$mtag"}) {
265                                 $tag = 'this_month_link';
266                         }
267                         else {
268                                 $tag = 'this_month_nolink';
269                         }
270                 }
271                 elsif ($cache{$pagespec}{"$year/$mtag"}) {
272                         $tag = 'month_link';
273                 } 
274                 elsif ($future_month && $month >= $future_month) {
275                         $tag = 'month_future';
276                 } 
277                 else {
278                         $tag = 'month_nolink';
279                 }
280
281                 if ($cache{$pagespec}{"$year/$mtag"}) {
282                         $murl = htmllink($params{page}, $params{destpage}, 
283                                 "$archivebase/$year/$mtag",
284                                 linktext => "$monthabbr");
285                         $calendar.=qq{\t<td class="$tag">};
286                         $calendar.=$murl;
287                         $calendar.=qq{\t</td>\n};
288                 }
289                 else {
290                         $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
291                 }
292                 add_depends($params{page}, "$archivebase/$year/$mtag");
293
294                 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
295         }
296
297         $calendar.=<<EOF;
298 </table>
299 EOF
300
301         return $calendar;
302 }
303
304 sub preprocess (@) {
305         my %params=@_;
306         $params{pages} = "*"            unless defined $params{pages};
307         $params{type}  = "month"        unless defined $params{type};
308         $params{year}  = 1900 + $now[5] unless defined $params{year};
309         $params{month} = sprintf("%02d", $params{month}) if defined  $params{month};
310         $params{month} = 1    + $now[4] unless defined $params{month};
311         $params{week_start_day} = 0     unless defined $params{week_start_day};
312         $params{months_per_row} = 3     unless defined $params{months_per_row};
313
314         # Calculate month names for next month, and previous months
315         my $pmonth = $params{month} - 1;
316         my $nmonth = $params{month} + 1;
317         my $pyear  = $params{year}  - 1;
318         my $nyear  = $params{year}  + 1;
319
320         # Adjust for January and December
321         if ($params{month} == 1) {
322                 $pmonth = 12;
323                 $pyear--;
324         }
325         if ($params{month} == 12) {
326                 $nmonth = 1;
327                 $nyear++;
328         }
329
330         $params{pmonth}=$pmonth;
331         $params{nmonth}=$nmonth;
332         $params{pyear} =$pyear;
333         $params{nyear} =$nyear;
334
335         my $calendar="\n";
336         my $pagespec=$params{pages};
337         my $page =$params{page};
338
339         if (! defined $cache{$pagespec}) {
340                 foreach my $p (keys %pagesources) {
341                         next unless pagespec_match($p, $pagespec);
342                         my $mtime = $IkiWiki::pagectime{$p};
343                         my $src   = $pagesources{$p};
344                         my @date  = localtime($mtime);
345                         my $mday  = $date[3];
346                         my $month = $date[4] + 1;
347                         my $year  = $date[5] + 1900;
348                         my $mtag  = sprintf("%02d", $month);
349
350                         # Only one posting per day is being linked to.
351                         $linkcache{"$year/$mtag/$mday"} = "$src";
352                         $cache{$pagespec}{"$year"}++;
353                         $cache{$pagespec}{"$year/$mtag"}++;
354                         $cache{$pagespec}{"$year/$mtag/$mday"}++;
355                 }
356         }
357
358         if ($params{type} =~ /month/i) {
359                 $calendar=format_month(%params);
360         }
361         elsif ($params{type} =~ /year/i) {
362                 $calendar=format_year(%params);
363         }
364
365         return "\n<div class=\"calendar\">$calendar</div><!-- calendar -->\n";
366 }
367
368 1