+}
+
+sub autocreate {
+ my ($page, $pagefile, $year, $month) = @_;
+ my $message=sprintf(gettext("creating calendar page %s"), $page);
+ debug($message);
+
+ my $template;
+ if (defined $month) {
+ $template=template("calendarmonth.tmpl");
+ } else {
+ $template=template("calendaryear.tmpl");
+ }
+ $template->param(year => $year);
+ $template->param(month => $month) if defined $month;
+ $template->param(pagespec => $config{archive_pagespec});
+
+ my $dir = $config{srcdir};
+ if (! $config{calendar_autocreate_commit}) {
+ $dir = $IkiWiki::Plugin::transient::transientdir;
+ }
+
+ writefile($pagefile, $dir, $template->output);
+ if ($config{rcs} && $config{calendar_autocreate_commit}) {
+ IkiWiki::disable_commit_hook();
+ IkiWiki::rcs_add($pagefile);
+ IkiWiki::rcs_commit_staged(message => $message);
+ IkiWiki::enable_commit_hook();
+ }
+}
+
+sub calendarlink($;$) {
+ my ($year, $month) = @_;
+ if (defined $month) {
+ return $config{archivebase} . "/" . $year . "/" . $month;
+ } else {
+ return $config{archivebase} . "/" . $year;
+ }
+}
+
+sub gencalendaryear {
+ my $year = shift;
+ my %params = @_;
+
+ return unless $config{calendar_autocreate};
+
+ # Building year page
+ my $page = calendarlink($year);
+ my $pagefile = newpagefile($page, $config{default_pageext});
+ add_autofile(
+ $pagefile, "calendar",
+ sub {return autocreate($page, $pagefile, $year);}
+ );
+
+ # Building month pages
+ foreach my $month (qw{01 02 03 04 05 06 07 08 09 10 11 12}) {
+ my $page = calendarlink($year, $month);
+ my $pagefile = newpagefile($page, $config{default_pageext});
+ add_autofile(
+ $pagefile, "calendar",
+ sub {return autocreate($page, $pagefile, $year, $month);}
+ );
+ }