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