From 82a35e4cb7d4b69b8d18aeb085e729655f67ebb9 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Sun, 12 Jun 2016 12:27:06 +0000 Subject: [PATCH] * Subtitle::BASE : add from_string() / from_file() helpers --- lib/Subtitle/BASE.pm | 32 +++++++++++++++++++++++++++++++- t/format-msub.t | 4 ++-- t/format-srt.t | 3 +-- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/Subtitle/BASE.pm b/lib/Subtitle/BASE.pm index 542b0b6..7d90342 100644 --- a/lib/Subtitle/BASE.pm +++ b/lib/Subtitle/BASE.pm @@ -25,6 +25,21 @@ sub log { sub parse { return "Unimplemented by subclass"; } sub build { return "Unimplemented by subclass"; } +sub from_string { + my ($self, $text) = @_; + my @lines = split /\r?\n/o, $text; + return $self->parse(\@lines); +} + +sub from_file { + my ($self, $path) = @_; + open my $FH, '<', $path + or return -1; + my @lines = <$FH>; + close $FH; + return $self->parse(\@lines); +} + 1; =pod @@ -55,6 +70,21 @@ Collected messages can be accessed like this: =head C =head C -Stubs for subclasses +Stubs for subclasses. +Should return -1 on error, >= 0 on success (parsed events count). + +=head2 C + + my $cnt = $obj->from_string($text); + +Parse subtitle from given string. +Return codes are the same as C + +=head2 C + + my $cnt = $obj->from_file($path); + +Parse subtitle from given file. +Return codes are the same as C =cut diff --git a/t/format-msub.t b/t/format-msub.t index e90ec9f..631cd9a 100644 --- a/t/format-msub.t +++ b/t/format-msub.t @@ -15,8 +15,8 @@ my $sample = <<"EOF"; {45}{60} Test event|with line wrap EOF -my @lines = split(/[\r\n]+/, $sample); -my $cnt = $msub->parse(\@lines); + +my $cnt = $msub->from_string($sample); is($cnt, 2); my @events = @{ $msub->{events} }; is($events[0]->{timing}->[0], 2.0); diff --git a/t/format-srt.t b/t/format-srt.t index f4c3530..017c03b 100644 --- a/t/format-srt.t +++ b/t/format-srt.t @@ -21,8 +21,7 @@ my $sample = <<"EOF"; EOF -my @lines = split(/\r?\n/, $sample); -my $cnt = $srt->parse(\@lines); +my $cnt = $srt->from_string($sample); is($cnt, 2); my @events = @{ $srt->{events} };