]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/calendar.pm
web commit by http://ptecza.myopenid.com/: * Reply
[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 @now=localtime();
31 sub import { #{{{
32         hook(type => "preprocess", id => "calendar", call => \&preprocess);
33 } #}}}
35 sub is_leap_year (@) { #{{{
36         my %params=@_;
37         return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
38 } #}}}
40 sub month_days { #{{{
41         my %params=@_;
42         my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
43         if ($params{month} == 2 && is_leap_year(%params)) {
44                 $days_in_month++;
45         }
46         return $days_in_month;
47 } #}}}
49 sub format_month (@) { #{{{
50         my %params=@_;
52         my $pagespec = $params{pages};
53         my $year     = $params{year};
54         my $month    = $params{month};
55         my $pmonth   = $params{pmonth};
56         my $nmonth   = $params{nmonth};
57         my $pyear    = $params{pyear};
58         my $nyear    = $params{nyear};
60         my @list;
61         my $calendar="\n";
63         # When did this month start?
64         my @monthstart = localtime(timelocal(0,0,0,1,$month-1,$year-1900));
66         my $future_dom = 0;
67         my $today      = 0;
68         if ($year == $now[5]+1900 && $month == $now[4]+1) {
69                 $future_dom = $now[3]+1;
70                 $today      = $now[3];
71         }
73         # Find out month names for this, next, and previous months
74         my $monthname=POSIX::strftime("%B", @monthstart);
75         my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
76         my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
78         my $archivebase = 'archives';
79         $archivebase = $config{archivebase} if defined $config{archivebase};
80         $archivebase = $params{archivebase} if defined $params{archivebase};
81   
82         # Calculate URL's for monthly archives.
83         my ($url, $purl, $nurl)=("$monthname",'','');
84         if (exists $cache{$pagespec}{"$year/$month"}) {
85                 $url = htmllink($params{page}, $params{destpage}, 
86                         "$archivebase/$year/".sprintf("%02d", $month),
87                         linktext => " $monthname ");
88         }
89         add_depends($params{page}, "$archivebase/$year/".sprintf("%02d", $month));
90         if (exists $cache{$pagespec}{"$pyear/$pmonth"}) {
91                 $purl = htmllink($params{page}, $params{destpage}, 
92                         "$archivebase/$pyear/" . sprintf("%02d", $pmonth),
93                         linktext => " $pmonthname ");
94         }
95         add_depends($params{page}, "$archivebase/$pyear/".sprintf("%02d", $pmonth));
96         if (exists $cache{$pagespec}{"$nyear/$nmonth"}) {
97                 $nurl = htmllink($params{page}, $params{destpage}, 
98                         "$archivebase/$nyear/" . sprintf("%02d", $nmonth),
99                         linktext => " $nmonthname ");
100         }
101         add_depends($params{page}, "$archivebase/$nyear/".sprintf("%02d", $nmonth));
103         # Start producing the month calendar
104         $calendar=<<EOF;
105 <table class="month-calendar">
106         <caption class="month-calendar-head">
107         $purl
108         $url
109         $nurl
110         </caption>
111         <tr>
112 EOF
114         # Suppose we want to start the week with day $week_start_day
115         # If $monthstart[6] == 1
116         my $week_start_day = $params{week_start_day};
118         my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
119         my %downame;
120         my %dowabbr;
121         for my $dow ($week_start_day..$week_start_day+6) {
122                 my @day=localtime(timelocal(0,0,0,$start_day++,$month-1,$year-1900));
123                 my $downame = POSIX::strftime("%A", @day);
124                 my $dowabbr = POSIX::strftime("%a", @day);
125                 $downame{$dow % 7}=$downame;
126                 $dowabbr{$dow % 7}=$dowabbr;
127                 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame">$dowabbr</th>\n};
128         }
130         $calendar.=<<EOF;
131         </tr>
132 EOF
134         my $wday;
135         # we start with a week_start_day, and skip until we get to the first
136         for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
137                 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
138                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
139         }
141         # At this point, either the first is a week_start_day, in which case
142         # nothing has been printed, or else we are in the middle of a row.
143         for (my $day = 1; $day <= month_days(year => $year, month => $month);
144              $day++, $wday++, $wday %= 7) {
145                 # At tihs point, on a week_start_day, we close out a row,
146                 # and start a new one -- unless it is week_start_day on the
147                 # first, where we do not close a row -- since none was started.
148                 if ($wday == $week_start_day) {
149                         $calendar.=qq{\t</tr>\n} unless $day == 1;
150                         $calendar.=qq{\t<tr>\n};
151                 }
152                 
153                 my $tag;
154                 my $mtag = sprintf("%02d", $month);
155                 if (defined $cache{$pagespec}{"$year/$mtag/$day"}) {
156                         if ($day == $today) {
157                                 $tag='month-calendar-day-this-day';
158                         }
159                         else {
160                                 $tag='month-calendar-day-link';
161                         }
162                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
163                         $calendar.=htmllink($params{page}, $params{destpage}, 
164                                             pagename($linkcache{"$year/$mtag/$day"}),
165                                             "linktext" => "$day");
166                         push @list, pagename($linkcache{"$year/$mtag/$day"});
167                         $calendar.=qq{</td>\n};
168                 }
169                 else {
170                         if ($day == $today) {
171                                 $tag='month-calendar-day-this-day';
172                         }
173                         elsif ($day == $future_dom) {
174                                 $tag='month-calendar-day-future';
175                         }
176                         else {
177                                 $tag='month-calendar-day-nolink';
178                         }
179                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
180                 }
181         }
183         # finish off the week
184         for (; $wday != $week_start_day; $wday++, $wday %= 7) {
185                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
186         }
187         $calendar.=<<EOF;
188         </tr>
189 </table>
190 EOF
192         # Add dependencies to update the calendar whenever pages
193         # matching the pagespec are added or removed.
194         add_depends($params{page}, $params{pages});
195         # Explicitly add all currently linked pages as dependencies, so
196         # that if they are removed, the calendar will be sure to be updated.
197         add_depends($params{page}, join(" or ", @list));
199         return $calendar;
200 } #}}}
202 sub format_year (@) { #{{{
203         my %params=@_;
205         my $pagespec = $params{pages};
206         my $year     = $params{year};
207         my $month    = $params{month};
208         my $pmonth   = $params{pmonth};
209         my $nmonth   = $params{nmonth};
210         my $pyear    = $params{pyear};
211         my $nyear    = $params{nyear};
213         my $calendar="\n";
215         my $future_month = 0;
216         $future_month = $now[4]+1 if ($year == $now[5]+1900);
218         my $archivebase = 'archives';
219         $archivebase = $config{archivebase} if defined $config{archivebase};
220         $archivebase = $params{archivebase} if defined $params{archivebase};
222         # calculate URL's for previous and next years
223         my ($url, $purl, $nurl)=("$year",'','');
224         if (exists $cache{$pagespec}{"$year"}) {
225                 $url = htmllink($params{page}, $params{destpage}, 
226                         "$archivebase/$year",
227                         linktext => "$year");
228         }
229         add_depends($params{page}, "$archivebase/$year");
230         if (exists $cache{$pagespec}{"$pyear"}) {
231                 $purl = htmllink($params{page}, $params{destpage}, 
232                         "$archivebase/$pyear",
233                         linktext => "\&larr;");
234         }
235         add_depends($params{page}, "$archivebase/$pyear");
236         if (exists $cache{$pagespec}{"$nyear"}) {
237                 $nurl = htmllink($params{page}, $params{destpage}, 
238                         "$archivebase/$nyear",
239                         linktext => "\&rarr;");
240         }
241         add_depends($params{page}, "$archivebase/$nyear");
243         # Start producing the year calendar
244         $calendar=<<EOF;
245 <table class="year-calendar">
246         <caption class="year-calendar-head">
247         $purl
248         $url
249         $nurl
250         </caption>
251         <tr>
252                 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
253         </tr>
254 EOF
256         for ($month = 1; $month <= 12; $month++) {
257                 my @day=localtime(timelocal(0,0,0,15,$month-1,$year-1900));
258                 my $murl;
259                 my $monthname = POSIX::strftime("%B", @day);
260                 my $monthabbr = POSIX::strftime("%b", @day);
261                 $calendar.=qq{\t<tr>\n}  if ($month % $params{months_per_row} == 1);
262                 my $tag;
263                 my $mtag=sprintf("%02d", $month);
264                 if ($month == $params{month}) {
265                         if ($cache{$pagespec}{"$year/$mtag"}) {
266                                 $tag = 'this_month_link';
267                         }
268                         else {
269                                 $tag = 'this_month_nolink';
270                         }
271                 }
272                 elsif ($cache{$pagespec}{"$year/$mtag"}) {
273                         $tag = 'month_link';
274                 } 
275                 elsif ($future_month && $month >= $future_month) {
276                         $tag = 'month_future';
277                 } 
278                 else {
279                         $tag = 'month_nolink';
280                 }
282                 if ($cache{$pagespec}{"$year/$mtag"}) {
283                         $murl = htmllink($params{page}, $params{destpage}, 
284                                 "$archivebase/$year/$mtag",
285                                 linktext => "$monthabbr");
286                         $calendar.=qq{\t<td class="$tag">};
287                         $calendar.=$murl;
288                         $calendar.=qq{\t</td>\n};
289                 }
290                 else {
291                         $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
292                 }
293                 add_depends($params{page}, "$archivebase/$year/$mtag");
295                 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
296         }
298         $calendar.=<<EOF;
299 </table>
300 EOF
302         return $calendar;
303 } #}}}
305 sub preprocess (@) { #{{{
306         my %params=@_;
307         $params{pages} = "*"            unless defined $params{pages};
308         $params{type}  = "month"        unless defined $params{type};
309         $params{year}  = 1900 + $now[5] unless defined $params{year};
310         $params{month} = sprintf("%02d", $params{month}) if defined  $params{month};
311         $params{month} = 1    + $now[4] unless defined $params{month};
312         $params{week_start_day} = 0     unless defined $params{week_start_day};
313         $params{months_per_row} = 3     unless defined $params{months_per_row};
315         # Calculate month names for next month, and previous months
316         my $pmonth = $params{month} - 1;
317         my $nmonth = $params{month} + 1;
318         my $pyear  = $params{year}  - 1;
319         my $nyear  = $params{year}  + 1;
321         # Adjust for January and December
322         if ($params{month} == 1) {
323                 $pmonth = 12;
324                 $pyear--;
325         }
326         if ($params{month} == 12) {
327                 $nmonth = 1;
328                 $nyear++;
329         }
331         $params{pmonth}=$pmonth;
332         $params{nmonth}=$nmonth;
333         $params{pyear} =$pyear;
334         $params{nyear} =$nyear;
336         my $calendar="\n";
337         my $pagespec=$params{pages};
338         my $page =$params{page};
340         if (! defined $cache{$pagespec}) {
341                 foreach my $p (keys %pagesources) {
342                         next unless pagespec_match($p, $pagespec);
343                         my $mtime = $IkiWiki::pagectime{$p};
344                         my $src   = $pagesources{$p};
345                         my @date  = localtime($mtime);
346                         my $mday  = $date[3];
347                         my $month = $date[4] + 1;
348                         my $year  = $date[5] + 1900;
349                         my $mtag  = sprintf("%02d", $month);
351                         # Only one posting per day is being linked to.
352                         $linkcache{"$year/$mtag/$mday"} = "$src";
353                         $cache{$pagespec}{"$year"}++;
354                         $cache{$pagespec}{"$year/$mtag"}++;
355                         $cache{$pagespec}{"$year/$mtag/$mday"}++;
356                 }
357         }
359         if ($params{type} =~ /month/i) {
360                 $calendar=format_month(%params);
361         }
362         elsif ($params{type} =~ /year/i) {
363                 $calendar=format_year(%params);
364         }
366         return "\n<div class=\"calendar\">$calendar</div><!-- calendar -->\n";
367 } #}}