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