Browse Source

* Subtitle::BASE : add from_string() / from_file() helpers

master
Alex 'AdUser' Z 8 years ago
parent
commit
82a35e4cb7
  1. 32
      lib/Subtitle/BASE.pm
  2. 4
      t/format-msub.t
  3. 3
      t/format-srt.t

32
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<build>
=head C<parse>
Stubs for subclasses
Stubs for subclasses.
Should return -1 on error, >= 0 on success (parsed events count).
=head2 C<from_string>
my $cnt = $obj->from_string($text);
Parse subtitle from given string.
Return codes are the same as C<parse>
=head2 C<from_file>
my $cnt = $obj->from_file($path);
Parse subtitle from given file.
Return codes are the same as C<parse>
=cut

4
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);

3
t/format-srt.t

@ -21,8 +21,7 @@ my $sample = <<"EOF";
</i>
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} };

Loading…
Cancel
Save