X-Git-Url: http://git.cyclocoop.org/?p=ikiwiki%2Fevents.git;a=blobdiff_plain;f=events.pm;h=2820c60b70e187471614190af77be7f8ef996d96;hp=e06e2ce6723d49723682eabeaf580d331674d42c;hb=d8aede5b74879b2369cd12964f0039e89231d934;hpb=3624b2511a8ff22fa7020a48437dfaf12605d9f0 diff --git a/events.pm b/events.pm index e06e2ce..2820c60 100644 --- a/events.pm +++ b/events.pm @@ -22,6 +22,7 @@ use warnings; use IkiWiki 3.00; use Time::Local; use DateTime; +use CGI::FormBuilder; #use Data::Dumper; sub import { @@ -154,11 +155,15 @@ sub date_of_page ($%) { my $dir = IkiWiki::dirname($page); my ($year, $month, $day, $hour, $hour_begin, $hour_end) = $dir =~ m{ - .*/ - (\d+)/ - (01|02|03|04|05|06|07|08|09|10|11|12)/ - (01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31)/ - (([0-2][0-9]h[0-5][0-9])(-[0-2][0-9]h[0-5][0-9])?)? + .*? + /(\d+) + (?:/(01|02|03|04|05|06|07|08|09|10|11|12) + (?:/(01|02|03|04|05|06|07|08|09|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24|25|26|27|28|29|30|31) + (?:/(([0-2][0-9]h[0-5][0-9]) + (?:-([0-2][0-9]h[0-5][0-9]))?) + )? + )? + )? $ }x; my $r = @@ -173,13 +178,13 @@ sub date_of_page ($%) { return $r; } sub event_of_page ($%) { - my ($event, %params) = @_; + my ($event, $date, %params) = @_; my $title = exists $pagestate{$event}{meta}{title} ? $pagestate{$event}{meta}{title} : pagetitle(IkiWiki::basename($event)); my $hour - = date_of_page($event)->{hour}; + = $date->{hour}; my $link = htmllink ( $params{page} @@ -197,7 +202,9 @@ sub event_of_page ($%) { (keys %{$IkiWiki::typedlinks{$event}{tag}})); @tags = map { - my $tag = $_; + my $tag_best = bestlink($params{page}, $_); + $tag_best = (length $tag_best > 0 ? $tag_best : bestlink($event, $_)); + my $tag = (length $tag_best > 0 ? $tag_best : $_); my $title = exists $pagestate{$tag}{meta}{title} ? $pagestate{$tag}{meta}{title} @@ -218,10 +225,15 @@ sub event_of_page ($%) { { class => "tag tag-$class" , link => $link } } @tags; + my $base = IkiWiki::dirname($event); + $base =~ s/[^a-zA-Z0-9-]/_/g; return { hour => $hour + , page => $event + , date => $date , link => $link - , tags => \@tags }; + , tags => \@tags + , base => $base }; } sub events_of_pages ($%) { my ($pages, %params) = @_; @@ -231,16 +243,20 @@ sub events_of_pages ($%) { foreach my $page (@$pages) { my $date = date_of_page($page); if (defined $date->{hour}) { - push @hour_events, $page; + push @hour_events, {page=>$page, date=>$date}; } else { - push @day_events, $page; + push @day_events, {page=>$page, date=>$date}; } } return - map {event_of_page($_, %params)} - ( (sort {lc $pagedir->($a) cmp lc $pagedir->($b)} @day_events) - , (sort {lc $pagedir->($a) cmp lc $pagedir->($b)} @hour_events) ); + map {event_of_page($_->{page}, $_->{date}, %params)} + ( (sort {lc IkiWiki::basename($a->{page}) cmp lc IkiWiki::basename($b->{page})} @day_events) + , (sort { + my $r = $a->{date}->{hour} cmp $b->{date}->{hour}; + if ($r) { $r } + else { IkiWiki::basename($a->{page}) cmp IkiWiki::basename($b->{page}) } + } @hour_events) ); } sub event_html ($$%) { my ($date, $format, %params) = @_; @@ -274,7 +290,7 @@ sub event_html ($$%) { , $date->year() ); add_depends($params{page}, $year_page, deptype("presence")); - if ($pagesources{$year_page}) { + if (exists $pagesources{$year_page}) { $year_html = htmllink ( $params{page} @@ -293,7 +309,7 @@ sub event_html ($$%) { , $date->month() ); add_depends($params{page}, $month_page, deptype("presence")); - if ($pagesources{$month_page}) { + if (exists $pagesources{$month_page}) { $month_html = htmllink ( $params{page} @@ -313,7 +329,7 @@ sub event_html ($$%) { , $date->day() ); add_depends($params{page}, $day_page, deptype("presence")); - if ($pagesources{$day_page}) { + if (exists $pagesources{$day_page}) { $day_html = htmllink ( $params{page} @@ -334,7 +350,7 @@ sub event_html ($$%) { ?(month => $month):()) , (($date->year() or $date->month() or $date->day()) ?(year => $date->year()):()) - , do => 'events' + , do => 'newevent' , page => $params{destpage} ) . qq{' rel='nofollow'>+}; @@ -367,7 +383,7 @@ sub preprocess_day (@) { my @tags = map {"".$_->{link}.""} @{$_->{tags}}; - "
  • " + "
  • " . "" . (defined $_->{hour} ? "$_->{hour}" : "") . "$_->{link}" @@ -404,15 +420,9 @@ sub preprocess_month (@) { # NOTE: add presence dependencies to update calendar when pages are added/removed ); - # NOTE: sort the pages by days of the month - my %days = map {($_=>[])} (1 .. $last_day); - foreach my $page (@pages) { - my $page_ctime = DateTime->from_epoch - ( epoch => $IkiWiki::pagectime{$page} - , time_zone => 'local' - , locale => $config{locale} - ); - push @{$days{$page_ctime->day()}}, $page; + my %events_by_day = map {($_=>[])} (1 .. $last_day); + foreach my $event (events_of_pages(\@pages, %params)) { + push @{$events_by_day{$event->{date}->{day}}}, $event; } my $t=''; @@ -436,7 +446,7 @@ sub preprocess_month (@) { my @tags = map {"".$_->{link}."
  • "} @{$_->{tags}}; - "" + "" . "" . (defined $_->{hour} ? "$_->{hour}" : "") . "$_->{link}" @@ -444,7 +454,7 @@ sub preprocess_month (@) { . "
      ".join("", @tags)."
    " . "\n" } - events_of_pages($days{$day->day()}, %params); + @{$events_by_day{sprintf('%02d',$day->day())}}; $t .= "" . "" @@ -492,6 +502,10 @@ sub preprocess_month (@) { sub preprocess (@) { my %params = @_; $params{focus} = $now->clone; + $params{focus}->set_hour(0); + $params{focus}->set_minute(0); + $params{focus}->set_second(0); + $params{focus}->set_nanosecond(0); $params{pages} = "*" unless defined $params{pages}; $params{type} = "month" unless defined $params{type}; @@ -504,47 +518,45 @@ sub preprocess (@) { ? $config{events_base} : gettext('Agenda'); } - if (defined $params{day}) { - if ($params{day} =~ m/^([+-])(\d+)$/) { - my ($sign, $days) = ($1, $2); - my $duration = DateTime::Duration->new(days => $days, end_of_month => 'limit'); - $params{focus} - = $sign eq '+' - ? $params{focus}->add_duration($duration) - : $params{focus}->subtract_duration($duration); - } - else { - $params{focus}->set(day => $params{day}); - } + + my %focus_set; + if (defined $params{day} and $params{day} =~ m/^(\d+)$/) { + my ($day) = ($1); + $focus_set{day} = $day; } - else { - #$params{focus}->set(day => 1); + if (defined $params{month} and $params{month} =~ m/^(\d+)$/) { + my ($month) = ($1); + $focus_set{month} = $month; } - if (defined $params{month}) { - if ($params{month} =~ m/^([+-])(\d+)$/) { - my ($sign, $months) = ($1, $2); - my $duration = DateTime::Duration->new(months => $months, end_of_month => 'limit'); - $params{focus} - = $sign eq '+' - ? $params{focus}->add_duration($duration) - : $params{focus}->subtract_duration($duration); - } - else { - $params{focus}->set(month => $params{month}); - } + if (defined $params{year} and $params{year} =~ m/^(\d+)$/) { + my ($year) = ($1); + $focus_set{year} = $year; } - if (defined $params{year}) { - if ($params{year} =~ m/^([+-])(\d+)$/) { - my ($sign, $years) = ($1, $2); - my $duration = DateTime::Duration->new(years => $years, end_of_month => 'limit'); - $params{focus} - = $sign eq '+' - ? $params{focus}->add_duration($duration) - : $params{focus}->subtract_duration($duration); - } - else { - $params{focus}->set(year => $params{year}); - } + $params{focus}->set(%focus_set); + + if (defined $params{day} and $params{day} =~ m/^([+-])(\d+)$/) { + my ($sign, $days) = ($1, $2); + my $duration = DateTime::Duration->new(days => $days, end_of_month => 'limit'); + $params{focus} + = $sign eq '+' + ? $params{focus}->add_duration($duration) + : $params{focus}->subtract_duration($duration); + } + if (defined $params{month} and $params{month} =~ m/^([+-])(\d+)$/) { + my ($sign, $months) = ($1, $2); + my $duration = DateTime::Duration->new(months => $months, end_of_month => 'limit'); + $params{focus} + = $sign eq '+' + ? $params{focus}->add_duration($duration) + : $params{focus}->subtract_duration($duration); + } + if (defined $params{year} and $params{year} =~ m/^([+-])(\d+)$/) { + my ($sign, $years) = ($1, $2); + my $duration = DateTime::Duration->new(years => $years, end_of_month => 'limit'); + $params{focus} + = $sign eq '+' + ? $params{focus}->add_duration($duration) + : $params{focus}->subtract_duration($duration); } #debug("events: focus=".$params{focus}->strftime('%Y-%m-%d_%H-%M-%S')); @@ -568,7 +580,7 @@ sub preprocess (@) { # new sub tmpl ($$) { my ($base, $model) = @_; - my $page = IkiWiki::dirname($base).'/'.'templates/'.$model; + my $page = $base.'/'.'templates/'.$model; my $file = defined srcfile($page, 1) ? '/'.$page : $model; return template($file); }