summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/calendar.pm
blob: c99170f923c00a7db9d4ef2b97595b0f69862692 (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. deptype("presence"));
  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. deptype("presence"));
  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. deptype("presence"));
  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}, deptype("presence"));
  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", deptype("presence");
  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", deptype("presence"));
  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", deptype("presence"));
  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. deptype("presence"));
  279. $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
  280. }
  281. $calendar.=<<EOF;
  282. </table>
  283. EOF
  284. return $calendar;
  285. }
  286. sub preprocess (@) {
  287. my %params=@_;
  288. $params{pages} = "*" unless defined $params{pages};
  289. $params{type} = "month" unless defined $params{type};
  290. $params{month} = sprintf("%02d", $params{month}) if defined $params{month};
  291. $params{week_start_day} = 0 unless defined $params{week_start_day};
  292. $params{months_per_row} = 3 unless defined $params{months_per_row};
  293. if (! defined $params{year} || ! defined $params{month}) {
  294. # Record that the calendar next changes at midnight.
  295. $pagestate{$params{destpage}}{calendar}{nextchange}=($time
  296. + (60 - $now[0]) # seconds
  297. + (59 - $now[1]) * 60 # minutes
  298. + (23 - $now[2]) * 60 * 60 # hours
  299. );
  300. $params{year} = 1900 + $now[5] unless defined $params{year};
  301. $params{month} = 1 + $now[4] unless defined $params{month};
  302. }
  303. else {
  304. delete $pagestate{$params{destpage}}{calendar};
  305. }
  306. # Calculate month names for next month, and previous months
  307. my $pmonth = $params{month} - 1;
  308. my $nmonth = $params{month} + 1;
  309. my $pyear = $params{year} - 1;
  310. my $nyear = $params{year} + 1;
  311. # Adjust for January and December
  312. if ($params{month} == 1) {
  313. $pmonth = 12;
  314. $pyear--;
  315. }
  316. if ($params{month} == 12) {
  317. $nmonth = 1;
  318. $nyear++;
  319. }
  320. $params{pmonth}=$pmonth;
  321. $params{nmonth}=$nmonth;
  322. $params{pyear} =$pyear;
  323. $params{nyear} =$nyear;
  324. my $calendar="\n";
  325. my $pagespec=$params{pages};
  326. my $page =$params{page};
  327. if (! defined $cache{$pagespec}) {
  328. foreach my $p (pagespec_match_list([keys %pagesources], $pagespec)) {
  329. my $mtime = $IkiWiki::pagectime{$p};
  330. my $src = $pagesources{$p};
  331. my @date = localtime($mtime);
  332. my $mday = $date[3];
  333. my $month = $date[4] + 1;
  334. my $year = $date[5] + 1900;
  335. my $mtag = sprintf("%02d", $month);
  336. # Only one posting per day is being linked to.
  337. $linkcache{"$year/$mtag/$mday"} = "$src";
  338. $cache{$pagespec}{"$year"}++;
  339. $cache{$pagespec}{"$year/$mtag"}++;
  340. $cache{$pagespec}{"$year/$mtag/$mday"}++;
  341. }
  342. }
  343. if ($params{type} =~ /month/i) {
  344. $calendar=format_month(%params);
  345. }
  346. elsif ($params{type} =~ /year/i) {
  347. $calendar=format_year(%params);
  348. }
  349. return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
  350. } #}}
  351. sub needsbuild (@) {
  352. my $needsbuild=shift;
  353. foreach my $page (keys %pagestate) {
  354. if (exists $pagestate{$page}{calendar}{nextchange}) {
  355. if ($pagestate{$page}{calendar}{nextchange} <= $time) {
  356. # force a rebuild so the calendar shows
  357. # the current day
  358. push @$needsbuild, $pagesources{$page};
  359. }
  360. if (exists $pagesources{$page} &&
  361. grep { $_ eq $pagesources{$page} } @$needsbuild) {
  362. # remove state, will be re-added if
  363. # the calendar is still there during the
  364. # rebuild
  365. delete $pagestate{$page}{calendar};
  366. }
  367. }
  368. }
  369. }
  370. 1