diff options
author | Jonas Smedegaard <dr@jones.dk> | 2024-08-28 17:45:00 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2024-08-28 20:50:06 +0200 |
commit | 944985c6a66e288f9a7f52cd25764bbfef2514ce (patch) | |
tree | 8d06a37583b1a3d0575b63d4c3ce4a110a4db88c | |
parent | ad289d23b089793cd0a43f7c145bf0846a827257 (diff) |
move object classes to library Object::Groupware
-rwxr-xr-x | bin/events2md.pl | 122 | ||||
-rw-r--r-- | lib/Object/Groupware/Calendar.pm | 51 | ||||
-rw-r--r-- | lib/Object/Groupware/Event.pm | 84 |
3 files changed, 141 insertions, 116 deletions
diff --git a/bin/events2md.pl b/bin/events2md.pl index c7cf572..d0dc9b5 100755 --- a/bin/events2md.pl +++ b/bin/events2md.pl @@ -5,9 +5,9 @@ use utf8; use open qw(:std :encoding(UTF-8)); use autodie; use Feature::Compat::Try; -use Feature::Compat::Class 0.07; use FindBin qw($Bin); +use lib "$Bin/../lib"; use POSIX qw(locale_h); use locale; @@ -21,11 +21,12 @@ use Log::Any::Adapter; use URI; use IO::Prompter; use Cal::DAV; -use Data::ICal::DateTime; use DateTime; use Path::Tiny; use Text::Xslate; +use Object::Groupware::Calendar; + if ( IO::Interactive::Tiny::is_interactive() ) { Log::Any::Adapter->set( 'Screen', default_level => 'info' ); } @@ -42,117 +43,6 @@ $CALENDAR_URI ||= shift @ARGV $OUTPUT_FILE = shift @ARGV if @ARGV; -class Calendar { - field $log = Log::Any->get_logger; - - # borrow from Data::ICal::new() signature - field $data : param = undef; - field $filename : param = undef; - - ADJUST { - if ($data) { - if ( $data isa Data::ICal ) { } - else { $data = Data::ICal->new( data => $data ) } - } - elsif ($filename) { $data = Data::ICal->new( filename => $filename ) } - - if ( $log->is_trace ) { - use DDP; - p $data; - } - } - - # mimick Data::ICal::DateTime::events() signature - method events ( $set = undef, $period = undef ) - { - $log->infof( - 'will pick events between %s and %s', - $set->start, $set->end - ) if $set; - - my @events = map { Event->new( entry => $_ ) } sort { - $a->start->compare( $b->start ) - || $a->start->compare( $b->start ) - || $a->summary cmp $b->summary - } $data->events( $set || (), $period || () ); - - return @events; - } -} - -class Event { - field $log = Log::Any->get_logger; - - field $entry : param; - - field $begin : reader = $entry->start; - field $date_begin : reader = $begin->strftime('%A %e. %B'); - field $time_begin : reader = $begin->strftime('%k.%M'); - field $end : reader = $entry->end; - field $date_end : reader; - field $time_end : reader; - field $datespan : reader; - field $timespan : reader; - field $time_brief : reader; - field $summary : reader = $entry->summary; - field $description : reader = $entry->description; - field $location : reader = $entry->_simple_property('location'); - field $price : reader; - field @attendees; - field @attachments; - - ADJUST { - if ( defined $end ) { - $date_end = $end->strftime('%A %e. %B'); - $time_end = $end->strftime('%k.%M'); - } - $datespan - = ( defined $end and $date_end ne $date_begin ) - ? ucfirst("$date_begin - $date_end") - : ucfirst("$date_begin"); - $timespan - = ( defined $end and not $entry->all_day ) - ? ucfirst("$date_begin kl. $time_begin-$time_end") - : undef; - $time_brief - = $entry->all_day - ? $datespan - : ucfirst("$date_begin kl. $time_begin"); - $description =~ s/\n\n[Pp]ris:\s*((?!\n).+)\s*\z//m; - $price = $1; - - if ( $entry->property('attendee') ) { - for ( @{ $entry->property('attendee') } ) { - push @attendees, $_->parameters->{'CN'} - || $_->value =~ s/^mailto://r; - } - } - if ( $entry->property('attach') ) { - for ( @{ $entry->property('attach') } ) { - my $uri; - try { $uri = URI->new( $_->value ) } - catch ($e) { - $log->errorf( 'failed to parse URI %s: %s', $uri, $e ); - next; - } - $uri->authority and $uri->host - or next; - push @attachments, $uri; - } - } - - if ( $log->is_trace ) { - use DDP; - p $entry; - p $begin; - p $end; - } - } - - method attendees { !!@attendees ? [@attendees] : undef } - method attachments { !!@attachments ? [@attachments] : undef } -} - # use system locale to format DateTime objects parsed from iCal data DateTime->DefaultLocale( setlocale(LC_TIME) ); @@ -207,7 +97,7 @@ if ( $base_uri->scheme eq 'http' or $base_uri->scheme eq 'https' ) { ); $session->get($calendar_uri) if $calendar_uri; - $calendar = Calendar->new( data => $session->cal ); + $calendar = Object::Groupware::Calendar->new( data => $session->cal ); } elsif ( $base_uri->scheme eq 'file' ) { defined $base_uri->file @@ -218,12 +108,12 @@ elsif ( $base_uri->scheme eq 'file' ) { $log->debug('parse local calendar data...'); my $path = path( $base_uri->file ); if ( $path->is_file ) { - $calendar = Calendar->new( filename => "$path" ); + $calendar = Object::Groupware::Calendar->new( filename => "$path" ); } else { my $data; $path->visit( sub { $data .= $_->slurp_raw if $_->is_file } ); - $calendar = Calendar->new( data => $data ); + $calendar = Object::Groupware::Calendar->new( data => $data ); } } if ( $log->is_trace ) { diff --git a/lib/Object/Groupware/Calendar.pm b/lib/Object/Groupware/Calendar.pm new file mode 100644 index 0000000..7226782 --- /dev/null +++ b/lib/Object/Groupware/Calendar.pm @@ -0,0 +1,51 @@ +use v5.36; +use Feature::Compat::Class 0.07; + +package Object::Groupware::Calendar 0.01; + +class Object::Groupware::Calendar; + +use utf8; + +use Log::Any qw( ); +use Data::ICal::DateTime; + +use Object::Groupware::Event; + +field $log = Log::Any->get_logger; + +# borrow from Data::ICal::new() signature +field $data : param = undef; +field $filename : param = undef; + +ADJUST { + if ($data) { + if ( $data isa Data::ICal ) { } + else { $data = Data::ICal->new( data => $data ) } + } + elsif ($filename) { $data = Data::ICal->new( filename => $filename ) } + + if ( $log->is_trace ) { + use DDP; + p $data; + } +} + +# mimick Data::ICal::DateTime::events() signature +method events ( $set = undef, $period = undef ) +{ + $log->infof( + 'will pick events between %s and %s', + $set->start, $set->end + ) if $set; + + my @events = map { Object::Groupware::Event->new( entry => $_ ) } sort { + $a->start->compare( $b->start ) + || $a->start->compare( $b->start ) + || $a->summary cmp $b->summary + } $data->events( $set || (), $period || () ); + + return @events; +} + +1; diff --git a/lib/Object/Groupware/Event.pm b/lib/Object/Groupware/Event.pm new file mode 100644 index 0000000..52fa143 --- /dev/null +++ b/lib/Object/Groupware/Event.pm @@ -0,0 +1,84 @@ +use v5.36; +use Feature::Compat::Class 0.07; + +package Object::Groupware::Event 0.01; + +class Object::Groupware::Event; + +use utf8; + +use Log::Any qw( ); +use Feature::Compat::Try; + +field $log = Log::Any->get_logger; + +field $entry : param; + +field $begin : reader = $entry->start; +field $date_begin : reader = $begin->strftime('%A %e. %B'); +field $time_begin : reader = $begin->strftime('%k.%M'); +field $end : reader = $entry->end; +field $date_end : reader; +field $time_end : reader; +field $datespan : reader; +field $timespan : reader; +field $time_brief : reader; +field $summary : reader = $entry->summary; +field $description : reader = $entry->description; +field $location : reader = $entry->_simple_property('location'); +field $price : reader; +field @attendees; +field @attachments; + +ADJUST { + if ( defined $end ) { + $date_end = $end->strftime('%A %e. %B'); + $time_end = $end->strftime('%k.%M'); + } + $datespan + = ( defined $end and $date_end ne $date_begin ) + ? ucfirst("$date_begin - $date_end") + : ucfirst("$date_begin"); + $timespan + = ( defined $end and not $entry->all_day ) + ? ucfirst("$date_begin kl. $time_begin-$time_end") + : undef; + $time_brief + = $entry->all_day + ? $datespan + : ucfirst("$date_begin kl. $time_begin"); + $description =~ s/\n\n[Pp]ris:\s*((?!\n).+)\s*\z//m; + $price = $1; + + if ( $entry->property('attendee') ) { + for ( @{ $entry->property('attendee') } ) { + push @attendees, $_->parameters->{'CN'} + || $_->value =~ s/^mailto://r; + } + } + if ( $entry->property('attach') ) { + for ( @{ $entry->property('attach') } ) { + my $uri; + try { $uri = URI->new( $_->value ) } + catch ($e) { + $log->errorf( 'failed to parse URI %s: %s', $uri, $e ); + next; + } + $uri->authority and $uri->host + or next; + push @attachments, $uri; + } + } + + if ( $log->is_trace ) { + use DDP; + p $entry; + p $begin; + p $end; + } +} + +method attendees { !!@attendees ? [@attendees] : undef } +method attachments { !!@attachments ? [@attachments] : undef } + +1; |