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