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 => "version", call => \&getsetup);
34 hook(type => "needsbuild", id => "version", call => \&needsbuild);
35 hook(type => "preprocess", id => "calendar", call => \&preprocess);
38 sub getsetup () { #{{{
42 default => "archives",
43 description => "base of the archives hierarchy",
49 sub is_leap_year (@) { #{{{
51 return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
56 my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
57 if ($params{month} == 2 && is_leap_year(%params)) {
60 return $days_in_month;
63 sub format_month (@) { #{{{
66 my $pagespec = $params{pages};
67 my $year = $params{year};
68 my $month = $params{month};
69 my $pmonth = $params{pmonth};
70 my $nmonth = $params{nmonth};
71 my $pyear = $params{pyear};
72 my $nyear = $params{nyear};
77 # When did this month start?
78 my @monthstart = localtime(timelocal(0,0,0,1,$month-1,$year-1900));
82 if ($year == $now[5]+1900 && $month == $now[4]+1) {
83 $future_dom = $now[3]+1;
87 # Find out month names for this, next, and previous months
88 my $monthname=POSIX::strftime("%B", @monthstart);
89 my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
90 my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
92 my $archivebase = 'archives';
93 $archivebase = $config{archivebase} if defined $config{archivebase};
94 $archivebase = $params{archivebase} if defined $params{archivebase};
96 # Calculate URL's for monthly archives.
97 my ($url, $purl, $nurl)=("$monthname",'','');
98 if (exists $cache{$pagespec}{"$year/$month"}) {
99 $url = htmllink($params{page}, $params{destpage},
100 "$archivebase/$year/".sprintf("%02d", $month),
101 linktext => " $monthname ");
103 add_depends($params{page}, "$archivebase/$year/".sprintf("%02d", $month));
104 if (exists $cache{$pagespec}{"$pyear/$pmonth"}) {
105 $purl = htmllink($params{page}, $params{destpage},
106 "$archivebase/$pyear/" . sprintf("%02d", $pmonth),
107 linktext => " $pmonthname ");
109 add_depends($params{page}, "$archivebase/$pyear/".sprintf("%02d", $pmonth));
110 if (exists $cache{$pagespec}{"$nyear/$nmonth"}) {
111 $nurl = htmllink($params{page}, $params{destpage},
112 "$archivebase/$nyear/" . sprintf("%02d", $nmonth),
113 linktext => " $nmonthname ");
115 add_depends($params{page}, "$archivebase/$nyear/".sprintf("%02d", $nmonth));
117 # Start producing the month calendar
119 <table class="month-calendar">
120 <caption class="month-calendar-head">
128 # Suppose we want to start the week with day $week_start_day
129 # If $monthstart[6] == 1
130 my $week_start_day = $params{week_start_day};
132 my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
135 for my $dow ($week_start_day..$week_start_day+6) {
136 my @day=localtime(timelocal(0,0,0,$start_day++,$month-1,$year-1900));
137 my $downame = POSIX::strftime("%A", @day);
138 my $dowabbr = POSIX::strftime("%a", @day);
139 $downame{$dow % 7}=$downame;
140 $dowabbr{$dow % 7}=$dowabbr;
141 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame">$dowabbr</th>\n};
149 # we start with a week_start_day, and skip until we get to the first
150 for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
151 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
152 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
155 # At this point, either the first is a week_start_day, in which case
156 # nothing has been printed, or else we are in the middle of a row.
157 for (my $day = 1; $day <= month_days(year => $year, month => $month);
158 $day++, $wday++, $wday %= 7) {
159 # At tihs point, on a week_start_day, we close out a row,
160 # and start a new one -- unless it is week_start_day on the
161 # first, where we do not close a row -- since none was started.
162 if ($wday == $week_start_day) {
163 $calendar.=qq{\t</tr>\n} unless $day == 1;
164 $calendar.=qq{\t<tr>\n};
168 my $mtag = sprintf("%02d", $month);
169 if (defined $cache{$pagespec}{"$year/$mtag/$day"}) {
170 if ($day == $today) {
171 $tag='month-calendar-day-this-day';
174 $tag='month-calendar-day-link';
176 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
177 $calendar.=htmllink($params{page}, $params{destpage},
178 pagename($linkcache{"$year/$mtag/$day"}),
179 "linktext" => "$day");
180 push @list, pagename($linkcache{"$year/$mtag/$day"});
181 $calendar.=qq{</td>\n};
184 if ($day == $today) {
185 $tag='month-calendar-day-this-day';
187 elsif ($day == $future_dom) {
188 $tag='month-calendar-day-future';
191 $tag='month-calendar-day-nolink';
193 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
197 # finish off the week
198 for (; $wday != $week_start_day; $wday++, $wday %= 7) {
199 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
206 # Add dependencies to update the calendar whenever pages
207 # matching the pagespec are added or removed.
208 add_depends($params{page}, $params{pages});
209 # Explicitly add all currently linked pages as dependencies, so
210 # that if they are removed, the calendar will be sure to be updated.
211 add_depends($params{page}, join(" or ", @list));
216 sub format_year (@) { #{{{
219 my $pagespec = $params{pages};
220 my $year = $params{year};
221 my $month = $params{month};
222 my $pmonth = $params{pmonth};
223 my $nmonth = $params{nmonth};
224 my $pyear = $params{pyear};
225 my $nyear = $params{nyear};
229 my $future_month = 0;
230 $future_month = $now[4]+1 if ($year == $now[5]+1900);
232 my $archivebase = 'archives';
233 $archivebase = $config{archivebase} if defined $config{archivebase};
234 $archivebase = $params{archivebase} if defined $params{archivebase};
236 # calculate URL's for previous and next years
237 my ($url, $purl, $nurl)=("$year",'','');
238 if (exists $cache{$pagespec}{"$year"}) {
239 $url = htmllink($params{page}, $params{destpage},
240 "$archivebase/$year",
241 linktext => "$year");
243 add_depends($params{page}, "$archivebase/$year");
244 if (exists $cache{$pagespec}{"$pyear"}) {
245 $purl = htmllink($params{page}, $params{destpage},
246 "$archivebase/$pyear",
247 linktext => "\←");
249 add_depends($params{page}, "$archivebase/$pyear");
250 if (exists $cache{$pagespec}{"$nyear"}) {
251 $nurl = htmllink($params{page}, $params{destpage},
252 "$archivebase/$nyear",
253 linktext => "\→");
255 add_depends($params{page}, "$archivebase/$nyear");
257 # Start producing the year calendar
259 <table class="year-calendar">
260 <caption class="year-calendar-head">
266 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
270 for ($month = 1; $month <= 12; $month++) {
271 my @day=localtime(timelocal(0,0,0,15,$month-1,$year-1900));
273 my $monthname = POSIX::strftime("%B", @day);
274 my $monthabbr = POSIX::strftime("%b", @day);
275 $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
277 my $mtag=sprintf("%02d", $month);
278 if ($month == $params{month}) {
279 if ($cache{$pagespec}{"$year/$mtag"}) {
280 $tag = 'this_month_link';
283 $tag = 'this_month_nolink';
286 elsif ($cache{$pagespec}{"$year/$mtag"}) {
289 elsif ($future_month && $month >= $future_month) {
290 $tag = 'month_future';
293 $tag = 'month_nolink';
296 if ($cache{$pagespec}{"$year/$mtag"}) {
297 $murl = htmllink($params{page}, $params{destpage},
298 "$archivebase/$year/$mtag",
299 linktext => "$monthabbr");
300 $calendar.=qq{\t<td class="$tag">};
302 $calendar.=qq{\t</td>\n};
305 $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
307 add_depends($params{page}, "$archivebase/$year/$mtag");
309 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
319 sub preprocess (@) { #{{{
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 my $pmonth = $params{month} - 1;
344 my $nmonth = $params{month} + 1;
345 my $pyear = $params{year} - 1;
346 my $nyear = $params{year} + 1;
348 # Adjust for January and December
349 if ($params{month} == 1) {
353 if ($params{month} == 12) {
358 $params{pmonth}=$pmonth;
359 $params{nmonth}=$nmonth;
360 $params{pyear} =$pyear;
361 $params{nyear} =$nyear;
364 my $pagespec=$params{pages};
365 my $page =$params{page};
367 if (! defined $cache{$pagespec}) {
368 foreach my $p (keys %pagesources) {
369 next unless pagespec_match($p, $pagespec);
370 my $mtime = $IkiWiki::pagectime{$p};
371 my $src = $pagesources{$p};
372 my @date = localtime($mtime);
374 my $month = $date[4] + 1;
375 my $year = $date[5] + 1900;
376 my $mtag = sprintf("%02d", $month);
378 # Only one posting per day is being linked to.
379 $linkcache{"$year/$mtag/$mday"} = "$src";
380 $cache{$pagespec}{"$year"}++;
381 $cache{$pagespec}{"$year/$mtag"}++;
382 $cache{$pagespec}{"$year/$mtag/$mday"}++;
386 if ($params{type} =~ /month/i) {
387 $calendar=format_month(%params);
389 elsif ($params{type} =~ /year/i) {
390 $calendar=format_year(%params);
393 return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
396 sub needsbuild (@) { #{{{
397 my $needsbuild=shift;
398 foreach my $page (keys %pagestate) {
399 if (exists $pagestate{$page}{calendar}{nextchange}) {
400 if ($pagestate{$page}{calendar}{nextchange} <= $time) {
401 # force a rebuild so the calendar shows
403 push @$needsbuild, $pagesources{$page};
405 if (exists $pagesources{$page} &&
406 grep { $_ eq $pagesources{$page} } @$needsbuild) {
407 # remove state, will be re-added if
408 # the calendar is still there during the
410 delete $pagestate{$page}{calendar};