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