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;
30 my @now=localtime($time);
33 hook(type => "getsetup", id => "calendar", call => \&getsetup);
34 hook(type => "needsbuild", id => "calendar", call => \&needsbuild);
35 hook(type => "preprocess", id => "calendar", call => \&preprocess);
46 example => "archives",
47 description => "base of the archives hierarchy",
53 sub is_leap_year (@) {
55 return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
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)) {
64 return $days_in_month;
67 sub format_month (@) {
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};
81 # When did this month start?
82 my @monthstart = localtime(timelocal(0,0,0,1,$month-1,$year-1900));
86 if ($year == $now[5]+1900 && $month == $now[4]+1) {
87 $future_dom = $now[3]+1;
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};
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 ");
107 add_depends($params{page}, "$archivebase/$year/".sprintf("%02d", $month),
109 if (exists $cache{$pagespec}{"$pyear/$pmonth"}) {
110 $purl = htmllink($params{page}, $params{destpage},
111 "$archivebase/$pyear/" . sprintf("%02d", $pmonth),
112 linktext => " $pmonthname ");
114 add_depends($params{page}, "$archivebase/$pyear/".sprintf("%02d", $pmonth),
116 if (exists $cache{$pagespec}{"$nyear/$nmonth"}) {
117 $nurl = htmllink($params{page}, $params{destpage},
118 "$archivebase/$nyear/" . sprintf("%02d", $nmonth),
119 linktext => " $nmonthname ");
121 add_depends($params{page}, "$archivebase/$nyear/".sprintf("%02d", $nmonth),
124 # Start producing the month calendar
126 <table class="month-calendar">
127 <caption class="month-calendar-head">
135 # Suppose we want to start the week with day $week_start_day
136 # If $monthstart[6] == 1
137 my $week_start_day = $params{week_start_day};
139 my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
142 for my $dow ($week_start_day..$week_start_day+6) {
143 my @day=localtime(timelocal(0,0,0,$start_day++,$month-1,$year-1900));
144 my $downame = POSIX::strftime("%A", @day);
145 my $dowabbr = POSIX::strftime("%a", @day);
146 $downame{$dow % 7}=$downame;
147 $dowabbr{$dow % 7}=$dowabbr;
148 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame">$dowabbr</th>\n};
156 # we start with a week_start_day, and skip until we get to the first
157 for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
158 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
159 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
162 # At this point, either the first is a week_start_day, in which case
163 # nothing has been printed, or else we are in the middle of a row.
164 for (my $day = 1; $day <= month_days(year => $year, month => $month);
165 $day++, $wday++, $wday %= 7) {
166 # At tihs point, on a week_start_day, we close out a row,
167 # and start a new one -- unless it is week_start_day on the
168 # first, where we do not close a row -- since none was started.
169 if ($wday == $week_start_day) {
170 $calendar.=qq{\t</tr>\n} unless $day == 1;
171 $calendar.=qq{\t<tr>\n};
175 my $mtag = sprintf("%02d", $month);
176 if (defined $cache{$pagespec}{"$year/$mtag/$day"}) {
177 if ($day == $today) {
178 $tag='month-calendar-day-this-day';
181 $tag='month-calendar-day-link';
183 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
184 $calendar.=htmllink($params{page}, $params{destpage},
185 pagename($linkcache{"$year/$mtag/$day"}),
186 "linktext" => "$day");
187 push @list, pagename($linkcache{"$year/$mtag/$day"});
188 $calendar.=qq{</td>\n};
191 if ($day == $today) {
192 $tag='month-calendar-day-this-day';
194 elsif ($day == $future_dom) {
195 $tag='month-calendar-day-future';
198 $tag='month-calendar-day-nolink';
200 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
204 # finish off the week
205 for (; $wday != $week_start_day; $wday++, $wday %= 7) {
206 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
213 # Add dependencies to update the calendar whenever pages
214 # matching the pagespec are added or removed.
215 add_depends($params{page}, $params{pages}, presence => 1);
216 # Explicitly add all currently linked pages as dependencies, so
217 # that if they are removed, the calendar will be sure to be updated.
218 foreach my $p (@list) {
219 add_depends($params{page}, $p, presence => 1);
225 sub format_year (@) {
228 my $pagespec = $params{pages};
229 my $year = $params{year};
230 my $month = $params{month};
231 my $pmonth = $params{pmonth};
232 my $nmonth = $params{nmonth};
233 my $pyear = $params{pyear};
234 my $nyear = $params{nyear};
238 my $future_month = 0;
239 $future_month = $now[4]+1 if ($year == $now[5]+1900);
241 my $archivebase = 'archives';
242 $archivebase = $config{archivebase} if defined $config{archivebase};
243 $archivebase = $params{archivebase} if defined $params{archivebase};
245 # calculate URL's for previous and next years
246 my ($url, $purl, $nurl)=("$year",'','');
247 if (exists $cache{$pagespec}{"$year"}) {
248 $url = htmllink($params{page}, $params{destpage},
249 "$archivebase/$year",
250 linktext => "$year");
252 add_depends($params{page}, "$archivebase/$year", presence => 1);
253 if (exists $cache{$pagespec}{"$pyear"}) {
254 $purl = htmllink($params{page}, $params{destpage},
255 "$archivebase/$pyear",
256 linktext => "\←");
258 add_depends($params{page}, "$archivebase/$pyear", presence => 1);
259 if (exists $cache{$pagespec}{"$nyear"}) {
260 $nurl = htmllink($params{page}, $params{destpage},
261 "$archivebase/$nyear",
262 linktext => "\→");
264 add_depends($params{page}, "$archivebase/$nyear", presence => 1);
266 # Start producing the year calendar
268 <table class="year-calendar">
269 <caption class="year-calendar-head">
275 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
279 for ($month = 1; $month <= 12; $month++) {
280 my @day=localtime(timelocal(0,0,0,15,$month-1,$year-1900));
282 my $monthname = POSIX::strftime("%B", @day);
283 my $monthabbr = POSIX::strftime("%b", @day);
284 $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
286 my $mtag=sprintf("%02d", $month);
287 if ($month == $params{month}) {
288 if ($cache{$pagespec}{"$year/$mtag"}) {
289 $tag = 'this_month_link';
292 $tag = 'this_month_nolink';
295 elsif ($cache{$pagespec}{"$year/$mtag"}) {
298 elsif ($future_month && $month >= $future_month) {
299 $tag = 'month_future';
302 $tag = 'month_nolink';
305 if ($cache{$pagespec}{"$year/$mtag"}) {
306 $murl = htmllink($params{page}, $params{destpage},
307 "$archivebase/$year/$mtag",
308 linktext => "$monthabbr");
309 $calendar.=qq{\t<td class="$tag">};
311 $calendar.=qq{\t</td>\n};
314 $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
316 add_depends($params{page}, "$archivebase/$year/$mtag", presence => 1);
318 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
330 $params{pages} = "*" unless defined $params{pages};
331 $params{type} = "month" unless defined $params{type};
332 $params{month} = sprintf("%02d", $params{month}) if defined $params{month};
333 $params{week_start_day} = 0 unless defined $params{week_start_day};
334 $params{months_per_row} = 3 unless defined $params{months_per_row};
336 if (! defined $params{year} || ! defined $params{month}) {
337 # Record that the calendar next changes at midnight.
338 $pagestate{$params{destpage}}{calendar}{nextchange}=($time
339 + (60 - $now[0]) # seconds
340 + (59 - $now[1]) * 60 # minutes
341 + (23 - $now[2]) * 60 * 60 # hours
344 $params{year} = 1900 + $now[5] unless defined $params{year};
345 $params{month} = 1 + $now[4] unless defined $params{month};
348 delete $pagestate{$params{destpage}}{calendar};
351 # Calculate month names for next month, and previous months
352 my $pmonth = $params{month} - 1;
353 my $nmonth = $params{month} + 1;
354 my $pyear = $params{year} - 1;
355 my $nyear = $params{year} + 1;
357 # Adjust for January and December
358 if ($params{month} == 1) {
362 if ($params{month} == 12) {
367 $params{pmonth}=$pmonth;
368 $params{nmonth}=$nmonth;
369 $params{pyear} =$pyear;
370 $params{nyear} =$nyear;
373 my $pagespec=$params{pages};
374 my $page =$params{page};
376 if (! defined $cache{$pagespec}) {
377 foreach my $p (pagespec_match_list([keys %pagesources], $pagespec)) {
378 my $mtime = $IkiWiki::pagectime{$p};
379 my $src = $pagesources{$p};
380 my @date = localtime($mtime);
382 my $month = $date[4] + 1;
383 my $year = $date[5] + 1900;
384 my $mtag = sprintf("%02d", $month);
386 # Only one posting per day is being linked to.
387 $linkcache{"$year/$mtag/$mday"} = "$src";
388 $cache{$pagespec}{"$year"}++;
389 $cache{$pagespec}{"$year/$mtag"}++;
390 $cache{$pagespec}{"$year/$mtag/$mday"}++;
394 if ($params{type} =~ /month/i) {
395 $calendar=format_month(%params);
397 elsif ($params{type} =~ /year/i) {
398 $calendar=format_year(%params);
401 return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
405 my $needsbuild=shift;
406 foreach my $page (keys %pagestate) {
407 if (exists $pagestate{$page}{calendar}{nextchange}) {
408 if ($pagestate{$page}{calendar}{nextchange} <= $time) {
409 # force a rebuild so the calendar shows
411 push @$needsbuild, $pagesources{$page};
413 if (exists $pagesources{$page} &&
414 grep { $_ eq $pagesources{$page} } @$needsbuild) {
415 # remove state, will be re-added if
416 # the calendar is still there during the
418 delete $pagestate{$page}{calendar};