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