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;
31 hook(type => "preprocess", id => "calendar", call => \&preprocess);
34 sub is_leap_year (@) { #{{
36 return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
41 my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
42 if ($params{month} == 2 && is_leap_year(%params)) {
45 return $days_in_month;
48 sub format_month (@) { #{{{
51 my $pagespec = $params{pages};
52 my $year = $params{year};
53 my $month = $params{month};
54 my $pmonth = $params{pmonth};
55 my $nmonth = $params{nmonth};
56 my $pyear = $params{pyear};
57 my $nyear = $params{nyear};
62 # When did this month start?
63 my @monthstart = localtime(timelocal(0,0,0,1,$month-1,$year-1900));
67 if ($year == $now[5]+1900 && $month == $now[4]+1) {
68 $future_dom = $now[3]+1;
72 # Find out month names for this, next, and previous months
73 my $monthname=POSIX::strftime("%B", @monthstart);
74 my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
75 my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
77 my $archivebase = 'archives';
78 $archivebase = $config{archivebase} if defined $config{archivebase};
79 $archivebase = $params{archivebase} if defined $params{archivebase};
81 # Calculate URL's for monthly archives.
82 my ($url, $purl, $nurl)=("$monthname",'','');
83 if (exists $cache{$pagespec}{"$year/$month"}) {
84 $url = htmllink($params{page}, $params{destpage},
85 "$archivebase/$year/".sprintf("%02d", $month),
86 linktext => " $monthname ");
88 add_depends($params{page}, "$archivebase/$year/".sprintf("%02d", $month));
89 if (exists $cache{$pagespec}{"$pyear/$pmonth"}) {
90 $purl = htmllink($params{page}, $params{destpage},
91 "$archivebase/$pyear/" . sprintf("%02d", $pmonth),
92 linktext => " $pmonthname ");
94 add_depends($params{page}, "$archivebase/$pyear/".sprintf("%02d", $pmonth));
95 if (exists $cache{$pagespec}{"$nyear/$nmonth"}) {
96 $nurl = htmllink($params{page}, $params{destpage},
97 "$archivebase/$nyear/" . sprintf("%02d", $nmonth),
98 linktext => " $nmonthname ");
100 add_depends($params{page}, "$archivebase/$nyear/".sprintf("%02d", $nmonth));
102 # Start producing the month calendar
104 <table class="month-calendar">
105 <caption class="month-calendar-head">
113 # Suppose we want to start the week with day $week_start_day
114 # If $monthstart[6] == 1
115 my $week_start_day = $params{week_start_day};
117 my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
120 for my $dow ($week_start_day..$week_start_day+6) {
121 my @day=localtime(timelocal(0,0,0,$start_day++,$month-1,$year-1900));
122 my $downame = POSIX::strftime("%A", @day);
123 my $dowabbr = POSIX::strftime("%a", @day);
124 $downame{$dow % 7}=$downame;
125 $dowabbr{$dow % 7}=$dowabbr;
126 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame">$dowabbr</th>\n};
134 # we start with a week_start_day, and skip until we get to the first
135 for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
136 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
137 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
140 # At this point, either the first is a week_start_day, in which case
141 # nothing has been printed, or else we are in the middle of a row.
142 for (my $day = 1; $day <= month_days(year => $year, month => $month);
143 $day++, $wday++, $wday %= 7) {
144 # At tihs point, on a week_start_day, we close out a row,
145 # and start a new one -- unless it is week_start_day on the
146 # first, where we do not close a row -- since none was started.
147 if ($wday == $week_start_day) {
148 $calendar.=qq{\t</tr>\n} unless $day == 1;
149 $calendar.=qq{\t<tr>\n};
153 my $mtag = sprintf("%02d", $month);
154 if (defined $cache{$pagespec}{"$year/$mtag/$day"}) {
155 if ($day == $today) {
156 $tag='month-calendar-day-this-day';
159 $tag='month-calendar-day-link';
161 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
162 $calendar.=htmllink($params{page}, $params{destpage},
163 pagename($linkcache{"$year/$mtag/$day"}),
164 "linktext" => "$day");
165 push @list, pagename($linkcache{"$year/$mtag/$day"});
166 $calendar.=qq{</td>\n};
169 if ($day == $today) {
170 $tag='month-calendar-day-this-day';
172 elsif ($day == $future_dom) {
173 $tag='month-calendar-day-future';
176 $tag='month-calendar-day-nolink';
178 $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
182 # finish off the week
183 for (; $wday != $week_start_day; $wday++, $wday %= 7) {
184 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}"> </td>\n};
191 # Add dependencies to update the calendar whenever pages
192 # matching the pagespec are added or removed.
193 add_depends($params{page}, $params{pages});
194 # Explicitly add all currently linked pages as dependencies, so
195 # that if they are removed, the calendar will be sure to be updated.
196 add_depends($params{page}, join(" or ", @list));
201 sub format_year (@) {
204 my $pagespec = $params{pages};
205 my $year = $params{year};
206 my $month = $params{month};
207 my $pmonth = $params{pmonth};
208 my $nmonth = $params{nmonth};
209 my $pyear = $params{pyear};
210 my $nyear = $params{nyear};
214 my $future_month = 0;
215 $future_month = $now[4]+1 if ($year == $now[5]+1900);
217 my $archivebase = 'archives';
218 $archivebase = $config{archivebase} if defined $config{archivebase};
219 $archivebase = $params{archivebase} if defined $params{archivebase};
221 # calculate URL's for previous and next years
222 my ($url, $purl, $nurl)=("$year",'','');
223 if (exists $cache{$pagespec}{"$year"}) {
224 $url = htmllink($params{page}, $params{destpage},
225 "$archivebase/$year",
226 linktext => "$year");
228 add_depends($params{page}, "$archivebase/$year");
229 if (exists $cache{$pagespec}{"$pyear"}) {
230 $purl = htmllink($params{page}, $params{destpage},
231 "$archivebase/$pyear",
232 linktext => "\←");
234 add_depends($params{page}, "$archivebase/$pyear");
235 if (exists $cache{$pagespec}{"$nyear"}) {
236 $nurl = htmllink($params{page}, $params{destpage},
237 "$archivebase/$nyear",
238 linktext => "\→");
240 add_depends($params{page}, "$archivebase/$nyear");
242 # Start producing the year calendar
244 <table class="year-calendar">
245 <caption class="year-calendar-head">
251 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
255 for ($month = 1; $month <= 12; $month++) {
256 my @day=localtime(timelocal(0,0,0,15,$month-1,$year-1900));
258 my $monthname = POSIX::strftime("%B", @day);
259 my $monthabbr = POSIX::strftime("%b", @day);
260 $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
262 my $mtag=sprintf("%02d", $month);
263 if ($month == $params{month}) {
264 if ($cache{$pagespec}{"$year/$mtag"}) {
265 $tag = 'this_month_link';
268 $tag = 'this_month_nolink';
271 elsif ($cache{$pagespec}{"$year/$mtag"}) {
274 elsif ($future_month && $month >= $future_month) {
275 $tag = 'month_future';
278 $tag = 'month_nolink';
281 if ($cache{$pagespec}{"$year/$mtag"}) {
282 $murl = htmllink($params{page}, $params{destpage},
283 "$archivebase/$year/$mtag",
284 linktext => "$monthabbr");
285 $calendar.=qq{\t<td class="$tag">};
287 $calendar.=qq{\t</td>\n};
290 $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
292 add_depends($params{page}, "$archivebase/$year/$mtag");
294 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
306 $params{pages} = "*" unless defined $params{pages};
307 $params{type} = "month" unless defined $params{type};
308 $params{year} = 1900 + $now[5] unless defined $params{year};
309 $params{month} = sprintf("%02d", $params{month}) if defined $params{month};
310 $params{month} = 1 + $now[4] unless defined $params{month};
311 $params{week_start_day} = 0 unless defined $params{week_start_day};
312 $params{months_per_row} = 3 unless defined $params{months_per_row};
314 # Calculate month names for next month, and previous months
315 my $pmonth = $params{month} - 1;
316 my $nmonth = $params{month} + 1;
317 my $pyear = $params{year} - 1;
318 my $nyear = $params{year} + 1;
320 # Adjust for January and December
321 if ($params{month} == 1) {
325 if ($params{month} == 12) {
330 $params{pmonth}=$pmonth;
331 $params{nmonth}=$nmonth;
332 $params{pyear} =$pyear;
333 $params{nyear} =$nyear;
336 my $pagespec=$params{pages};
337 my $page =$params{page};
339 if (! defined $cache{$pagespec}) {
340 foreach my $p (keys %pagesources) {
341 next unless pagespec_match($p, $pagespec);
342 my $mtime = $IkiWiki::pagectime{$p};
343 my $src = $pagesources{$p};
344 my @date = localtime($mtime);
346 my $month = $date[4] + 1;
347 my $year = $date[5] + 1900;
348 my $mtag = sprintf("%02d", $month);
350 # Only one posting per day is being linked to.
351 $linkcache{"$year/$mtag/$mday"} = "$src";
352 $cache{$pagespec}{"$year"}++;
353 $cache{$pagespec}{"$year/$mtag"}++;
354 $cache{$pagespec}{"$year/$mtag/$mday"}++;
358 if ($params{type} =~ /month/i) {
359 $calendar=format_month(%params);
361 elsif ($params{type} =~ /year/i) {
362 $calendar=format_year(%params);
365 return "\n<div class=\"calendar\">$calendar</div><!-- calendar -->\n";