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 (@) {
69 foreach my $p (pagespec_match_list($params{page}, $params{pages},
70 # add presence dependencies to update
71 # month calendar when pages are added/removed
72 deptype => deptype("presence"))) {
73 my $mtime = $IkiWiki::pagectime{$p};
74 my $src = $pagesources{$p};
75 my @date = localtime($mtime);
77 my $month = $date[4] + 1;
78 my $year = $date[5] + 1900;
79 my $mtag = sprintf("%02d", $month);
81 # Only one posting per day is being linked to.
82 $linkcache{"$year/$mtag/$mday"} = "$src";
88 # When did this month start?
89 my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
93 if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
94 $future_dom = $now[3]+1;
98 # Find out month names for this, next, and previous months
99 my $monthname=POSIX::strftime("%B", @monthstart);
100 my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$params{pmonth}-1,$params{pyear}-1900)));
101 my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$params{nmonth}-1,$params{nyear}-1900)));
103 my $archivebase = 'archives';
104 $archivebase = $config{archivebase} if defined $config{archivebase};
105 $archivebase = $params{archivebase} if defined $params{archivebase};
107 # Calculate URL's for monthly archives.
108 my ($url, $purl, $nurl)=("$monthname",'','');
109 if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
110 $url = htmllink($params{page}, $params{destpage},
111 "$archivebase/$params{year}/".sprintf("%02d", $params{month}),
112 linktext => " $monthname ");
114 add_depends($params{page}, "$archivebase/$params{year}/".sprintf("%02d", $params{month}),
115 deptype("presence"));
116 if (exists $pagesources{"$archivebase/$params{pyear}/$params{pmonth}"}) {
117 $purl = htmllink($params{page}, $params{destpage},
118 "$archivebase/$params{pyear}/" . sprintf("%02d", $params{pmonth}),
119 linktext => " $pmonthname ");
121 add_depends($params{page}, "$archivebase/$params{pyear}/".sprintf("%02d", $params{pmonth}),
122 deptype("presence"));
123 if (exists $pagesources{"$archivebase/$params{nyear}/$params{nmonth}"}) {
124 $nurl = htmllink($params{page}, $params{destpage},
125 "$archivebase/$params{nyear}/" . sprintf("%02d", $params{nmonth}),
126 linktext => " $nmonthname ");
128 add_depends($params{page}, "$archivebase/$params{nyear}/".sprintf("%02d", $params{nmonth}),
129 deptype("presence"));
131 # Start producing the month calendar
133 <table class="month-calendar">
134 <caption class="month-calendar-head">
142 # Suppose we want to start the week with day $week_start_day
143 # If $monthstart[6] == 1
144 my $week_start_day = $params{week_start_day};
146 my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
149 for my $dow ($week_start_day..$week_start_day+6) {
150 my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
151 my $downame = POSIX::strftime("%A", @day);
152 my $dowabbr = POSIX::strftime("%a", @day);
153 $downame{$dow % 7}=$downame;
154 $dowabbr{$dow % 7}=$dowabbr;
155 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame">$dowabbr</th>\n};
163 # we start with a week_start_day, and skip until we get to the first
164 for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
165 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
166 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
169 # At this point, either the first is a week_start_day, in which case
170 # nothing has been printed, or else we are in the middle of a row.
171 for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
172 $day++, $wday++, $wday %= 7) {
173 # At tihs point, on a week_start_day, we close out a row,
174 # and start a new one -- unless it is week_start_day on the
175 # first, where we do not close a row -- since none was started.
176 if ($wday == $week_start_day) {
177 $calendar.=qq{\t</tr>\n} unless $day == 1;
178 $calendar.=qq{\t<tr>\n};
182 my $mtag = sprintf("%02d", $params{month});
183 if (defined $pagesources{"$archivebase/$params{year}/$mtag/$day"}) {
184 if ($day == $today) {
185 $tag='month-calendar-day-this-day';
188 $tag='month-calendar-day-link';
190 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
191 $calendar.=htmllink($params{page}, $params{destpage},
192 pagename($linkcache{"$params{year}/$mtag/$day"}),
193 "linktext" => "$day");
194 push @list, pagename($linkcache{"$params{year}/$mtag/$day"});
195 $calendar.=qq{</td>\n};
198 if ($day == $today) {
199 $tag='month-calendar-day-this-day';
201 elsif ($day == $future_dom) {
202 $tag='month-calendar-day-future';
205 $tag='month-calendar-day-nolink';
207 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
211 # finish off the week
212 for (; $wday != $week_start_day; $wday++, $wday %= 7) {
213 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
223 sub format_year (@) {
228 my $future_month = 0;
229 $future_month = $now[4]+1 if ($params{year} == $now[5]+1900);
231 my $archivebase = 'archives';
232 $archivebase = $config{archivebase} if defined $config{archivebase};
233 $archivebase = $params{archivebase} if defined $params{archivebase};
235 # calculate URL's for previous and next years
236 my ($url, $purl, $nurl)=("$params{year}",'','');
237 if (exists $pagesources{"$archivebase/$params{year}"}) {
238 $url = htmllink($params{page}, $params{destpage},
239 "$archivebase/$params{year}",
240 linktext => "$params{year}");
242 add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
243 if (exists $pagesources{"$archivebase/$params{pyear}"}) {
244 $purl = htmllink($params{page}, $params{destpage},
245 "$archivebase/$params{pyear}",
246 linktext => "\←");
248 add_depends($params{page}, "$archivebase/$params{pyear}", deptype("presence"));
249 if (exists $pagesources{"$archivebase/$params{nyear}"}) {
250 $nurl = htmllink($params{page}, $params{destpage},
251 "$archivebase/$params{nyear}",
252 linktext => "\→");
254 add_depends($params{page}, "$archivebase/$params{nyear}", deptype("presence"));
256 # Start producing the year calendar
258 <table class="year-calendar">
259 <caption class="year-calendar-head">
265 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
269 for (my $month = 1; $month <= 12; $month++) {
270 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
272 my $monthname = POSIX::strftime("%B", @day);
273 my $monthabbr = POSIX::strftime("%b", @day);
274 $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
276 my $mtag=sprintf("%02d", $month);
277 if ($month == $params{month}) {
278 if ($pagesources{"$archivebase/$params{year}/$mtag"}) {
279 $tag = 'this_month_link';
282 $tag = 'this_month_nolink';
285 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
288 elsif ($future_month && $month >= $future_month) {
289 $tag = 'month_future';
292 $tag = 'month_nolink';
295 if ($pagesources{"$archivebase/$params{year}/$mtag"}) {
296 $murl = htmllink($params{page}, $params{destpage},
297 "$archivebase/$params{year}/$mtag",
298 linktext => "$monthabbr");
299 $calendar.=qq{\t<td class="$tag">};
301 $calendar.=qq{\t</td>\n};
304 $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
306 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
307 deptype("presence"));
309 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
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
335 $params{year} = 1900 + $now[5] unless defined $params{year};
336 $params{month} = 1 + $now[4] unless defined $params{month};
339 delete $pagestate{$params{destpage}}{calendar};
342 # Calculate month names for next month, and previous months
343 $params{pmonth} = $params{month} - 1;
344 $params{nmonth} = $params{month} + 1;
345 $params{pyear} = $params{year} - 1;
346 $params{nyear} = $params{year} + 1;
348 # Adjust for January and December
349 if ($params{month} == 1) {
350 $params{pmonth} = 12;
353 if ($params{month} == 12) {
359 if ($params{type} =~ /month/i) {
360 $calendar=format_month(%params);
362 elsif ($params{type} =~ /year/i) {
363 $calendar=format_year(%params);
366 return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
370 my $needsbuild=shift;
371 foreach my $page (keys %pagestate) {
372 if (exists $pagestate{$page}{calendar}{nextchange}) {
373 if ($pagestate{$page}{calendar}{nextchange} <= $time) {
374 # force a rebuild so the calendar shows
376 push @$needsbuild, $pagesources{$page};
378 if (exists $pagesources{$page} &&
379 grep { $_ eq $pagesources{$page} } @$needsbuild) {
380 # remove state, will be re-added if
381 # the calendar is still there during the
383 delete $pagestate{$page}{calendar};