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