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