]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Plugin/calendar.pm
progress: switch to new pagespec_match_list
[git.ikiwiki.info.git] / IkiWiki / Plugin / calendar.pm
1 #! /usr/bin/perl
2 # Copyright (c) 2006, 2007 Manoj Srivastava <srivasta@debian.org>
3 #
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.
8 #
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.
13 #
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
18 require 5.002;
19 package IkiWiki::Plugin::calendar;
21 use warnings;
22 use strict;
23 use IkiWiki 3.00;
24 use Time::Local;
25 use POSIX;
27 my $time=time;
28 my @now=localtime($time);
30 sub import {
31         hook(type => "getsetup", id => "calendar", call => \&getsetup);
32         hook(type => "needsbuild", id => "calendar", call => \&needsbuild);
33         hook(type => "preprocess", id => "calendar", call => \&preprocess);
34 }
36 sub getsetup () {
37         return
38                 plugin => {
39                         safe => 1,
40                         rebuild => undef,
41                 },
42                 archivebase => {
43                         type => "string",
44                         example => "archives",
45                         description => "base of the archives hierarchy",
46                         safe => 1,
47                         rebuild => 1,
48                 },
49 }
51 sub is_leap_year (@) {
52         my %params=@_;
53         return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
54 }
56 sub month_days {
57         my %params=@_;
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)) {
60                 $days_in_month++;
61         }
62         return $days_in_month;
63 }
65 sub format_month (@) {
66         my %params=@_;
68         my $pagespec = $params{pages};
69         my $year     = $params{year};
70         my $month    = $params{month};
71         my $pmonth   = $params{pmonth};
72         my $nmonth   = $params{nmonth};
73         my $pyear    = $params{pyear};
74         my $nyear    = $params{nyear};
76         my %linkcache;
77         foreach my $p (pagespec_match_list($params{page}, $params{pagespec},
78                                 # add presence dependencies to update
79                                 # month calendar when pages are added/removed
80                                 deptype => deptype("presence"))) {
81                 my $mtime = $IkiWiki::pagectime{$p};
82                 my $src   = $pagesources{$p};
83                 my @date  = localtime($mtime);
84                 my $mday  = $date[3];
85                 my $month = $date[4] + 1;
86                 my $year  = $date[5] + 1900;
87                 my $mtag  = sprintf("%02d", $month);
89                 # Only one posting per day is being linked to.
90                 $linkcache{"$year/$mtag/$mday"} = "$src";
91         }
93         my @list;
94         my $calendar="\n";
96         # When did this month start?
97         my @monthstart = localtime(timelocal(0,0,0,1,$month-1,$year-1900));
99         my $future_dom = 0;
100         my $today      = 0;
101         if ($year == $now[5]+1900 && $month == $now[4]+1) {
102                 $future_dom = $now[3]+1;
103                 $today      = $now[3];
104         }
106         # Find out month names for this, next, and previous months
107         my $monthname=POSIX::strftime("%B", @monthstart);
108         my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
109         my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
111         my $archivebase = 'archives';
112         $archivebase = $config{archivebase} if defined $config{archivebase};
113         $archivebase = $params{archivebase} if defined $params{archivebase};
114   
115         # Calculate URL's for monthly archives.
116         my ($url, $purl, $nurl)=("$monthname",'','');
117         if (exists $pagesources{"$archivebase/$year/$month"}) {
118                 $url = htmllink($params{page}, $params{destpage}, 
119                         "$archivebase/$year/".sprintf("%02d", $month),
120                         linktext => " $monthname ");
121         }
122         add_depends($params{page}, "$archivebase/$year/".sprintf("%02d", $month),
123                 deptype("presence"));
124         if (exists $pagesources{"$archivebase/$pyear/$pmonth"}) {
125                 $purl = htmllink($params{page}, $params{destpage}, 
126                         "$archivebase/$pyear/" . sprintf("%02d", $pmonth),
127                         linktext => " $pmonthname ");
128         }
129         add_depends($params{page}, "$archivebase/$pyear/".sprintf("%02d", $pmonth),
130                 deptype("presence"));
131         if (exists $pagesources{"$archivebase/$nyear/$nmonth"}) {
132                 $nurl = htmllink($params{page}, $params{destpage}, 
133                         "$archivebase/$nyear/" . sprintf("%02d", $nmonth),
134                         linktext => " $nmonthname ");
135         }
136         add_depends($params{page}, "$archivebase/$nyear/".sprintf("%02d", $nmonth),
137                 deptype("presence"));
139         # Start producing the month calendar
140         $calendar=<<EOF;
141 <table class="month-calendar">
142         <caption class="month-calendar-head">
143         $purl
144         $url
145         $nurl
146         </caption>
147         <tr>
148 EOF
150         # Suppose we want to start the week with day $week_start_day
151         # If $monthstart[6] == 1
152         my $week_start_day = $params{week_start_day};
154         my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
155         my %downame;
156         my %dowabbr;
157         for my $dow ($week_start_day..$week_start_day+6) {
158                 my @day=localtime(timelocal(0,0,0,$start_day++,$month-1,$year-1900));
159                 my $downame = POSIX::strftime("%A", @day);
160                 my $dowabbr = POSIX::strftime("%a", @day);
161                 $downame{$dow % 7}=$downame;
162                 $dowabbr{$dow % 7}=$dowabbr;
163                 $calendar.= qq{\t\t<th class="month-calendar-day-head $downame">$dowabbr</th>\n};
164         }
166         $calendar.=<<EOF;
167         </tr>
168 EOF
170         my $wday;
171         # we start with a week_start_day, and skip until we get to the first
172         for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
173                 $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
174                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
175         }
177         # At this point, either the first is a week_start_day, in which case
178         # nothing has been printed, or else we are in the middle of a row.
179         for (my $day = 1; $day <= month_days(year => $year, month => $month);
180              $day++, $wday++, $wday %= 7) {
181                 # At tihs point, on a week_start_day, we close out a row,
182                 # and start a new one -- unless it is week_start_day on the
183                 # first, where we do not close a row -- since none was started.
184                 if ($wday == $week_start_day) {
185                         $calendar.=qq{\t</tr>\n} unless $day == 1;
186                         $calendar.=qq{\t<tr>\n};
187                 }
188                 
189                 my $tag;
190                 my $mtag = sprintf("%02d", $month);
191                 if (defined $pagesources{"$archivebase/$year/$mtag/$day"}) {
192                         if ($day == $today) {
193                                 $tag='month-calendar-day-this-day';
194                         }
195                         else {
196                                 $tag='month-calendar-day-link';
197                         }
198                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
199                         $calendar.=htmllink($params{page}, $params{destpage}, 
200                                             pagename($linkcache{"$year/$mtag/$day"}),
201                                             "linktext" => "$day");
202                         push @list, pagename($linkcache{"$year/$mtag/$day"});
203                         $calendar.=qq{</td>\n};
204                 }
205                 else {
206                         if ($day == $today) {
207                                 $tag='month-calendar-day-this-day';
208                         }
209                         elsif ($day == $future_dom) {
210                                 $tag='month-calendar-day-future';
211                         }
212                         else {
213                                 $tag='month-calendar-day-nolink';
214                         }
215                         $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
216                 }
217         }
219         # finish off the week
220         for (; $wday != $week_start_day; $wday++, $wday %= 7) {
221                 $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
222         }
223         $calendar.=<<EOF;
224         </tr>
225 </table>
226 EOF
228         return $calendar;
231 sub format_year (@) {
232         my %params=@_;
234         my $pagespec = $params{pages};
235         my $year     = $params{year};
236         my $month    = $params{month};
237         my $pmonth   = $params{pmonth};
238         my $nmonth   = $params{nmonth};
239         my $pyear    = $params{pyear};
240         my $nyear    = $params{nyear};
242         my $calendar="\n";
244         my $future_month = 0;
245         $future_month = $now[4]+1 if ($year == $now[5]+1900);
247         my $archivebase = 'archives';
248         $archivebase = $config{archivebase} if defined $config{archivebase};
249         $archivebase = $params{archivebase} if defined $params{archivebase};
251         # calculate URL's for previous and next years
252         my ($url, $purl, $nurl)=("$year",'','');
253         if (exists $pagesources{"$archivebase/$year"}) {
254                 $url = htmllink($params{page}, $params{destpage}, 
255                         "$archivebase/$year",
256                         linktext => "$year");
257         }
258         add_depends($params{page}, "$archivebase/$year", deptype("presence"));
259         if (exists $pagesources{"$archivebase/$pyear"}) {
260                 $purl = htmllink($params{page}, $params{destpage}, 
261                         "$archivebase/$pyear",
262                         linktext => "\&larr;");
263         }
264         add_depends($params{page}, "$archivebase/$pyear", deptype("presence"));
265         if (exists $pagesources{"$archivebase/$nyear"}) {
266                 $nurl = htmllink($params{page}, $params{destpage}, 
267                         "$archivebase/$nyear",
268                         linktext => "\&rarr;");
269         }
270         add_depends($params{page}, "$archivebase/$nyear", deptype("presence"));
272         # Start producing the year calendar
273         $calendar=<<EOF;
274 <table class="year-calendar">
275         <caption class="year-calendar-head">
276         $purl
277         $url
278         $nurl
279         </caption>
280         <tr>
281                 <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
282         </tr>
283 EOF
285         for ($month = 1; $month <= 12; $month++) {
286                 my @day=localtime(timelocal(0,0,0,15,$month-1,$year-1900));
287                 my $murl;
288                 my $monthname = POSIX::strftime("%B", @day);
289                 my $monthabbr = POSIX::strftime("%b", @day);
290                 $calendar.=qq{\t<tr>\n}  if ($month % $params{months_per_row} == 1);
291                 my $tag;
292                 my $mtag=sprintf("%02d", $month);
293                 if ($month == $params{month}) {
294                         if ($pagesources{"$archivebase/$year/$mtag"}) {
295                                 $tag = 'this_month_link';
296                         }
297                         else {
298                                 $tag = 'this_month_nolink';
299                         }
300                 }
301                 elsif ($pagesources{"$archivebase/$year/$mtag"}) {
302                         $tag = 'month_link';
303                 } 
304                 elsif ($future_month && $month >= $future_month) {
305                         $tag = 'month_future';
306                 } 
307                 else {
308                         $tag = 'month_nolink';
309                 }
311                 if ($pagesources{"$archivebase/$year/$mtag"}) {
312                         $murl = htmllink($params{page}, $params{destpage}, 
313                                 "$archivebase/$year/$mtag",
314                                 linktext => "$monthabbr");
315                         $calendar.=qq{\t<td class="$tag">};
316                         $calendar.=$murl;
317                         $calendar.=qq{\t</td>\n};
318                 }
319                 else {
320                         $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
321                 }
322                 add_depends($params{page}, "$archivebase/$year/$mtag",
323                         deptype("presence"));
325                 $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
326         }
328         $calendar.=<<EOF;
329 </table>
330 EOF
332         return $calendar;
335 sub preprocess (@) {
336         my %params=@_;
337         $params{pages} = "*"            unless defined $params{pages};
338         $params{type}  = "month"        unless defined $params{type};
339         $params{month} = sprintf("%02d", $params{month}) if defined  $params{month};
340         $params{week_start_day} = 0     unless defined $params{week_start_day};
341         $params{months_per_row} = 3     unless defined $params{months_per_row};
343         if (! defined $params{year} || ! defined $params{month}) {
344                 # Record that the calendar next changes at midnight.
345                 $pagestate{$params{destpage}}{calendar}{nextchange}=($time
346                         + (60 - $now[0])                # seconds
347                         + (59 - $now[1]) * 60           # minutes
348                         + (23 - $now[2]) * 60 * 60      # hours
349                 );
350                 
351                 $params{year}  = 1900 + $now[5] unless defined $params{year};
352                 $params{month} = 1    + $now[4] unless defined $params{month};
353         }
354         else {
355                 delete $pagestate{$params{destpage}}{calendar};
356         }
358         # Calculate month names for next month, and previous months
359         my $pmonth = $params{month} - 1;
360         my $nmonth = $params{month} + 1;
361         my $pyear  = $params{year}  - 1;
362         my $nyear  = $params{year}  + 1;
364         # Adjust for January and December
365         if ($params{month} == 1) {
366                 $pmonth = 12;
367                 $pyear--;
368         }
369         if ($params{month} == 12) {
370                 $nmonth = 1;
371                 $nyear++;
372         }
374         $params{pmonth}=$pmonth;
375         $params{nmonth}=$nmonth;
376         $params{pyear} =$pyear;
377         $params{nyear} =$nyear;
379         my $calendar="\n";
381         if ($params{type} =~ /month/i) {
382                 $calendar=format_month(%params);
383         }
384         elsif ($params{type} =~ /year/i) {
385                 $calendar=format_year(%params);
386         }
388         return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
389 } #}}
391 sub needsbuild (@) {
392         my $needsbuild=shift;
393         foreach my $page (keys %pagestate) {
394                 if (exists $pagestate{$page}{calendar}{nextchange}) {
395                         if ($pagestate{$page}{calendar}{nextchange} <= $time) {
396                                 # force a rebuild so the calendar shows
397                                 # the current day
398                                 push @$needsbuild, $pagesources{$page};
399                         }
400                         if (exists $pagesources{$page} && 
401                             grep { $_ eq $pagesources{$page} } @$needsbuild) {
402                                 # remove state, will be re-added if
403                                 # the calendar is still there during the
404                                 # rebuild
405                                 delete $pagestate{$page}{calendar};
406                         }
407                 }
408         }