You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
2.3 KiB
126 lines
2.3 KiB
6 years ago
|
package Subtitle::Format;
|
||
10 years ago
|
|
||
|
use strict;
|
||
|
use warnings;
|
||
|
use utf8;
|
||
|
|
||
6 years ago
|
use Subtitle::Utils 'strip_bom';
|
||
|
|
||
10 years ago
|
sub log {
|
||
|
my ($self, $level, $msg) = @_;
|
||
|
|
||
9 years ago
|
if ($level eq 'error') {
|
||
|
push @{ $self->{log} }, "E: $msg";
|
||
|
} elsif ($level eq 'warn') {
|
||
|
push @{ $self->{log} }, "W: $msg";
|
||
|
} elsif ($level eq 'info') {
|
||
|
push @{ $self->{log} }, "I: $msg";
|
||
|
} elsif ($level eq 'debug') {
|
||
|
push @{ $self->{log} }, "D: $msg" if $self->{debug};
|
||
|
} else {
|
||
|
warn "Unknown loglevel $level of $msg\n";
|
||
10 years ago
|
}
|
||
|
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
10 years ago
|
sub parse { return "Unimplemented by subclass"; }
|
||
|
sub build { return "Unimplemented by subclass"; }
|
||
|
|
||
6 years ago
|
sub events {
|
||
|
my ($self, $events) = @_;
|
||
|
if ($events and ref($events) eq 'ARRAY') {
|
||
|
$self->{events} = $events;
|
||
|
}
|
||
|
return wantarray ? @{ $self->{events} } : $self->{events};
|
||
|
}
|
||
|
|
||
9 years ago
|
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
|
||
6 years ago
|
or return $self->log(error => "can't open file: $path -- $!");
|
||
9 years ago
|
my @lines = <$FH>;
|
||
|
close $FH;
|
||
6 years ago
|
return unless @lines; # empty file?
|
||
6 years ago
|
strip_bom($lines[0]);
|
||
9 years ago
|
return $self->parse(\@lines);
|
||
|
}
|
||
|
|
||
6 years ago
|
sub to_string {
|
||
|
my ($self) = @_;
|
||
|
return $self->build;
|
||
|
}
|
||
|
|
||
|
sub to_file {
|
||
|
my ($self, $path) = @_;
|
||
|
open my $FH, '>', $path
|
||
|
or return $self->log(error => "can't open file: $path -- $!");
|
||
|
print $FH $self->build;
|
||
|
close $FH;
|
||
|
|
||
|
return 1;
|
||
|
}
|
||
|
|
||
10 years ago
|
1;
|
||
9 years ago
|
|
||
|
=pod
|
||
|
|
||
|
=head1 NAME
|
||
|
|
||
6 years ago
|
Subtitle::Format - base class of subtitle file
|
||
9 years ago
|
|
||
|
=head1 SYNOPSYS
|
||
|
|
||
6 years ago
|
use base 'Subtitle::Format';
|
||
9 years ago
|
|
||
|
This module should not be used directly.
|
||
|
|
||
|
=head1 METHODS
|
||
|
|
||
|
=head2 C<log>
|
||
|
|
||
|
$obj->log(warn => "something happens");
|
||
|
|
||
|
Add some info to processing log.
|
||
|
Takes two args: message level (debug/info/warn/error) and message text
|
||
|
|
||
|
Collected messages can be accessed like this:
|
||
|
|
||
|
my @log = @{ $obj->{log} };
|
||
|
|
||
6 years ago
|
=head2 C<events>
|
||
|
|
||
|
my @events = $obj->events; # array
|
||
|
my $events = $obj->events; # arrayref
|
||
|
$obj->events([$e1, $e2, $e3]);
|
||
|
|
||
|
Access to subtitle events
|
||
|
|
||
|
=head2 C<build>
|
||
|
=head2 C<parse>
|
||
9 years ago
|
|
||
9 years ago
|
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>
|
||
9 years ago
|
|
||
|
=cut
|