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