summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/calendar.pm
blob: a4a54d2c8845284746d9c3190bd3791c3e1f2af6 (plain)
  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
  17. require 5.002;
  18. package IkiWiki::Plugin::calendar;
  19. use warnings;
  20. use strict;
  21. use IkiWiki 2.00;
  22. use Time::Local;
  23. use POSIX;
  24. my %cache;
  25. my %linkcache;
  26. my @now=localtime();
  27. sub import { #{{{
  28. hook(type => "preprocess", id => "calendar", call => \&preprocess);
  29. } #}}}
  30. sub is_leap_year (@) { #{{{
  31. my %params=@_;
  32. return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
  33. } #}}}
  34. sub month_days { #{{{
  35. my %params=@_;
  36. my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
  37. if ($params{month} == 2 && is_leap_year(%params)) {
  38. $days_in_month++;
  39. }
  40. return $days_in_month;
  41. } #}}}
  42. sub format_month (@) { #{{{
  43. my %params=@_;
  44. my $pagespec = $params{pages};
  45. my $year = $params{year};
  46. my $month = $params{month};
  47. my $pmonth = $params{pmonth};
  48. my $nmonth = $params{nmonth};
  49. my $pyear = $params{pyear};
  50. my $nyear = $params{nyear};
  51. my @list;
  52. my $calendar="\n";
  53. # When did this month start?
  54. my @monthstart = localtime(timelocal(0,0,0,1,$month-1,$year-1900));
  55. my $future_dom = 0;
  56. my $today = 0;
  57. if ($year == $now[5]+1900 && $month == $now[4]+1) {
  58. $future_dom = $now[3]+1;
  59. $today = $now[3];
  60. }
  61. # Find out month names for this, next, and previous months
  62. my $monthname=POSIX::strftime("%B", @monthstart);
  63. my $pmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$pmonth-1,$pyear-1900)));
  64. my $nmonthname=POSIX::strftime("%B", localtime(timelocal(0,0,0,1,$nmonth-1,$nyear-1900)));
  65. my $archivebase = 'archives';
  66. $archivebase = $config{archivebase} if defined $config{archivebase};
  67. $archivebase = $params{archivebase} if defined $params{archivebase};
  68. # Calculate URL's for monthly archives.
  69. my ($url, $purl, $nurl)=("$monthname",'','');
  70. if (exists $cache{$pagespec}{"$year/$month"}) {
  71. $url = htmllink($params{page}, $params{destpage},
  72. "$archivebase/$year/".sprintf("%02d", $month),
  73. linktext => " $monthname ");
  74. }
  75. add_depends($params{page}, "$archivebase/$year/".sprintf("%02d", $month));
  76. if (exists $cache{$pagespec}{"$pyear/$pmonth"}) {
  77. $purl = htmllink($params{page}, $params{destpage},
  78. "$archivebase/$pyear/" . sprintf("%02d", $pmonth),
  79. linktext => " $pmonthname ");
  80. }
  81. add_depends($params{page}, "$archivebase/$pyear/".sprintf("%02d", $pmonth));
  82. if (exists $cache{$pagespec}{"$nyear/$nmonth"}) {
  83. $nurl = htmllink($params{page}, $params{destpage},
  84. "$archivebase/$nyear/" . sprintf("%02d", $nmonth),
  85. linktext => " $nmonthname ");
  86. }
  87. add_depends($params{page}, "$archivebase/$nyear/".sprintf("%02d", $nmonth));
  88. # Start producing the month calendar
  89. $calendar=<<EOF;
  90. <table class="month-calendar">
  91. <caption class="month-calendar-head">
  92. $purl
  93. $url
  94. $nurl
  95. </caption>
  96. <tr>
  97. EOF
  98. # Suppose we want to start the week with day $week_start_day
  99. # If $monthstart[6] == 1
  100. my $week_start_day = $params{week_start_day};
  101. my $start_day = 1 + (7 - $monthstart[6] + $week_start_day) % 7;
  102. my %downame;
  103. my %dowabbr;
  104. for my $dow ($week_start_day..$week_start_day+6) {
  105. my @day=localtime(timelocal(0,0,0,$start_day++,$month-1,$year-1900));
  106. my $downame = POSIX::strftime("%A", @day);
  107. my $dowabbr = POSIX::strftime("%a", @day);
  108. $downame{$dow % 7}=$downame;
  109. $dowabbr{$dow % 7}=$dowabbr;
  110. $calendar.= qq{\t\t<th class="month-calendar-day-head $downame">$dowabbr</th>\n};
  111. }
  112. $calendar.=<<EOF;
  113. </tr>
  114. EOF
  115. my $wday;
  116. # we start with a week_start_day, and skip until we get to the first
  117. for ($wday=$week_start_day; $wday != $monthstart[6]; $wday++, $wday %= 7) {
  118. $calendar.=qq{\t<tr>\n} if $wday == $week_start_day;
  119. $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
  120. }
  121. # At this point, either the first is a week_start_day, in which case
  122. # nothing has been printed, or else we are in the middle of a row.
  123. for (my $day = 1; $day <= month_days(year => $year, month => $month);
  124. $day++, $wday++, $wday %= 7) {
  125. # At tihs point, on a week_start_day, we close out a row,
  126. # and start a new one -- unless it is week_start_day on the
  127. # first, where we do not close a row -- since none was started.
  128. if ($wday == $week_start_day) {
  129. $calendar.=qq{\t</tr>\n} unless $day == 1;
  130. $calendar.=qq{\t<tr>\n};
  131. }
  132. my $tag;
  133. my $mtag = sprintf("%02d", $month);
  134. if (defined $cache{$pagespec}{"$year/$mtag/$day"}) {
  135. if ($day == $today) {
  136. $tag='month-calendar-day-this-day';
  137. }
  138. else {
  139. $tag='month-calendar-day-link';
  140. }
  141. $calendar.=qq{\t\t<td class="$tag $downame{$wday}">};
  142. $calendar.=htmllink($params{page}, $params{destpage},
  143. pagename($linkcache{"$year/$mtag/$day"}),
  144. "linktext" => "$day");
  145. push @list, pagename($linkcache{"$year/$mtag/$day"});
  146. $calendar.=qq{</td>\n};
  147. }
  148. else {
  149. if ($day == $today) {
  150. $tag='month-calendar-day-this-day';
  151. }
  152. elsif ($day == $future_dom) {
  153. $tag='month-calendar-day-future';
  154. }
  155. else {
  156. $tag='month-calendar-day-nolink';
  157. }
  158. $calendar.=qq{\t\t<td class="$tag $downame{$wday}">$day</td>\n};
  159. }
  160. }
  161. # finish off the week
  162. for (; $wday != $week_start_day; $wday++, $wday %= 7) {
  163. $calendar.=qq{\t\t<td class="month-calendar-day-noday $downame{$wday}">&nbsp;</td>\n};
  164. }
  165. $calendar.=<<EOF;
  166. </tr>
  167. </table>
  168. EOF
  169. # Add dependencies to update the calendar whenever pages
  170. # matching the pagespec are added or removed.
  171. add_depends($params{page}, $params{pages});
  172. # Explicitly add all currently linked pages as dependencies, so
  173. # that if they are removed, the calendar will be sure to be updated.
  174. add_depends($params{page}, join(" or ", @list));
  175. return $calendar;
  176. } #}}}
  177. sub format_year (@) { #{{{
  178. my %params=@_;
  179. my $pagespec = $params{pages};
  180. my $year = $params{year};
  181. my $month = $params{month};
  182. my $pmonth = $params{pmonth};
  183. my $nmonth = $params{nmonth};
  184. my $pyear = $params{pyear};
  185. my $nyear = $params{nyear};
  186. my $calendar="\n";
  187. my $future_month = 0;
  188. $future_month = $now[4]+1 if ($year == $now[5]+1900);
  189. my $archivebase = 'archives';
  190. $archivebase = $config{archivebase} if defined $config{archivebase};
  191. $archivebase = $params{archivebase} if defined $params{archivebase};
  192. # calculate URL's for previous and next years
  193. my ($url, $purl, $nurl)=("$year",'','');
  194. if (exists $cache{$pagespec}{"$year"}) {
  195. $url = htmllink($params{page}, $params{destpage},
  196. "$archivebase/$year",
  197. linktext => "$year");
  198. }
  199. add_depends($params{page}, "$archivebase/$year");
  200. if (exists $cache{$pagespec}{"$pyear"}) {
  201. $purl = htmllink($params{page}, $params{destpage},
  202. "$archivebase/$pyear",
  203. linktext => "\&larr;");
  204. }
  205. add_depends($params{page}, "$archivebase/$pyear");
  206. if (exists $cache{$pagespec}{"$nyear"}) {
  207. $nurl = htmllink($params{page}, $params{destpage},
  208. "$archivebase/$nyear",
  209. linktext => "\&rarr;");
  210. }
  211. add_depends($params{page}, "$archivebase/$nyear");
  212. # Start producing the year calendar
  213. $calendar=<<EOF;
  214. <table class="year-calendar">
  215. <caption class="year-calendar-head">
  216. $purl
  217. $url
  218. $nurl
  219. </caption>
  220. <tr>
  221. <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
  222. </tr>
  223. EOF
  224. for ($month = 1; $month <= 12; $month++) {
  225. my @day=localtime(timelocal(0,0,0,15,$month-1,$year-1900));
  226. my $murl;
  227. my $monthname = POSIX::strftime("%B", @day);
  228. my $monthabbr = POSIX::strftime("%b", @day);
  229. $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
  230. my $tag;
  231. my $mtag=sprintf("%02d", $month);
  232. if ($month == $params{month}) {
  233. if ($cache{$pagespec}{"$year/$mtag"}) {
  234. $tag = 'this_month_link';
  235. }
  236. else {
  237. $tag = 'this_month_nolink';
  238. }
  239. }
  240. elsif ($cache{$pagespec}{"$year/$mtag"}) {
  241. $tag = 'month_link';
  242. }
  243. elsif ($future_month && $month >= $future_month) {
  244. $tag = 'month_future';
  245. }
  246. else {
  247. $tag = 'month_nolink';
  248. }
  249. if ($cache{$pagespec}{"$year/$mtag"}) {
  250. $murl = htmllink($params{page}, $params{destpage},
  251. "$archivebase/$year/$mtag",
  252. linktext => "$monthabbr");
  253. $calendar.=qq{\t<td class="$tag">};
  254. $calendar.=$murl;
  255. $calendar.=qq{\t</td>\n};
  256. }
  257. else {
  258. $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
  259. }
  260. add_depends($params{page}, "$archivebase/$year/$mtag");
  261. $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
  262. }
  263. $calendar.=<<EOF;
  264. </table>
  265. EOF
  266. return $calendar;
  267. } #}}}
  268. sub preprocess (@) { #{{{
  269. my %params=@_;
  270. $params{pages} = "*" unless defined $params{pages};
  271. $params{type} = "month" unless defined $params{type};
  272. $params{year} = 1900 + $now[5] unless defined $params{year};
  273. $params{month} = sprintf("%02d", $params{month}) if defined $params{month};
  274. $params{month} = 1 + $now[4] unless defined $params{month};
  275. $params{week_start_day} = 0 unless defined $params{week_start_day};
  276. $params{months_per_row} = 3 unless defined $params{months_per_row};
  277. # Calculate month names for next month, and previous months
  278. my $pmonth = $params{month} - 1;
  279. my $nmonth = $params{month} + 1;
  280. my $pyear = $params{year} - 1;
  281. my $nyear = $params{year} + 1;
  282. # Adjust for January and December
  283. if ($params{month} == 1) {
  284. $pmonth = 12;
  285. $pyear--;
  286. }
  287. if ($params{month} == 12) {
  288. $nmonth = 1;
  289. $nyear++;
  290. }
  291. $params{pmonth}=$pmonth;
  292. $params{nmonth}=$nmonth;
  293. $params{pyear} =$pyear;
  294. $params{nyear} =$nyear;
  295. my $calendar="\n";
  296. my $pagespec=$params{pages};
  297. my $page =$params{page};
  298. if (! defined $cache{$pagespec}) {
  299. foreach my $p (keys %pagesources) {
  300. next unless pagespec_match($p, $pagespec);
  301. my $mtime = $IkiWiki::pagectime{$p};
  302. my $src = $pagesources{$p};
  303. my @date = localtime($mtime);
  304. my $mday = $date[3];
  305. my $month = $date[4] + 1;
  306. my $year = $date[5] + 1900;
  307. my $mtag = sprintf("%02d", $month);
  308. # Only one posting per day is being linked to.
  309. $linkcache{"$year/$mtag/$mday"} = "$src";
  310. $cache{$pagespec}{"$year"}++;
  311. $cache{$pagespec}{"$year/$mtag"}++;
  312. $cache{$pagespec}{"$year/$mtag/$mday"}++;
  313. }
  314. }
  315. if ($params{type} =~ /month/i) {
  316. $calendar=format_month(%params);
  317. }
  318. elsif ($params{type} =~ /year/i) {
  319. $calendar=format_year(%params);
  320. }
  321. return "\n<div class=\"calendar\">$calendar</div><!-- calendar -->\n";
  322. } #}}
  323. 1