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