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);
45 example => "archives",
46 description => "base of the archives hierarchy",
52 sub is_leap_year (@) {
54 return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
59 my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
60 if ($params{month} == 2 && is_leap_year(%params)) {
63 return $days_in_month;
66 sub format_month (@) {
70 foreach my $p (pagespec_match_list($params{page},
71 "creation_year($params{year}) and creation_month($params{month}) and ($params{pages})",
72 # add presence dependencies to update
73 # month calendar when pages are added/removed
74 deptype => deptype("presence"))) {
75 my $mtime = $IkiWiki::pagectime{$p};
76 my @date = localtime($mtime);
78 my $month = $date[4] + 1;
79 my $year = $date[5] + 1900;
80 my $mtag = sprintf("%02d", $month);
82 # Only one posting per day is being linked to.
83 $linkcache{"$year/$mtag/$mday"} = $p;
86 my $pmonth = $params{month} - 1;
87 my $nmonth = $params{month} + 1;
88 my $pyear = $params{year};
89 my $nyear = $params{year};
91 # Adjust for January and December
92 if ($params{month} == 1) {
96 if ($params{month} == 12) {
102 $pmonth=sprintf("%02d", $pmonth);
103 $nmonth=sprintf("%02d", $nmonth);
107 # When did this month start?
108 my @monthstart = localtime(timelocal(0,0,0,1,$params{month}-1,$params{year}-1900));
112 if ($params{year} == $now[5]+1900 && $params{month} == $now[4]+1) {
113 $future_dom = $now[3]+1;
117 # Find out month names for this, next, and previous months
118 my $monthname=POSIX::strftime("%B", @monthstart);
119 my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
120 my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
122 my $archivebase = 'archives';
123 $archivebase = $config{archivebase} if defined $config{archivebase};
124 $archivebase = $params{archivebase} if defined $params{archivebase};
126 # Calculate URL's for monthly archives.
127 my ($url, $purl, $nurl)=("$monthname",'','');
128 if (exists $pagesources{"$archivebase/$params{year}/$params{month}"}) {
129 $url = htmllink($params{page}, $params{destpage},
130 "$archivebase/$params{year}/".$params{month},
132 linktext => $monthname,
133 title => $monthname);
135 add_depends($params{page}, "$archivebase/$params{year}/$params{month}",
136 deptype("presence"));
137 if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
138 $purl = htmllink($params{page}, $params{destpage},
139 "$archivebase/$pyear/$pmonth",
141 linktext => "\←",
142 title => $pmonthname);
144 add_depends($params{page}, "$archivebase/$pyear/$pmonth",
145 deptype("presence"));
146 if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
147 $nurl = htmllink($params{page}, $params{destpage},
148 "$archivebase/$nyear/$nmonth",
150 linktext => "\→",
151 title => $nmonthname);
153 add_depends($params{page}, "$archivebase/$nyear/$nmonth",
154 deptype("presence"));
156 # Start producing the month calendar
158 <table class="month-calendar">
159 <caption class="month-calendar-head">
167 # Suppose we want to start the week with day $week_start_day
168 # If $monthstart[6] == 1
169 my $week_start_day = $params{week_start_day};
171 my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
174 for my $dow ($week_start_day..$week_start_day+6) {
175 my @day=localtime(timelocal(0,0,0,$start_day++,$params{month}-1,$params{year}-1900));
176 my $downame = POSIX::strftime("%A", @day);
177 my $dowabbr = POSIX::strftime("%a", @day);
178 $downame{$dow % 7}=$downame;
179 $dowabbr{$dow % 7}=$dowabbr;
180 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame" title="$downame">$dowabbr</th>\n};
188 # we start with a week_start_day, and skip until we get to the first
189 for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
190 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
191 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
194 # At this point, either the first is a week_start_day, in which case
195 # nothing has been printed, or else we are in the middle of a row.
196 for (my $day = 1; $day <= month_days(year => $params{year}, month => $params{month});
197 $day++, $wday++, $wday %= 7) {
198 # At this point, on a week_start_day, we close out a row,
199 # and start a new one -- unless it is week_start_day on the
200 # first, where we do not close a row -- since none was started.
201 if ($wday == $week_start_day) {
202 $calendar.=qq{\t</tr>\n} unless $day == 1;
203 $calendar.=qq{\t<tr>\n};
207 my $key="$params{year}/$params{month}/$day";
208 if (defined $linkcache{$key}) {
209 if ($day == $today) {
210 $tag='month-calendar-day-this-day';
213 $tag='month-calendar-day-link';
215 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
216 $calendar.=htmllink($params{page}, $params{destpage},
220 title => pagetitle(IkiWiki::basename($linkcache{$key})));
221 $calendar.=qq{</td>\n};
224 if ($day == $today) {
225 $tag='month-calendar-day-this-day';
227 elsif ($day == $future_dom) {
228 $tag='month-calendar-day-future';
231 $tag='month-calendar-day-nolink';
233 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
237 # finish off the week
238 for (; $wday != $week_start_day; $wday++, $wday %= 7) {
239 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
249 sub format_year (@) {
253 foreach my $p (pagespec_match_list($params{page},
254 "creation_year($params{year}) and ($params{pages})",
255 # add presence dependencies to update
256 # year calendar's links to months when
257 # pages are added/removed
258 deptype => deptype("presence"))) {
259 my $mtime = $IkiWiki::pagectime{$p};
260 my @date = localtime($mtime);
261 my $month = $date[4] + 1;
263 $post_months[$month]++;
268 my $pyear = $params{year} - 1;
269 my $nyear = $params{year} + 1;
271 my $thisyear = $now[5]+1900;
272 my $future_month = 0;
273 $future_month = $now[4]+1 if $params{year} == $thisyear;
275 my $archivebase = 'archives';
276 $archivebase = $config{archivebase} if defined $config{archivebase};
277 $archivebase = $params{archivebase} if defined $params{archivebase};
279 # calculate URL's for previous and next years
280 my ($url, $purl, $nurl)=("$params{year}",'','');
281 if (exists $pagesources{"$archivebase/$params{year}"}) {
282 $url = htmllink($params{page}, $params{destpage},
283 "$archivebase/$params{year}",
285 linktext => $params{year},
286 title => $params{year});
288 add_depends($params{page}, "$archivebase/$params{year}", deptype("presence"));
289 if (exists $pagesources{"$archivebase/$pyear"}) {
290 $purl = htmllink($params{page}, $params{destpage},
291 "$archivebase/$pyear",
293 linktext => "\←",
296 add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
297 if (exists $pagesources{"$archivebase/$nyear"}) {
298 $nurl = htmllink($params{page}, $params{destpage},
299 "$archivebase/$nyear",
301 linktext => "\→",
304 add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
306 # Start producing the year calendar
308 <table class="year-calendar">
309 <caption class="year-calendar-head">
315 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
319 for (my $month = 1; $month <= 12; $month++) {
320 my @day=localtime(timelocal(0,0,0,15,$month-1,$params{year}-1900));
322 my $monthname = POSIX::strftime("%B", @day);
323 my $monthabbr = POSIX::strftime("%b", @day);
324 $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
326 my $mtag=sprintf("%02d", $month);
327 if ($month == $params{month} && $thisyear == $params{year}) {
328 $tag = 'year-calendar-this-month';
330 elsif ($pagesources{"$archivebase/$params{year}/$mtag"}) {
331 $tag = 'year-calendar-month-link';
333 elsif ($future_month && $month >= $future_month) {
334 $tag = 'year-calendar-month-future';
337 $tag = 'year-calendar-month-nolink';
340 if ($pagesources{"$archivebase/$params{year}/$mtag"} &&
341 $post_months[$mtag]) {
342 $murl = htmllink($params{page}, $params{destpage},
343 "$archivebase/$params{year}/$mtag",
345 linktext => $monthabbr,
346 title => $monthname);
347 $calendar.=qq{\t<td class="$tag">};
349 $calendar.=qq{\t</td>\n};
352 $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
354 add_depends($params{page}, "$archivebase/$params{year}/$mtag",
355 deptype("presence"));
357 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
370 my $thisyear=1900 + $now[5];
371 my $thismonth=1 + $now[4];
373 $params{pages} = "*" unless defined $params{pages};
374 $params{type} = "month" unless defined $params{type};
375 $params{week_start_day} = 0 unless defined $params{week_start_day};
376 $params{months_per_row} = 3 unless defined $params{months_per_row};
377 $params{year} = $thisyear unless defined $params{year};
378 $params{month} = $thismonth unless defined $params{month};
380 $params{month} = sprintf("%02d", $params{month});
382 if ($params{type} eq 'month' && $params{year} == $thisyear
383 && $params{month} == $thismonth) {
384 # calendar for current month, updates next midnight
385 $pagestate{$params{destpage}}{calendar}{nextchange}=($time
386 + (60 - $now[0]) # seconds
387 + (59 - $now[1]) * 60 # minutes
388 + (23 - $now[2]) * 60 * 60 # hours
391 elsif ($params{type} eq 'month' &&
392 (($params{year} == $thisyear && $params{month} > $thismonth) ||
393 $params{year} > $thisyear)) {
394 # calendar for upcoming month, updates 1st of that month
395 $pagestate{$params{destpage}}{calendar}{nextchange}=
396 timelocal(0, 0, 0, 1, $params{month}-1, $params{year});
398 elsif ($params{type} eq 'year' && $params{year} == $thisyear) {
399 # calendar for current year, updates 1st of next month
400 if ($thismonth < 12) {
401 $pagestate{$params{destpage}}{calendar}{nextchange}=
402 timelocal(0, 0, 0, 1, $thismonth+1-1, $params{year});
405 $pagestate{$params{destpage}}{calendar}{nextchange}=
406 timelocal(0, 0, 0, 1, 1-1, $params{year}+1);
409 elsif ($params{type} eq 'year' && $params{year} > $thisyear) {
410 # calendar for upcoming year, updates 1st of that year
411 $pagestate{$params{destpage}}{calendar}{nextchange}=
412 timelocal(0, 0, 0, 1, 1-1, $params{year});
415 # calendar for past month or year, does not need
417 delete $pagestate{$params{destpage}}{calendar};
420 # Calculate month names for next month, and previous months
422 if ($params{type} eq 'month') {
423 $calendar=format_month(%params);
425 elsif ($params{type} eq 'year') {
426 $calendar=format_year(%params);
429 return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
433 my $needsbuild=shift;
434 foreach my $page (keys %pagestate) {
435 if (exists $pagestate{$page}{calendar}{nextchange}) {
436 if ($pagestate{$page}{calendar}{nextchange} <= $time) {
437 # force a rebuild so the calendar shows
439 push @$needsbuild, $pagesources{$page};
441 if (exists $pagesources{$page} &&
442 grep { $_ eq $pagesources{$page} } @$needsbuild) {
443 # remove state, will be re-added if
444 # the calendar is still there during the
446 delete $pagestate{$page}{calendar};