]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blobdiff - IkiWiki/Plugin/calendar.pm
Merge branch 'master' into dependency-types
[git.ikiwiki.info.git] / IkiWiki / Plugin / calendar.pm
index 30e2f26a301d3d24bf71ba0a9d8bece05588ea3c..a1117992a865ed3a5cd5a7a8ded409d3b8ace33e 100644 (file)
@@ -20,32 +20,51 @@ package IkiWiki::Plugin::calendar;
 
 use warnings;
 use strict;
 
 use warnings;
 use strict;
-use IkiWiki 2.00;
+use IkiWiki 3.00;
 use Time::Local;
 use Time::Local;
+use POSIX;
 
 my %cache;
 my %linkcache;
 
 my %cache;
 my %linkcache;
-my @now=localtime();
+my $time=time;
+my @now=localtime($time);
 
 
-sub import { #{{{
+sub import {
+       hook(type => "getsetup", id => "calendar", call => \&getsetup);
+       hook(type => "needsbuild", id => "calendar", call => \&needsbuild);
        hook(type => "preprocess", id => "calendar", call => \&preprocess);
        hook(type => "preprocess", id => "calendar", call => \&preprocess);
-} #}}}
+}
+
+sub getsetup () {
+       return
+               plugin => {
+                       safe => 1,
+                       rebuild => undef,
+               },
+               archivebase => {
+                       type => "string",
+                       example => "archives",
+                       description => "base of the archives hierarchy",
+                       safe => 1,
+                       rebuild => 1,
+               },
+}
 
 
-sub is_leap_year (@) { #{{
+sub is_leap_year (@) {
        my %params=@_;
        return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
        my %params=@_;
        return ($params{year} % 4 == 0 && (($params{year} % 100 != 0) || $params{year} % 400 == 0));
-} #}}}
+}
 
 
-sub month_days { #{{{
+sub month_days {
        my %params=@_;
        my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
        if ($params{month} == 2 && is_leap_year(%params)) {
                $days_in_month++;
        }
        return $days_in_month;
        my %params=@_;
        my $days_in_month = (31,28,31,30,31,30,31,31,30,31,30,31)[$params{month}-1];
        if ($params{month} == 2 && is_leap_year(%params)) {
                $days_in_month++;
        }
        return $days_in_month;
-} #}}}
+}
 
 
-sub format_month (@) { #{{{
+sub format_month (@) {
        my %params=@_;
 
        my $pagespec = $params{pages};
        my %params=@_;
 
        my $pagespec = $params{pages};
@@ -85,19 +104,22 @@ sub format_month (@) { #{{{
                        "$archivebase/$year/".sprintf("%02d", $month),
                        linktext => " $monthname ");
        }
                        "$archivebase/$year/".sprintf("%02d", $month),
                        linktext => " $monthname ");
        }
-       add_depends($params{page}, "$archivebase/$year/".sprintf("%02d", $month));
+       add_depends($params{page}, "$archivebase/$year/".sprintf("%02d", $month),
+               presence => 1);
        if (exists $cache{$pagespec}{"$pyear/$pmonth"}) {
                $purl = htmllink($params{page}, $params{destpage}, 
                        "$archivebase/$pyear/" . sprintf("%02d", $pmonth),
                        linktext => " $pmonthname ");
        }
        if (exists $cache{$pagespec}{"$pyear/$pmonth"}) {
                $purl = htmllink($params{page}, $params{destpage}, 
                        "$archivebase/$pyear/" . sprintf("%02d", $pmonth),
                        linktext => " $pmonthname ");
        }
-       add_depends($params{page}, "$archivebase/$pyear/".sprintf("%02d", $pmonth));
+       add_depends($params{page}, "$archivebase/$pyear/".sprintf("%02d", $pmonth),
+               presence => 1);
        if (exists $cache{$pagespec}{"$nyear/$nmonth"}) {
                $nurl = htmllink($params{page}, $params{destpage}, 
                        "$archivebase/$nyear/" . sprintf("%02d", $nmonth),
                        linktext => " $nmonthname ");
        }
        if (exists $cache{$pagespec}{"$nyear/$nmonth"}) {
                $nurl = htmllink($params{page}, $params{destpage}, 
                        "$archivebase/$nyear/" . sprintf("%02d", $nmonth),
                        linktext => " $nmonthname ");
        }
-       add_depends($params{page}, "$archivebase/$nyear/".sprintf("%02d", $nmonth));
+       add_depends($params{page}, "$archivebase/$nyear/".sprintf("%02d", $nmonth),
+               presence => 1);
 
        # Start producing the month calendar
        $calendar=<<EOF;
 
        # Start producing the month calendar
        $calendar=<<EOF;
@@ -190,10 +212,12 @@ EOF
 
        # Add dependencies to update the calendar whenever pages
        # matching the pagespec are added or removed.
 
        # Add dependencies to update the calendar whenever pages
        # matching the pagespec are added or removed.
-       add_depends($params{page}, $params{pages});
+       add_depends($params{page}, $params{pages}, presence => 1);
        # Explicitly add all currently linked pages as dependencies, so
        # Explicitly add all currently linked pages as dependencies, so
-        # that if they are removed, the calendar will be sure to be updated.
-        add_depends($params{page}, join(" or ", @list));
+       # that if they are removed, the calendar will be sure to be updated.
+       foreach my $p (@list) {
+               add_depends($params{page}, $p, presence => 1);
+       }
 
        return $calendar;
 }
 
        return $calendar;
 }
