summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/calendar.pm
blob: a1117992a865ed3a5cd5a7a8ded409d3b8ace33e (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. presence => 1);
  94. if (exists $cache{$pagespec}{"$pyear/$pmonth"}) {
  95. $purl = htmllink($params{page}, $params{destpage},
  96. "$archivebase/$pyear/" . sprintf("%02d", $pmonth),
  97. linktext => " $pmonthname ");
  98. }
  99. add_depends($params{page}, "$archivebase/$pyear/".sprintf("%02d", $pmonth),
  100. presence => 1);
  101. if (exists $cache{$pagespec}{"$nyear/$nmonth"}) {
  102. $nurl = htmllink($params{page}, $params{destpage},
  103. "$archivebase/$nyear/" . sprintf("%02d", $nmonth),
  104. linktext => " $nmonthname ");
  105. }
  106. add_depends($params{page}, "$archivebase/$nyear/".sprintf("%02d", $nmonth),
  107. presence => 1);
  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}, presence => 1);
  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. foreach my $p (@list) {
  195. add_depends($params{page}, $p, presence => 1);
  196. }
  197. return $calendar;
  198. }
  199. sub format_year (@) {
  200. my %params=@_;
  201. my $pagespec = $params{pages};
  202. my $year = $params{year};
  203. my $month = $params{month};
  204. my $pmonth = $params{pmonth};
  205. my $nmonth = $params{nmonth};
  206. my $pyear = $params{pyear};
  207. my $nyear = $params{nyear};
  208. my $calendar="\n";
  209. my $future_month = 0;
  210. $future_month = $now[4]+1 if ($year == $now[5]+1900);
  211. my $archivebase = 'archives';
  212. $archivebase = $config{archivebase} if defined $config{archivebase};
  213. $archivebase = $params{archivebase} if defined $params{archivebase};
  214. # calculate URL's for previous and next years
  215. my ($url, $purl, $nurl)=("$year",'','');
  216. if (exists $cache{$pagespec}{"$year"}) {
  217. $url = htmllink($params{page}, $params{destpage},
  218. "$archivebase/$year",
  219. linktext => "$year");
  220. }
  221. add_depends($params{page}, "$archivebase/$year", presence => 1);
  222. if (exists $cache{$pagespec}{"$pyear"}) {
  223. $purl = htmllink($params{page}, $params{destpage},
  224. "$archivebase/$pyear",
  225. linktext => "\&larr;");
  226. }
  227. add_depends($params{page}, "$archivebase/$pyear", presence => 1);
  228. if (exists $cache{$pagespec}{"$nyear"}) {
  229. $nurl = htmllink($params{page}, $params{destpage},
  230. "$archivebase/$nyear",
  231. linktext => "\&rarr;");
  232. }
  233. add_depends($params{page}, "$archivebase/$nyear", presence => 1);
  234. # Start producing the year calendar
  235. $calendar=<<EOF;
  236. <table class="year-calendar">
  237. <caption class="year-calendar-head">
  238. $purl
  239. $url
  240. $nurl
  241. </caption>
  242. <tr>
  243. <th class="year-calendar-subhead" colspan="$params{months_per_row}">Months</th>
  244. </tr>
  245. EOF
  246. for ($month = 1; $month <= 12; $month++) {
  247. my @day=localtime(timelocal(0,0,0,15,$month-1,$year-1900));
  248. my $murl;
  249. my $monthname = POSIX::strftime("%B", @day);
  250. my $monthabbr = POSIX::strftime("%b", @day);
  251. $calendar.=qq{\t<tr>\n} if ($month % $params{months_per_row} == 1);
  252. my $tag;
  253. my $mtag=sprintf("%02d", $month);
  254. if ($month == $params{month}) {
  255. if ($cache{$pagespec}{"$year/$mtag"}) {
  256. $tag = 'this_month_link';
  257. }
  258. else {
  259. $tag = 'this_month_nolink';
  260. }
  261. }
  262. elsif ($cache{$pagespec}{"$year/$mtag"}) {
  263. $tag = 'month_link';
  264. }
  265. elsif ($future_month && $month >= $future_month) {
  266. $tag = 'month_future';
  267. }
  268. else {
  269. $tag = 'month_nolink';
  270. }
  271. if ($cache{$pagespec}{"$year/$mtag"}) {
  272. $murl = htmllink($params{page}, $params{destpage},
  273. "$archivebase/$year/$mtag",
  274. linktext => "$monthabbr");
  275. $calendar.=qq{\t<td class="$tag">};
  276. $calendar.=$murl;
  277. $calendar.=qq{\t</td>\n};
  278. }
  279. else {
  280. $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
  281. }
  282. add_depends($params{page}, "$archivebase/$year/$mtag", presence => 1);
  283. $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
  284. }
  285. $calendar.=<<EOF;
  286. </table>
  287. EOF
  288. return $calendar;
  289. }
  290. sub preprocess (@) {
  291. my %params=@_;
  292. $params{pages} = "*" unless defined $params{pages};
  293. $params{type} = "month" unless defined $params{type};
  294. $params{month} = sprintf("%02d", $params{month}) if defined $params{month};
  295. $params{week_start_day} = 0 unless defined $params{week_start_day};
  296. $params{months_per_row} = 3 unless defined $params{months_per_row};
  297. if (! defined $params{year} || ! defined $params{month}) {
  298. # Record that the calendar next changes at midnight.
  299. $pagestate{$params{destpage}}{calendar}{nextchange}=($time
  300. + (60 - $now[0]) # seconds
  301. + (59 - $now[1]) * 60 # minutes
  302. + (23 - $now[2]) * 60 * 60 # hours
  303. );
  304. $params{year} = 1900 + $now[5] unless defined $params{year};
  305. $params{month} = 1 + $now[4] unless defined $params{month};
  306. }
  307. else {
  308. delete $pagestate{$params{destpage}}{calendar};
  309. }
  310. # Calculate month names for next month, and previous months
  311. my $pmonth = $params{month} - 1;
  312. my $nmonth = $params{month} + 1;
  313. my $pyear = $params{year} - 1;
  314. my $nyear = $params{year} + 1;
  315. # Adjust for January and December
  316. if ($params{month} == 1) {
  317. $pmonth = 12;
  318. $pyear--;
  319. }
  320. if ($params{month} == 12) {
  321. $nmonth = 1;
  322. $nyear++;
  323. }
  324. $params{pmonth}=$pmonth;
  325. $params{nmonth}=$nmonth;
  326. $params{pyear} =$pyear;
  327. $params{nyear} =$nyear;
  328. my $calendar="\n";
  329. my $pagespec=$params{pages};
  330. my $page =$params{page};
  331. if (! defined $cache{$pagespec}) {
  332. foreach my $p (pagespec_match_list([keys %pagesources], $pagespec)) {
  333. my $mtime = $IkiWiki::pagectime{$p};
  334. my $src = $pagesources{$p};
  335. my @date = localtime($mtime);
  336. my $mday = $date[3];
  337. my $month = $date[4] + 1;
  338. my $year = $date[5] + 1900;
  339. my $mtag = sprintf("%02d", $month);
  340. # Only one posting per day is being linked to.
  341. $linkcache{"$year/$mtag/$mday"} = "$src";
  342. $cache{$pagespec}{"$year"}++;
  343. $cache{$pagespec}{"$year/$mtag"}++;
  344. $cache{$pagespec}{"$year/$mtag/$mday"}++;
  345. }
  346. }
  347. if ($params{type} =~ /month/i) {
  348. $calendar=format_month(%params);
  349. }
  350. elsif ($params{type} =~ /year/i) {
  351. $calendar=format_year(%params);
  352. }
  353. return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
  354. } #}}
  355. sub needsbuild (@) {
  356. my $needsbuild=shift;
  357. foreach my $page (keys %pagestate) {
  358. if (exists $pagestate{$page}{calendar}{nextchange}) {
  359. if ($pagestate{$page}{calendar}{nextchange} <= $time) {
  360. # force a rebuild so the calendar shows
  361. # the current day
  362. push @$needsbuild, $pagesources{$page};
  363. }
  364. if (exists $pagesources{$page} &&
  365. grep { $_ eq $pagesources{$page} } @$needsbuild) {
  366. # remove state, will be re-added if
  367. # the calendar is still there during the
  368. # rebuild
  369. delete $pagestate{$page}{calendar};
  370. }
  371. }
  372. }
  373. }
  374. 1