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