]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/calendar.pm
Added minimal default CSS for calendar plugin, just highlighting the current day.
[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         # Add padding.
100         $pmonth=sprintf("%02d", $pmonth);
101         $nmonth=sprintf("%02d", $nmonth);
102
103         my $calendar="\n";
104
105         # When did this month start?
106         my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
107
108         my $future_dom = 0;
109         my $today      = 0;
110         if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
111                 $future_dom = $now[3]+1;
112                 $today      = $now[3];
113         }
114
115         # Find out month names for this, next, and previous months
116         my $monthname=POSIX::strftime("%B", @monthstart);
117         my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
118         my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
119
120         my $archivebase = 'archives';
121         $archivebase = $config{archivebase} if defined $config{archivebase};
122         $archivebase = $params{archivebase} if defined $params{archivebase};
123   
124         # Calculate URL's for monthly archives.
125         my ($url, $purl, $nurl)=("$monthname",'','');
126         if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
127                 $url = htmllink($params{page}, $params{destpage}, 
128                         "$archivebase/$params{year}/".$params{month},
129                         noimageinline => 1,
130                         linktext => " $monthname ");
131         }
132         add_depends($params{page}, "$archivebase/$params{year}/$params{month}",
133                 deptype("presence"));
134         if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
135                 $purl = htmllink($params{page}, $params{destpage}, 
136                         "$archivebase/$pyear/$pmonth",
137                         noimageinline => 1,
138                         linktext => " \&larr ");
139         }
140         add_depends($params{page}, "$archivebase/$pyear/$pmonth",
141                 deptype("presence"));
142         if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
143                 $nurl = htmllink($params{page}, $params{destpage}, 
144                         "$archivebase/$nyear/$nmonth",
145                         noimageinline => 1,
146                         linktext => " \&rarr ");
147         }
148         add_depends($params{page}, "$archivebase/$nyear/$nmonth",
149                 deptype("presence"));
150
151         # Start producing the month calendar
152         $calendar=<<EOF;
153 <table class="month-calendar">
154         <caption class="month-calendar-head">
155         $purl
156         $url
157         $nurl
158         </caption>
159         <tr>
160 EOF
161
162         # Suppose we want to start the week with day $week_start_day
163         # If $monthstart[6] == 1
164         my $week_start_day = $params{week_start_day};
165
166         my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
167         my %downame;
168         my %dowabbr;
169         for my $dow ($week_start_day..$week_start_day+6) {
170                 my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
171                 my $downame = POSIX::strftime("%A", @day);
172                 my $dowabbr = POSIX::strftime("%a", @day);
173                 $downame{$dow % 7}=$downame;
174                 $dowabbr{$dow % 7}=$dowabbr;
175                 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame">$dowabbr</th>\n};
176         }
177
178         $calendar.=<<EOF;
179         </tr>
180 EOF
181
182         my $wday;
183         # we start with a week_start_day, and skip until we get to the first
184         for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
185                 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
186                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
187         }
188
189         # At this point, either the first is a week_start_day, in which case
190         # nothing has been printed, or else we are in the middle of a row.
191         for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
192              $day++, $wday++, $wday %= 7) {
193                 # At tihs point, on a week_start_day, we close out a row,
194                 # and start a new one -- unless it is week_start_day on the
195                 # first, where we do not close a row -- since none was started.
196                 if ($wday == $week_start_day) {
197                         $calendar.=qq{\t</tr>\n} unless $day == 1;
198                         $calendar.=qq{\t<tr>\n};
199                 }
200                 
201                 my $tag;
202                 if (defined $linkcache{"$params{year}/$params{month}/$day"}) {
203                         if ($day == $today) {
204                                 $tag='month-calendar-day-this-day';
205                         }
206                         else {
207                                 $tag='month-calendar-day-link';
208                         }
209                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
210                         $calendar.=htmllink($params{page}, $params{destpage}, 
211                                 $linkcache{"$params{year}/$params{month}/$day"},
212                                 noimageinline => 1,
213                                 "linktext" => "$day");
214                         $calendar.=qq{</td>\n};
215                 }
216                 else {
217                         if ($day == $today) {
218                                 $tag='month-calendar-day-this-day';
219                         }
220                         elsif ($day == $future_dom) {
221                                 $tag='month-calendar-day-future';
222                         }
223                         else {
224                                 $tag='month-calendar-day-nolink';
225                         }
226                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
227                 }
228         }
229
230         # finish off the week
231         for (; $wday != $week_start_day; $wday++, $wday %= 7) {
232                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
233         }
234         $calendar.=<<EOF;
235         </tr>
236 </table>
237 EOF
238
239         return $calendar;
240 }
241
242 sub format_year (@) {
243         my %params=@_;
244                 
245         my $calendar="\n";
246         
247         my $pyear = $params{year}  - 1;
248         my $nyear = $params{year}  + 1;
249
250         my $future_month = 0;
251         $future_month = $now[4]+1 if ($params{year} == $now[5]+1900);
252
253         my $archivebase = 'archives';
254         $archivebase = $config{archivebase} if defined $config{archivebase};
255         $archivebase = $params{archivebase} if defined $params{archivebase};
256
257         # calculate URL's for previous and next years
258         my ($url, $purl, $nurl)=("$params{year}",'','');
259         if (exists $pagesources{"$archivebase/$params{year}"}) {
260                 $url = htmllink($params{page}, $params{destpage}, 
261                         "$archivebase/$params{year}",
262                         noimageinline => 1,
263                         linktext => "$params{year}");
264         }
265         add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
266         if (exists $pagesources{"$archivebase/$pyear"}) {
267                 $purl = htmllink($params{page}, $params{destpage}, 
268                         "$archivebase/$pyear",
269                         noimageinline => 1,
270                         linktext => "\&larr;");
271         }
272         add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
273         if (exists $pagesources{"$archivebase/$nyear"}) {
274                 $nurl = htmllink($params{page}, $params{destpage}, 
275                         "$archivebase/$nyear",
276                         noimageinline => 1,
277                         linktext => "\&rarr;");
278         }
279         add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
280
281         # Start producing the year calendar
282         $calendar=<<EOF;
283 <table class="year-calendar">
284         <caption class="year-calendar-head">
285         $purl
286         $url
287         $nurl
288         </caption>
289         <tr>
290                 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
291         </tr>
292 EOF
293
294         for (my $month = 1; $month <= 12; $month++) {
295                 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
296                 my $murl;
297                 my $monthname = POSIX::strftime("%B", @day);
298                 my $monthabbr = POSIX::strftime("%b", @day);
299                 $calendar.=qq{\t<tr>\n}  if ($month % $params{months_per_row} == 1);
300                 my $tag;
301                 my $mtag=sprintf("%02d", $month);
302                 if ($month == $params{month}) {
303                         $tag = 'year-calendar-this-month';
304                 }
305                 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
306                         $tag = 'year-calendar-month-link';
307                 } 
308                 elsif ($future_month && $month >= $future_month) {
309                         $tag = 'year-calendar-month-future';
310                 } 
311                 else {
312                         $tag = 'year-calendar-month-nolink';
313                 }
314
315                 if ($pagesources{"$archivebase/$params{year}/$mtag"}) {
316                         $murl = htmllink($params{page}, $params{destpage}, 
317                                 "$archivebase/$params{year}/$mtag",
318                                 noimageinline => 1,
319                                 linktext => "$monthabbr");
320                         $calendar.=qq{\t<td class="$tag">};
321                         $calendar.=$murl;
322                         $calendar.=qq{\t</td>\n};
323                 }
324                 else {
325                         $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
326                 }
327                 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
328                         deptype("presence"));
329
330                 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
331         }
332
333         $calendar.=<<EOF;
334 </table>
335 EOF
336
337         return $calendar;
338 }
339
340 sub preprocess (@) {
341         my %params=@_;
342
343         my $thisyear=1900 + $now[5];
344         my $thismonth=1 + $now[4];
345
346         $params{pages} = "*"            unless defined $params{pages};
347         $params{type}  = "month"        unless defined $params{type};
348         $params{week_start_day} = 0     unless defined $params{week_start_day};
349         $params{months_per_row} = 3     unless defined $params{months_per_row};
350         $params{year}  = $thisyear      unless defined $params{year};
351         $params{month} = $thismonth     unless defined $params{month};
352
353         $params{month} = sprintf("%02d", $params{month});
354                         
355         if ($params{type} eq 'month' && $params{year} == $thisyear
356             && $params{month} == $thismonth) {
357                 # calendar for current month, updates next midnight
358                 $pagestate{$params{destpage}}{calendar}{nextchange}=($time
359                         + (60 - $now[0])                # seconds
360                         + (59 - $now[1]) * 60           # minutes
361                         + (23 - $now[2]) * 60 * 60      # hours
362                 );
363         }
364         elsif ($params{type} eq 'month' &&
365                (($params{year} == $thisyear && $params{month} > $thismonth) ||
366                 $params{year} > $thisyear)) {
367                 # calendar for upcoming month, updates 1st of that month
368                 $pagestate{$params{destpage}}{calendar}{nextchange}=
369                         timelocal(0, 0, 0, 1, $params{month}-1, $params{year});
370         }
371         elsif ($params{type} eq 'year' && $params{year} == $thisyear) {
372                 # calendar for current year, updates 1st of next month
373                 $pagestate{$params{destpage}}{calendar}{nextchange}=
374                         timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year});
375         }
376         elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
377                 # calendar for upcoming year, updates 1st of that year
378                 $pagestate{$params{destpage}}{calendar}{nextchange}=
379                         timelocal(0, 0, 0, 1, 1-1, $params{year});
380         }
381         else {
382                 # calendar for past month or year, does not need
383                 # to update any more
384                 delete $pagestate{$params{destpage}}{calendar};
385         }
386
387         # Calculate month names for next month, and previous months
388         my $calendar="";
389         if ($params{type} eq 'month') {
390                 $calendar=format_month(%params);
391         }
392         elsif ($params{type} eq 'year') {
393                 $calendar=format_year(%params);
394         }
395
396         return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
397 } #}}
398
399 sub needsbuild (@) {
400         my $needsbuild=shift;
401         foreach my $page (keys %pagestate) {
402                 if (exists $pagestate{$page}{calendar}{nextchange}) {
403                         if ($pagestate{$page}{calendar}{nextchange} <= $time) {
404                                 # force a rebuild so the calendar shows
405                                 # the current day
406                                 push @$needsbuild, $pagesources{$page};
407                         }
408                         if (exists $pagesources{$page} && 
409                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
410                                 # remove state, will be re-added if
411                                 # the calendar is still there during the
412                                 # rebuild
413                                 delete $pagestate{$page}{calendar};
414                         }
415                 }
416         }
417 }
418
419 1