From 05aaf18b325e1421309dd5d9bdb3c2a42d39a51a Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Sun, 12 Jun 2016 11:54:43 +0000 Subject: [PATCH] * Subtitle::SRT : update --- lib/Subtitle/SRT.pm | 47 ++++++++++++++++++++++++++------------------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/lib/Subtitle/SRT.pm b/lib/Subtitle/SRT.pm index 9ba0321..bd36027 100644 --- a/lib/Subtitle/SRT.pm +++ b/lib/Subtitle/SRT.pm @@ -2,9 +2,10 @@ package Subtitle::SRT; use strict; use warnings; -use feature qw(switch); use utf8; +use Subtitle::Utils qw(:string :timing); + use base 'Subtitle::BASE'; sub new { @@ -15,7 +16,7 @@ sub new { %args, events => [], log => [], - timing_fmt => "%02d:%02d:%02d,%s", + timing_fmt => "%02d:%02d:%02d,%03d", }; return bless($self, $class); @@ -28,13 +29,14 @@ sub parse { my $linenum = 0; my $event; - foreach my $line (@$lines) { + foreach my $line (@{ $lines }) { $linenum++; - $line = $self->chomp($line); + chomp_all($line); # expected: event id if ($line and not $event) { $event = $self->new_event; - $event->{id} = $self->trim($line); + trim($line); + $event->{id} = $line; $self->log(debug => "New event with id: $event->{id}"); next; } @@ -47,16 +49,18 @@ sub parse { $self->log(warn => "Expected timing but found `$line` at $linenum, skipped"); next; } - unless (defined ($timing->[0] = $self->parse_timing($start))) { + $timing->[0] = parse_timing($start); + unless ($timing->[0] >= 0) { $self->log(warn => "Can't parse timing at line $linenum: $start"); next; } - unless (defined ($timing->[1] = $self->parse_timing($end))) { + $timing->[1] = parse_timing($end); + unless ($timing->[1] >= 0) { $self->log(warn => "Can't parse timing at line $linenum: $end"); next; } $event->{timing} = $timing; - $rest = $self->trim($rest); + trim($rest); next unless $rest; # extension: timing # example: X1:050 X2:669 Y1:234 Y2:436 @@ -81,8 +85,9 @@ sub parse { $rest = $1; foreach my $token (split(/\s*,\s*/, $rest)) { next unless $token =~ m/^(\S+):\s*(.+)/o; + trim($2); $event->{style} //= {}; - $event->{style}->{lc($1)} = $self->trim($2); + $event->{style}->{lc($1)} = $2; } next; } @@ -98,15 +103,14 @@ sub parse { } # expected: event text if ($line and $event and $event->{timing}) { + trim($line); $self->log(debug => "Text line at $linenum -> append"); - if ($event->{text}) { - $event->{text} .= $self->{eol} . $self->trim($line); - } else { - $event->{text} = $self->trim($line); - } + $event->{text} //= ''; + $event->{text} .= $self->{eol} if $event->{text}; + $event->{text} .= $line; next; } - } + } # foreach @lines # finalize last event if ($event and $event->{timing} and $event->{text}) { push @{ $self->{events} }, $event; @@ -117,13 +121,16 @@ sub parse { sub build { my ($self) = @_; - my $out = ""; + my ($hrs, $min, $sec, $msec, $out); + $out = ''; foreach my $e (@{ $self->{events} }) { - $out .= "$e->{id}\n"; - $out .= sprintf "%s --> %s\n", - $self->build_timing($e->{timing}->[0], 3, ','), - $self->build_timing($e->{timing}->[1], 3, ','); + $out .= $e->{id} . "\n"; + my $start = sprintf $self->{timing_fmt}, + make_timing($e->{timing}->[0]); + my $end = sprintf $self->{timing_fmt}, + make_timing($e->{timing}->[1]); + $out .= sprintf "%s --> %s\n", $start, $end; $out .= "$e->{text}\n"; $out .= "\n"; }