diff options
author | Jonas Smedegaard <dr@jones.dk> | 2024-08-25 19:30:39 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2024-08-27 23:39:36 +0200 |
commit | b08609649a576ba82d34384110373ac4804f0231 (patch) | |
tree | 220e7d440934a2f398ae0b15e1992b0dd79362d0 | |
parent | cc64a4bc013986bdfdfa6c45136c262cabcd63e7 (diff) |
use Text::Xslate
-rwxr-xr-x | bin/events2md.pl | 69 | ||||
-rw-r--r-- | templates/list.md | 32 |
2 files changed, 70 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 diff --git a/templates/list.md b/templates/list.md new file mode 100644 index 0000000..02b8c1b --- /dev/null +++ b/templates/list.md @@ -0,0 +1,32 @@ +[%- FOREACH e IN events -%] +### [% e.time_brief %]. +[%- IF e.summary %] [% e.summary %] +[%- END %] +[% e.description %] +[%- IF e.attendees %] +Med [% e.attendees.join(' og ') %]. +[%- END %] +[%- IF e.location %] +**Mødested:** [% e.location %] +[%- END %] +[%- IF e.timespan %] +**Tid:** [% e.timespan %]. +[%- END %] +[%- IF e.price %] +**Pris:** [% e.price %] +[%- END %] +[%- FOREACH uri IN e.attachments %] +[%- IF uri.host == 'byvandring.holdbar.com' %] +[Køb billet på Holdbar]([% uri.as_iri %]) +[%- END %] +[%- IF uri.host == 'billetto.dk' %] +[Køb billet på Billetto]([% uri.as_iri %]) +[%- END %] +[%- IF uri.host == 'byvandring.nu' %] +[Læs mere her]([% uri.as_iri %]) +[%- END %] +[%- END %] + +--- + +[% END %] |