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