@@ -225,19 +249,19 @@ sub format_year (@) {
                        "$archivebase/$year",
                        linktext => "$year");
        }
                        "$archivebase/$year",
                        linktext => "$year");
        }
-       add_depends($params{page}, "$archivebase/$year");
+       add_depends($params{page}, "$archivebase/$year", presence => 1);
        if (exists $cache{$pagespec}{"$pyear"}) {
                $purl = htmllink($params{page}, $params{destpage}, 
                        "$archivebase/$pyear",
                        linktext => "\&larr;");
        }
        if (exists $cache{$pagespec}{"$pyear"}) {
                $purl = htmllink($params{page}, $params{destpage}, 
                        "$archivebase/$pyear",
                        linktext => "\&larr;");
        }
-       add_depends($params{page}, "$archivebase/$pyear");
+       add_depends($params{page}, "$archivebase/$pyear", presence => 1);
        if (exists $cache{$pagespec}{"$nyear"}) {
                $nurl = htmllink($params{page}, $params{destpage}, 
                        "$archivebase/$nyear",
                        linktext => "\&rarr;");
        }
        if (exists $cache{$pagespec}{"$nyear"}) {
                $nurl = htmllink($params{page}, $params{destpage}, 
                        "$archivebase/$nyear",
                        linktext => "\&rarr;");
        }
-       add_depends($params{page}, "$archivebase/$nyear");
+       add_depends($params{page}, "$archivebase/$nyear", presence => 1);
 
        # Start producing the year calendar
        $calendar=<<EOF;
 
        # Start producing the year calendar
        $calendar=<<EOF;
@@ -289,7 +313,7 @@ EOF
                else {
                        $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
                }
                else {
                        $calendar.=qq{\t<td class="$tag">$monthabbr</td>\n};
                }
-               add_depends($params{page}, "$archivebase/$year/$mtag");
+               add_depends($params{page}, "$archivebase/$year/$mtag", presence => 1);
 
                $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
        }
 
                $calendar.=qq{\t</tr>\n} if ($month % $params{months_per_row} == 0);
        }
@@ -305,12 +329,25 @@ sub preprocess (@) {
        my %params=@_;
        $params{pages} = "*"            unless defined $params{pages};
        $params{type}  = "month"        unless defined $params{type};
        my %params=@_;
        $params{pages} = "*"            unless defined $params{pages};
        $params{type}  = "month"        unless defined $params{type};
-       $params{year}  = 1900 + $now[5] unless defined $params{year};
        $params{month} = sprintf("%02d", $params{month}) if defined  $params{month};
        $params{month} = sprintf("%02d", $params{month}) if defined  $params{month};
-       $params{month} = 1    + $now[4] unless defined $params{month};
        $params{week_start_day} = 0     unless defined $params{week_start_day};
        $params{months_per_row} = 3     unless defined $params{months_per_row};
 
        $params{week_start_day} = 0     unless defined $params{week_start_day};
        $params{months_per_row} = 3     unless defined $params{months_per_row};
 
+       if (! defined $params{year} || ! defined $params{month}) {
+               # Record that the calendar next changes at midnight.
+               $pagestate{$params{destpage}}{calendar}{nextchange}=($time
+                       + (60 - $now[0])                # seconds
+                       + (59 - $now[1]) * 60           # minutes
+                       + (23 - $now[2]) * 60 * 60      # hours
+               );
+               
+               $params{year}  = 1900 + $now[5] unless defined $params{year};
+               $params{month} = 1    + $now[4] unless defined $params{month};
+       }
+       else {
+               delete $pagestate{$params{destpage}}{calendar};
+       }
+
        # Calculate month names for next month, and previous months
        my $pmonth = $params{month} - 1;
        my $nmonth = $params{month} + 1;
        # Calculate month names for next month, and previous months
        my $pmonth = $params{month} - 1;
        my $nmonth = $params{month} + 1;
@@ -337,8 +374,7 @@ sub preprocess (@) {
        my $page =$params{page};
 
        if (! defined $cache{$pagespec}) {
        my $page =$params{page};
 
        if (! defined $cache{$pagespec}) {
-               foreach my $p (keys %pagesources) {
-                       next unless pagespec_match($p, $pagespec);
+               foreach my $p (pagespec_match_list([keys %pagesources], $pagespec)) {
                        my $mtime = $IkiWiki::pagectime{$p};
                        my $src   = $pagesources{$p};
                        my @date  = localtime($mtime);
                        my $mtime = $IkiWiki::pagectime{$p};
                        my $src   = $pagesources{$p};
                        my @date  = localtime($mtime);
@@ -362,7 +398,27 @@ sub preprocess (@) {
                $calendar=format_year(%params);
        }
 
                $calendar=format_year(%params);
        }
 
-       return "\n<div class=\"calendar\">$calendar</div><!-- calendar -->\n";
+       return "\n<div><div class=\"calendar\">$calendar</div></div>\n";
+} #}}
+
+sub needsbuild (@) {
+       my $needsbuild=shift;
+       foreach my $page (keys %pagestate) {
+               if (exists $pagestate{$page}{calendar}{nextchange}) {
+                       if ($pagestate{$page}{calendar}{nextchange} <= $time) {
+                               # force a rebuild so the calendar shows
+                               # the current day
+                               push @$needsbuild, $pagesources{$page};
+                       }
+                       if (exists $pagesources{$page} && 
+                           grep { $_ eq $pagesources{$page} } @$needsbuild) {
+                               # remove state, will be re-added if
+                               # the calendar is still there during the
+                               # rebuild
+                               delete $pagestate{$page}{calendar};
+                       }
+               }
+       }
 }
 
 1
 }
 
 1