]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/calendar.pm
5d16dff75ba7a98a68c3fc8c94d9d9378cb11732
[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 %cache;
28 my %linkcache;
29 my $time=time;
30 my @now=localtime($time);
32 sub import {
33         hook(type => "getsetup", id => "calendar", call => \&getsetup);
34         hook(type => "needsbuild", id => "calendar", call => \&needsbuild);
35         hook(type => "preprocess", id => "calendar", call => \&preprocess);
36 }
38 sub getsetup () {
39         return
40                 plugin => {
41                         safe => 1,
42                         rebuild => undef,
43                 },
44                 archivebase => {
45                         type => "string",
46                         example => "archives",
47                         description => "base of the archives hierarchy",
48                         safe => 1,
49                         rebuild => 1,
50                 },
51 }
53 sub is_leap_year (@) {
54         my %params=@_;
55         return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
56 }
58 sub month_days {
59         my %params=@_;
60         my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
61         if ($params{month} == 2 && is_leap_year(%params)) {
62                 $days_in_month++;
63         }
64         return $days_in_month;
65 }
67 sub format_month (@) {
68         my %params=@_;
70         my $pagespec = $params{pages};
71         my $year     = $params{year};
72         my $month    = $params{month};
73         my $pmonth   = $params{pmonth};
74         my $nmonth   = $params{nmonth};
75         my $pyear    = $params{pyear};
76         my $nyear    = $params{nyear};
78         my @list;
79         my $calendar="\n";
81         # When did this month start?
82         my @monthstart = localtime(timelocal(0,0,0,1,$month-1,$year-1900));
84         my $future_dom = 0;
85         my $today      = 0;
86         if ($year == $now[5]+1900 && $month == $now[4]+1) {
87                 $future_dom = $now[3]+1;
88                 $today      = $now[3];
89         }
91         # Find out month names for this, next, and previous months
92         my $monthname=POSIX::strftime("%B", @monthstart);
93         my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
94         my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
96         my $archivebase = 'archives';
97         $archivebase = $config{archivebase} if defined $config{archivebase};
98         $archivebase = $params{archivebase} if defined $params{archivebase};
99   
100         # Calculate URL's for monthly archives.
101         my ($url, $purl, $nurl)=("$monthname",'','');
102         if (exists $cache{$pagespec}{"$year/$month"}) {
103                 $url = htmllink($params{page}, $params{destpage}, 
104                         "$archivebase/$year/".sprintf("%02d", $month),
105                         linktext => " $monthname ");
106         }
107         add_depends($params{page}, "$archivebase/$year/".sprintf("%02d", $month));
108         if (exists $cache{$pagespec}{"$pyear/$pmonth"}) {
109                 $purl = htmllink($params{page}, $params{destpage}, 
110                         "$archivebase/$pyear/" . sprintf("%02d", $pmonth),
111                         linktext => " $pmonthname ");
112         }
113         add_depends($params{page}, "$archivebase/$pyear/".sprintf("%02d", $pmonth));
114         if (exists $cache{$pagespec}{"$nyear/$nmonth"}) {
115                 $nurl = htmllink($params{page}, $params{destpage}, 
116                         "$archivebase/$nyear/" . sprintf("%02d", $nmonth),
117                         linktext => " $nmonthname ");
118         }
119         add_depends($params{page}, "$archivebase/$nyear/".sprintf("%02d", $nmonth));
121         # Start producing the month calendar
122         $calendar=<<EOF;
123 <table class="month-calendar">
124         <caption class="month-calendar-head">
125         $purl
126         $url
127         $nurl
128         </caption>
129         <tr>
130 EOF
132         # Suppose we want to start the week with day $week_start_day
133         # If $monthstart[6] == 1
134         my $week_start_day = $params{week_start_day};
136         my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
137         my %downame;
138         my %dowabbr;
139         for my $dow ($week_start_day..$week_start_day+6) {
140                 my @day=localtime(timelocal(0,0,0,$start_day++,$month-1,$year-1900));
141                 my $downame = POSIX::strftime("%A", @day);
142                 my $dowabbr = POSIX::strftime("%a", @day);
143                 $downame{$dow % 7}=$downame;
144                 $dowabbr{$dow % 7}=$dowabbr;
145                 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame">$dowabbr</th>\n};
146         }
148         $calendar.=<<EOF;
149         </tr>
150 EOF
152         my $wday;
153         # we start with a week_start_day, and skip until we get to the first
154         for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
155                 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
156                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
157         }
159         # At this point, either the first is a week_start_day, in which case
160         # nothing has been printed, or else we are in the middle of a row.
161         for (my $day = 1; $day <= month_days(year => $year, month => $month);
162              $day++, $wday++, $wday %= 7) {
163                 # At tihs point, on a week_start_day, we close out a row,
164                 # and start a new one -- unless it is week_start_day on the
165                 # first, where we do not close a row -- since none was started.
166                 if ($wday == $week_start_day) {
167                         $calendar.=qq{\t</tr>\n} unless $day == 1;
168                         $calendar.=qq{\t<tr>\n};
169                 }
170                 
171                 my $tag;
172                 my $mtag = sprintf("%02d", $month);
173                 if (defined $cache{$pagespec}{"$year/$mtag/$day"}) {
174                         if ($day == $today) {
175                                 $tag='month-calendar-day-this-day';
176                         }
177                         else {
178                                 $tag='month-calendar-day-link';
179                         }
180                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
181                         $calendar.=htmllink($params{page}, $params{destpage}, 
182                                             pagename($linkcache{"$year/$mtag/$day"}),
183                                             "linktext" => "$day");
184                         push @list, pagename($linkcache{"$year/$mtag/$day"});
185                         $calendar.=qq{</td>\n};
186                 }
187                 else {
188                         if ($day == $today) {
189                                 $tag='month-calendar-day-this-day';
190                         }
191                         elsif ($day == $future_dom) {
192                                 $tag='month-calendar-day-future';
193                         }
194                         else {
195                                 $tag='month-calendar-day-nolink';
196                         }
197                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
198                 }
199         }
201         # finish off the week
202         for (; $wday != $week_start_day; $wday++, $wday %= 7) {
203                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
204         }
205         $calendar.=<<EOF;
206         </tr>
207 </table>
208 EOF
210         # Add dependencies to update the calendar whenever pages
211         # matching the pagespec are added or removed.
212         add_depends($params{page}, $params{pages});
213         # Explicitly add all currently linked pages as dependencies, so
214         # that if they are removed, the calendar will be sure to be updated.
215         foreach my $p (@list) {
216                 add_depends($params{page}, $p);
217         }
219         return $calendar;
222 sub format_year (@) {
223         my %params=@_;
225         my $pagespec = $params{pages};
226         my $year     = $params{year};
227         my $month    = $params{month};
228         my $pmonth   = $params{pmonth};
229         my $nmonth   = $params{nmonth};
230         my $pyear    = $params{pyear};
231         my $nyear    = $params{nyear};
233         my $calendar="\n";
235         my $future_month = 0;
236         $future_month = $now[4]+1 if ($year == $now[5]+1900);
238         my $archivebase = 'archives';
239         $archivebase = $config{archivebase} if defined $config{archivebase};
240         $archivebase = $params{archivebase} if defined $params{archivebase};
242         # calculate URL's for previous and next years
243         my ($url, $purl, $nurl)=("$year",'','');
244         if (exists $cache{$pagespec}{"$year"}) {
245                 $url = htmllink($params{page}, $params{destpage}, 
246                         "$archivebase/$year",
247                         linktext => "$year");
248         }
249         add_depends($params{page}, "$archivebase/$year");
250         if (exists $cache{$pagespec}{"$pyear"}) {
251                 $purl = htmllink($params{page}, $params{destpage}, 
252                         "$archivebase/$pyear",
253                         linktext => "\&larr;");
254         }
255         add_depends($params{page}, "$archivebase/$pyear");
256         if (exists $cache{$pagespec}{"$nyear"}) {
257                 $nurl = htmllink($params{page}, $params{destpage}, 
258                         "$archivebase/$nyear",
259                         linktext => "\&rarr;");
260         }
261         add_depends($params{page}, "$archivebase/$nyear");
263         # Start producing the year calendar
264         $calendar=<<EOF;
265 <table class="year-calendar">
266         <caption class="year-calendar-head">
267         $purl
268         $url
269         $nurl
270         </caption>
271         <tr>
272                 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
273         </tr>
274 EOF
276         for ($month = 1; $month <= 12; $month++) {
277                 my @day=localtime(timelocal(0,0,0,15,$month-1,$year-1900));
278                 my $murl;
279                 my $monthname = POSIX::strftime("%B", @day);
280                 my $monthabbr = POSIX::strftime("%b", @day);
281                 $calendar.=qq{\t<tr>\n}  if ($month % $params{months_per_row} == 1);
282                 my $tag;
283                 my $mtag=sprintf("%02d", $month);
284                 if ($month == $params{month}) {
285                         if ($cache{$pagespec}{"$year/$mtag"}) {
286                                 $tag = 'this_month_link';
287                         }
288                         else {
289                                 $tag = 'this_month_nolink';
290                         }
291                 }
292                 elsif ($cache{$pagespec}{"$year/$mtag"}) {
293                         $tag = 'month_link';
294                 } 
295                 elsif ($future_month && $month >= $future_month) {
296                         $tag = 'month_future';
297                 } 
298                 else {
299                         $tag = 'month_nolink';
300                 }
302                 if ($cache{$pagespec}{"$year/$mtag"}) {
303                         $murl = htmllink($params{page}, $params{destpage}, 
304                                 "$archivebase/$year/$mtag",
305                                 linktext => "$monthabbr");
306                         $calendar.=qq{\t<td class="$tag">};
307                         $calendar.=$murl;
308                         $calendar.=qq{\t</td>\n};
309                 }
310                 else {
311                         $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
312                 }
313                 add_depends($params{page}, "$archivebase/$year/$mtag");
315                 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
316         }
318         $calendar.=<<EOF;
319 </table>
320 EOF
322         return $calendar;
325 sub preprocess (@) {
326         my %params=@_;
327         $params{pages} = "*"            unless defined $params{pages};
328         $params{type}  = "month"        unless defined $params{type};
329         $params{month} = sprintf("%02d", $params{month}) if defined  $params{month};
330         $params{week_start_day} = 0     unless defined $params{week_start_day};
331         $params{months_per_row} = 3     unless defined $params{months_per_row};
333         if (! defined $params{year} || ! defined $params{month}) {
334                 # Record that the calendar next changes at midnight.
335                 $pagestate{$params{destpage}}{calendar}{nextchange}=($time
336                         + (60 - $now[0])                # seconds
337                         + (59 - $now[1]) * 60           # minutes
338                         + (23 - $now[2]) * 60 * 60      # hours
339                 );
340                 
341                 $params{year}  = 1900 + $now[5] unless defined $params{year};
342                 $params{month} = 1    + $now[4] unless defined $params{month};
343         }
344         else {
345                 delete $pagestate{$params{destpage}}{calendar};
346         }
348         # Calculate month names for next month, and previous months
349         my $pmonth = $params{month} - 1;
350         my $nmonth = $params{month} + 1;
351         my $pyear  = $params{year}  - 1;
352         my $nyear  = $params{year}  + 1;
354         # Adjust for January and December
355         if ($params{month} == 1) {
356                 $pmonth = 12;
357                 $pyear--;
358         }
359         if ($params{month} == 12) {
360                 $nmonth = 1;
361                 $nyear++;
362         }
364         $params{pmonth}=$pmonth;
365         $params{nmonth}=$nmonth;
366         $params{pyear} =$pyear;
367         $params{nyear} =$nyear;
369         my $calendar="\n";
370         my $pagespec=$params{pages};
371         my $page =$params{page};
373         if (! defined $cache{$pagespec}) {
374                 foreach my $p (pagespec_match_list([keys %pagesources], $pagespec)) {
375                         my $mtime = $IkiWiki::pagectime{$p};
376                         my $src   = $pagesources{$p};
377                         my @date  = localtime($mtime);
378                         my $mday  = $date[3];
379                         my $month = $date[4] + 1;
380                         my $year  = $date[5] + 1900;
381                         my $mtag  = sprintf("%02d", $month);
383                         # Only one posting per day is being linked to.
384                         $linkcache{"$year/$mtag/$mday"} = "$src";
385                         $cache{$pagespec}{"$year"}++;
386                         $cache{$pagespec}{"$year/$mtag"}++;
387                         $cache{$pagespec}{"$year/$mtag/$mday"}++;
388                 }
389         }
391         if ($params{type} =~ /month/i) {
392                 $calendar=format_month(%params);
393         }
394         elsif ($params{type} =~ /year/i) {
395                 $calendar=format_year(%params);
396         }
398         return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
399 } #}}
401 sub needsbuild (@) {
402         my $needsbuild=shift;
403         foreach my $page (keys %pagestate) {
404                 if (exists $pagestate{$page}{calendar}{nextchange}) {
405                         if ($pagestate{$page}{calendar}{nextchange} <= $time) {
406                                 # force a rebuild so the calendar shows
407                                 # the current day
408                                 push @$needsbuild, $pagesources{$page};
409                         }
410                         if (exists $pagesources{$page} && 
411                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
412                                 # remove state, will be re-added if
413                                 # the calendar is still there during the
414                                 # rebuild
415                                 delete $pagestate{$page}{calendar};
416                         }
417                 }
418         }