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