diff options
Diffstat (limited to 'bin/events2md.pl')
-rwxr-xr-x | bin/events2md.pl | 69 |
1 files changed, 38 insertions, 31 deletions
diff --git a/bin/events2md.pl b/bin/events2md.pl index cd662bb..6d73768 100755 --- a/bin/events2md.pl +++ b/bin/events2md.pl @@ -7,6 +7,8 @@ use strictures; use autodie; use Feature::Compat::Try; +use FindBin qw($Bin); + use POSIX qw(locale_h); use locale; use Encode qw(decode_utf8); # TODO: modernize CalDAV access instead @@ -23,15 +25,17 @@ use Cal::DAV; use Data::ICal::DateTime; use DateTime; use Path::Tiny; +use Text::Xslate; if ( IO::Interactive::Tiny::is_interactive() ) { Log::Any::Adapter->set( 'Screen', default_level => 'info' ); } # set defaults and parse command-line options -my ( $BASE_URI, $CALENDAR_URI, $OUTPUT_FILE ); +my ( $BASE_URI, $CALENDAR_URI, $SKELDIR, $OUTPUT_FILE ); $BASE_URI = $ENV{CAL_DAV_URL_BASE}; $CALENDAR_URI = $ENV{CAL_DAV_URL_CALENDAR}; +$SKELDIR = $ENV{SKELDIR} || "$Bin/../templates"; $BASE_URI ||= shift @ARGV if @ARGV; $CALENDAR_URI ||= shift @ARGV @@ -160,12 +164,31 @@ if ($OUTPUT_FILE) { $output_path->remove; } +my %vars; for (@events) { next unless $_->summary; - print_event( $_, $_->start, $_->end, $output_path, ); + push @{ $vars{events} }, resolve_event( $_, $_->start, $_->end ); +} + +my %tmpl; +$tmpl{list} = path($SKELDIR)->child('list.md')->slurp_utf8; + +my $template = Text::Xslate->new( + path => \%tmpl, + syntax => 'TTerse', + type => 'text', +); + +my $content = $template->render( 'list', \%vars ); + +if ($output_path) { + $output_path->append_utf8($content); +} +else { + print $content; } -sub print_event +sub resolve_event { my ( $entry, $start, $end, $path ) = @_; @@ -196,7 +219,7 @@ sub print_event = $time_end ? ucfirst("$date_begin kl. $time_begin-$time_end") : undef; - my %attachments; + my @attachments; if ( $entry->property('attach') ) { for ( @{ $entry->property('attach') } ) { @@ -208,36 +231,20 @@ sub print_event } $uri->authority and $uri->host or next; - push @{ $attachments{ $uri->host } }, $uri; + push @attachments, $uri; } } - $_ = "### $time_brief."; - $_ .= " $summary" - if $summary; - $_ .= "\n$description"; - $_ .= " \nMed " . join( ' og ', @attendees ) . '.' - if @attendees; - $_ .= " \n**Mødested:** $location" - if $location; - $_ .= " \n**Tid:** $timespan." - if $timespan; - $_ .= " \n**Pris:** $price" - if $price; - $_ .= " \n[Køb billet på Holdbar]($attachments{'byvandring.holdbar.com'}[0])" - if $attachments{'byvandring.holdbar.com'}; - $_ .= " \n[Køb billet på Billetto]($attachments{'billetto.dk'}[0])" - if $attachments{'billetto.dk'}; - $_ .= " \n[Læs mere her]($attachments{'byvandring.nu'}[0])" - if $attachments{'byvandring.nu'}; - $_ .= "\n\n---\n\n"; - - if ($path) { - $path->append_utf8($_); - } - else { - print $_; - } + return { + time_brief => $time_brief, + summary => $summary, + description => $description, + attendees => [@attendees], + location => $location, + timespan => $timespan, + price => $price, + attachments => [@attachments], + }; } sub get_property_string |