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},
70 "creation_year($params{year}) and creation_month($params{month}) and ($params{pages})",
71 # add presence dependencies to update
72 # month calendar when pages are added/removed
73 deptype => deptype("presence"))) {
74 my $mtime = $IkiWiki::pagectime{$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"} = $p;
85 my $pmonth = $params{month} - 1;
86 my $nmonth = $params{month} + 1;
87 my $pyear = $params{year};
88 my $nyear = $params{year};
90 # Adjust for January and December
91 if ($params{month} == 1) {
95 if ($params{month} == 12) {
101 $pmonth=sprintf("%02d", $pmonth);
102 $nmonth=sprintf("%02d", $nmonth);
106 # When did this month start?
107 my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
111 if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
112 $future_dom = $now[3]+1;
116 # Find out month names for this, next, and previous months
117 my $monthname=POSIX::strftime("%B", @monthstart);
118 my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
119 my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
121 my $archivebase = 'archives';
122 $archivebase = $config{archivebase} if defined $config{archivebase};
123 $archivebase = $params{archivebase} if defined $params{archivebase};
125 # Calculate URL's for monthly archives.
126 my ($url, $purl, $nurl)=("$monthname",'','');
127 if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
128 $url = htmllink($params{page}, $params{destpage},
129 "$archivebase/$params{year}/".$params{month},
131 linktext => $monthname,
132 title => $monthname);
134 add_depends($params{page}, "$archivebase/$params{year}/$params{month}",
135 deptype("presence"));
136 if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
137 $purl = htmllink($params{page}, $params{destpage},
138 "$archivebase/$pyear/$pmonth",
140 linktext => "\←",
141 title => $pmonthname);
143 add_depends($params{page}, "$archivebase/$pyear/$pmonth",
144 deptype("presence"));
145 if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
146 $nurl = htmllink($params{page}, $params{destpage},
147 "$archivebase/$nyear/$nmonth",
149 linktext => "\→",
150 title => $nmonthname);
152 add_depends($params{page}, "$archivebase/$nyear/$nmonth",
153 deptype("presence"));
155 # Start producing the month calendar
157 <table class="month-calendar">
158 <caption class="month-calendar-head">
166 # Suppose we want to start the week with day $week_start_day
167 # If $monthstart[6] == 1
168 my $week_start_day = $params{week_start_day};
170 my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
173 for my $dow ($week_start_day..$week_start_day+6) {
174 my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
175 my $downame = POSIX::strftime("%A", @day);
176 my $dowabbr = POSIX::strftime("%a", @day);
177 $downame{$dow % 7}=$downame;
178 $dowabbr{$dow % 7}=$dowabbr;
179 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame" title="$downame">$dowabbr</th>\n};
187 # we start with a week_start_day, and skip until we get to the first
188 for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
189 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
190 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
193 # At this point, either the first is a week_start_day, in which case
194 # nothing has been printed, or else we are in the middle of a row.
195 for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
196 $day++, $wday++, $wday %= 7) {
197 # At this point, on a week_start_day, we close out a row,
198 # and start a new one -- unless it is week_start_day on the
199 # first, where we do not close a row -- since none was started.
200 if ($wday == $week_start_day) {
201 $calendar.=qq{\t</tr>\n} unless $day == 1;
202 $calendar.=qq{\t<tr>\n};
206 my $key="$params{year}/$params{month}/$day";
207 if (defined $linkcache{$key}) {
208 if ($day == $today) {
209 $tag='month-calendar-day-this-day';
212 $tag='month-calendar-day-link';
214 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
215 $calendar.=htmllink($params{page}, $params{destpage},
219 title => pagetitle(IkiWiki::basename($linkcache{$key})));
220 $calendar.=qq{</td>\n};
223 if ($day == $today) {
224 $tag='month-calendar-day-this-day';
226 elsif ($day == $future_dom) {
227 $tag='month-calendar-day-future';
230 $tag='month-calendar-day-nolink';
232 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
236 # finish off the week
237 for (; $wday != $week_start_day; $wday++, $wday %= 7) {
238 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
248 sub format_year (@) {
252 foreach my $p (pagespec_match_list($params{page},
253 "creation_year($params{year}) and ($params{pages})",
254 # add presence dependencies to update
255 # year calendar's links to months when
256 # pages are added/removed
257 deptype => deptype("presence"))) {
258 my $mtime = $IkiWiki::pagectime{$p};
259 my @date = localtime($mtime);
260 my $month = $date[4] + 1;
262 $post_months[$month]++;
267 my $pyear = $params{year} - 1;
268 my $nyear = $params{year} + 1;
270 my $thisyear = $now[5]+1900;
271 my $future_month = 0;
272 $future_month = $now[4]+1 if $params{year} == $thisyear;
274 my $archivebase = 'archives';
275 $archivebase = $config{archivebase} if defined $config{archivebase};
276 $archivebase = $params{archivebase} if defined $params{archivebase};
278 # calculate URL's for previous and next years
279 my ($url, $purl, $nurl)=("$params{year}",'','');
280 if (exists $pagesources{"$archivebase/$params{year}"}) {
281 $url = htmllink($params{page}, $params{destpage},
282 "$archivebase/$params{year}",
284 linktext => $params{year},
285 title => $params{year});
287 add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
288 if (exists $pagesources{"$archivebase/$pyear"}) {
289 $purl = htmllink($params{page}, $params{destpage},
290 "$archivebase/$pyear",
292 linktext => "\←",
295 add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
296 if (exists $pagesources{"$archivebase/$nyear"}) {
297 $nurl = htmllink($params{page}, $params{destpage},
298 "$archivebase/$nyear",
300 linktext => "\→",
303 add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
305 # Start producing the year calendar
307 <table class="year-calendar">
308 <caption class="year-calendar-head">
314 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
318 for (my $month = 1; $month <= 12; $month++) {
319 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
321 my $monthname = POSIX::strftime("%B", @day);
322 my $monthabbr = POSIX::strftime("%b", @day);
323 $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
325 my $mtag=sprintf("%02d", $month);
326 if ($month == $params{month} && $thisyear == $params{year}) {
327 $tag = 'year-calendar-this-month';
329 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
330 $tag = 'year-calendar-month-link';
332 elsif ($future_month && $month >= $future_month) {
333 $tag = 'year-calendar-month-future';
336 $tag = 'year-calendar-month-nolink';
339 if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
340 $post_months[$mtag]) {
341 $murl = htmllink($params{page}, $params{destpage},
342 "$archivebase/$params{year}/$mtag",
344 linktext => $monthabbr,
345 title => $monthname);
346 $calendar.=qq{\t<td class="$tag">};
348 $calendar.=qq{\t</td>\n};
351 $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
353 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
354 deptype("presence"));
356 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
369 my $thisyear=1900 + $now[5];
370 my $thismonth=1 + $now[4];
372 $params{pages} = "*" unless defined $params{pages};
373 $params{type} = "month" unless defined $params{type};
374 $params{week_start_day} = 0 unless defined $params{week_start_day};
375 $params{months_per_row} = 3 unless defined $params{months_per_row};
376 $params{year} = $thisyear unless defined $params{year};
377 $params{month} = $thismonth unless defined $params{month};
379 $params{month} = sprintf("%02d", $params{month});
381 if ($params{type} eq 'month' && $params{year} == $thisyear
382 && $params{month} == $thismonth) {
383 # calendar for current month, updates next midnight
384 $pagestate{$params{destpage}}{calendar}{nextchange}=($time
385 + (60 - $now[0]) # seconds
386 + (59 - $now[1]) * 60 # minutes
387 + (23 - $now[2]) * 60 * 60 # hours
390 elsif ($params{type} eq 'month' &&
391 (($params{year} == $thisyear && $params{month} > $thismonth) ||
392 $params{year} > $thisyear)) {
393 # calendar for upcoming month, updates 1st of that month
394 $pagestate{$params{destpage}}{calendar}{nextchange}=
395 timelocal(0, 0, 0, 1, $params{month}-1, $params{year});
397 elsif ($params{type} eq 'year' && $params{year} == $thisyear) {
398 # calendar for current year, updates 1st of next month
399 if ($thismonth < 12) {
400 $pagestate{$params{destpage}}{calendar}{nextchange}=
401 timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year});
404 $pagestate{$params{destpage}}{calendar}{nextchange}=
405 timelocal(0, 0, 0, 1, 1-1, $params{year}+1);
408 elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
409 # calendar for upcoming year, updates 1st of that year
410 $pagestate{$params{destpage}}{calendar}{nextchange}=
411 timelocal(0, 0, 0, 1, 1-1, $params{year});
414 # calendar for past month or year, does not need
416 delete $pagestate{$params{destpage}}{calendar};
419 # Calculate month names for next month, and previous months
421 if ($params{type} eq 'month') {
422 $calendar=format_month(%params);
424 elsif ($params{type} eq 'year') {
425 $calendar=format_year(%params);
428 return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
432 my $needsbuild=shift;
433 foreach my $page (keys %pagestate) {
434 if (exists $pagestate{$page}{calendar}{nextchange}) {
435 if ($pagestate{$page}{calendar}{nextchange} <= $time) {
436 # force a rebuild so the calendar shows
438 push @$needsbuild, $pagesources{$page};
440 if (exists $pagesources{$page} &&
441 grep { $_ eq $pagesources{$page} } @$needsbuild) {
442 # remove state, will be re-added if
443 # the calendar is still there during the
445 delete $pagestate{$page}{calendar};