2 # Copyright (c) 2006, 2007 Manoj Srivastava <srivasta@debian.org>
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.
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.
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
19 package IkiWiki::Plugin::calendar;
28 my @now=localtime($time);
31 hook(type => "getsetup", id => "calendar", call => \&getsetup);
32 hook(type => "needsbuild", id => "calendar", call => \&needsbuild);
33 hook(type => "preprocess", id => "calendar", call => \&preprocess);
44 example => "archives",
45 description => "base of the archives hierarchy",
51 sub is_leap_year (@) {
53 return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
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)) {
62 return $days_in_month;
65 sub format_month (@) {
68 my $pagespec = $params{pages};
69 my $year = $params{year};
70 my $month = $params{month};
71 my $pmonth = $params{pmonth};
72 my $nmonth = $params{nmonth};
73 my $pyear = $params{pyear};
74 my $nyear = $params{nyear};
77 foreach my $p (pagespec_match_list($params{page}, $params{pagespec},
78 # add presence dependencies to update
79 # month calendar when pages are added/removed
80 deptype => deptype("presence"))) {
81 my $mtime = $IkiWiki::pagectime{$p};
82 my $src = $pagesources{$p};
83 my @date = localtime($mtime);
85 my $month = $date[4] + 1;
86 my $year = $date[5] + 1900;
87 my $mtag = sprintf("%02d", $month);
89 # Only one posting per day is being linked to.
90 $linkcache{"$year/$mtag/$mday"} = "$src";
96 # When did this month start?
97 my @monthstart = localtime(timelocal(0,0,0,1,$month-1,$year-1900));
101 if ($year == $now[5]+1900 && $month == $now[4]+1) {
102 $future_dom = $now[3]+1;
106 # Find out month names for this, next, and previous months
107 my $monthname=POSIX::strftime("%B", @monthstart);
108 my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
109 my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
111 my $archivebase = 'archives';
112 $archivebase = $config{archivebase} if defined $config{archivebase};
113 $archivebase = $params{archivebase} if defined $params{archivebase};
115 # Calculate URL's for monthly archives.
116 my ($url, $purl, $nurl)=("$monthname",'','');
117 if (exists $pagesources{"$archivebase/$year/$month"}) {
118 $url = htmllink($params{page}, $params{destpage},
119 "$archivebase/$year/".sprintf("%02d", $month),
120 linktext => " $monthname ");
122 add_depends($params{page}, "$archivebase/$year/".sprintf("%02d", $month),
123 deptype("presence"));
124 if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
125 $purl = htmllink($params{page}, $params{destpage},
126 "$archivebase/$pyear/" . sprintf("%02d", $pmonth),
127 linktext => " $pmonthname ");
129 add_depends($params{page}, "$archivebase/$pyear/".sprintf("%02d", $pmonth),
130 deptype("presence"));
131 if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
132 $nurl = htmllink($params{page}, $params{destpage},
133 "$archivebase/$nyear/" . sprintf("%02d", $nmonth),
134 linktext => " $nmonthname ");
136 add_depends($params{page}, "$archivebase/$nyear/".sprintf("%02d", $nmonth),
137 deptype("presence"));
139 # Start producing the month calendar
141 <table class="month-calendar">
142 <caption class="month-calendar-head">
150 # Suppose we want to start the week with day $week_start_day
151 # If $monthstart[6] == 1
152 my $week_start_day = $params{week_start_day};
154 my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
157 for my $dow ($week_start_day..$week_start_day+6) {
158 my @day=localtime(timelocal(0,0,0,$start_day++,$month-1,$year-1900));
159 my $downame = POSIX::strftime("%A", @day);
160 my $dowabbr = POSIX::strftime("%a", @day);
161 $downame{$dow % 7}=$downame;
162 $dowabbr{$dow % 7}=$dowabbr;
163 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame">$dowabbr</th>\n};
171 # we start with a week_start_day, and skip until we get to the first
172 for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
173 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
174 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
177 # At this point, either the first is a week_start_day, in which case
178 # nothing has been printed, or else we are in the middle of a row.
179 for (my $day = 1; $day <= month_days(year => $year, month => $month);
180 $day++, $wday++, $wday %= 7) {
181 # At tihs point, on a week_start_day, we close out a row,
182 # and start a new one -- unless it is week_start_day on the
183 # first, where we do not close a row -- since none was started.
184 if ($wday == $week_start_day) {
185 $calendar.=qq{\t</tr>\n} unless $day == 1;
186 $calendar.=qq{\t<tr>\n};
190 my $mtag = sprintf("%02d", $month);
191 if (defined $pagesources{"$archivebase/$year/$mtag/$day"}) {
192 if ($day == $today) {
193 $tag='month-calendar-day-this-day';
196 $tag='month-calendar-day-link';
198 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
199 $calendar.=htmllink($params{page}, $params{destpage},
200 pagename($linkcache{"$year/$mtag/$day"}),
201 "linktext" => "$day");
202 push @list, pagename($linkcache{"$year/$mtag/$day"});
203 $calendar.=qq{</td>\n};
206 if ($day == $today) {
207 $tag='month-calendar-day-this-day';
209 elsif ($day == $future_dom) {
210 $tag='month-calendar-day-future';
213 $tag='month-calendar-day-nolink';
215 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
219 # finish off the week
220 for (; $wday != $week_start_day; $wday++, $wday %= 7) {
221 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
231 sub format_year (@) {
234 my $pagespec = $params{pages};
235 my $year = $params{year};
236 my $month = $params{month};
237 my $pmonth = $params{pmonth};
238 my $nmonth = $params{nmonth};
239 my $pyear = $params{pyear};
240 my $nyear = $params{nyear};
244 my $future_month = 0;
245 $future_month = $now[4]+1 if ($year == $now[5]+1900);
247 my $archivebase = 'archives';
248 $archivebase = $config{archivebase} if defined $config{archivebase};
249 $archivebase = $params{archivebase} if defined $params{archivebase};
251 # calculate URL's for previous and next years
252 my ($url, $purl, $nurl)=("$year",'','');
253 if (exists $pagesources{"$archivebase/$year"}) {
254 $url = htmllink($params{page}, $params{destpage},
255 "$archivebase/$year",
256 linktext => "$year");
258 add_depends($params{page}, "$archivebase/$year", deptype("presence"));
259 if (exists $pagesources{"$archivebase/$pyear"}) {
260 $purl = htmllink($params{page}, $params{destpage},
261 "$archivebase/$pyear",
262 linktext => "\←");
264 add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
265 if (exists $pagesources{"$archivebase/$nyear"}) {
266 $nurl = htmllink($params{page}, $params{destpage},
267 "$archivebase/$nyear",
268 linktext => "\→");
270 add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
272 # Start producing the year calendar
274 <table class="year-calendar">
275 <caption class="year-calendar-head">
281 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
285 for ($month = 1; $month <= 12; $month++) {
286 my @day=localtime(timelocal(0,0,0,15,$month-1,$year-1900));
288 my $monthname = POSIX::strftime("%B", @day);
289 my $monthabbr = POSIX::strftime("%b", @day);
290 $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
292 my $mtag=sprintf("%02d", $month);
293 if ($month == $params{month}) {
294 if ($pagesources{"$archivebase/$year/$mtag"}) {
295 $tag = 'this_month_link';
298 $tag = 'this_month_nolink';
301 elsif ($pagesources{"$archivebase/$year/$mtag"}) {
304 elsif ($future_month && $month >= $future_month) {
305 $tag = 'month_future';
308 $tag = 'month_nolink';
311 if ($pagesources{"$archivebase/$year/$mtag"}) {
312 $murl = htmllink($params{page}, $params{destpage},
313 "$archivebase/$year/$mtag",
314 linktext => "$monthabbr");
315 $calendar.=qq{\t<td class="$tag">};
317 $calendar.=qq{\t</td>\n};
320 $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
322 add_depends($params{page}, "$archivebase/$year/$mtag",
323 deptype("presence"));
325 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
337 $params{pages} = "*" unless defined $params{pages};
338 $params{type} = "month" unless defined $params{type};
339 $params{month} = sprintf("%02d", $params{month}) if defined $params{month};
340 $params{week_start_day} = 0 unless defined $params{week_start_day};
341 $params{months_per_row} = 3 unless defined $params{months_per_row};
343 if (! defined $params{year} || ! defined $params{month}) {
344 # Record that the calendar next changes at midnight.
345 $pagestate{$params{destpage}}{calendar}{nextchange}=($time
346 + (60 - $now[0]) # seconds
347 + (59 - $now[1]) * 60 # minutes
348 + (23 - $now[2]) * 60 * 60 # hours
351 $params{year} = 1900 + $now[5] unless defined $params{year};
352 $params{month} = 1 + $now[4] unless defined $params{month};
355 delete $pagestate{$params{destpage}}{calendar};
358 # Calculate month names for next month, and previous months
359 my $pmonth = $params{month} - 1;
360 my $nmonth = $params{month} + 1;
361 my $pyear = $params{year} - 1;
362 my $nyear = $params{year} + 1;
364 # Adjust for January and December
365 if ($params{month} == 1) {
369 if ($params{month} == 12) {
374 $params{pmonth}=$pmonth;
375 $params{nmonth}=$nmonth;
376 $params{pyear} =$pyear;
377 $params{nyear} =$nyear;
381 if ($params{type} =~ /month/i) {
382 $calendar=format_month(%params);
384 elsif ($params{type} =~ /year/i) {
385 $calendar=format_year(%params);
388 return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
392 my $needsbuild=shift;
393 foreach my $page (keys %pagestate) {
394 if (exists $pagestate{$page}{calendar}{nextchange}) {
395 if ($pagestate{$page}{calendar}{nextchange} <= $time) {
396 # force a rebuild so the calendar shows
398 push @$needsbuild, $pagesources{$page};
400 if (exists $pagesources{$page} &&
401 grep { $_ eq $pagesources{$page} } @$needsbuild) {
402 # remove state, will be re-added if
403 # the calendar is still there during the
405 delete $pagestate{$page}{calendar};