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