From 75c4b937c29a0e87d518fc8c912f0decc5383580 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Tue, 17 Mar 2015 14:44:17 +1000 Subject: [PATCH] + Subtitle::SSA->build --- lib/Subtitle/SSA.pm | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/lib/Subtitle/SSA.pm b/lib/Subtitle/SSA.pm index b6fd2a3..bbbe018 100644 --- a/lib/Subtitle/SSA.pm +++ b/lib/Subtitle/SSA.pm @@ -203,4 +203,48 @@ sub parse { return scalar @{ $self->{events} }; } +sub build { + my ($self) = @_; + my $out = "[Script Info]\n"; + + # headers + foreach my $h (sort keys($self->{headers})) { + $out .= sprintf "%s: %s\n", $h, $self->{headers}->{$h}; + } + $out .= "\n"; + + # styles + if ($self->{type} eq 'ass') { + $out .= "[V4+ Styles]\n"; + $out .= sprintf "Format: %s\n", join(', ' => @{ $self->ASS_STYLE_FMT }); + } else { + $out .= "[V4 Styles]\n"; + $out .= sprintf "Format: %s\n", join(', ' => @{ $self->SSA_STYLE_FMT }); + } + my @fields = @{ $self->{style_fmt} }; + foreach my $s (@{ $self->{styles} }) { + $out .= sprintf "Style: %s\n", join(",", @{$s}{@fields}); + } + $out .= "\n"; + + # events + $out .= "[Events]\n"; + if ($self->{type} eq 'ass') { + $out .= sprintf "Format: %s\n", join(', ' => @{ $self->ASS_EVENT_FMT }); + } else { + $out .= sprintf "Format: %s\n", join(', ' => @{ $self->SSA_EVENT_FMT }); + } + @fields = @{ $self->{event_fmt} }; + foreach my $e (@{ $self->{events} }) { + my %event = %{$e}; + $event{start} = $self->build_timing($event{start}); + $event{end} = $self->build_timing($event{end}); + $event{marked} =~ s/^/Marked=/o if $self->{type} eq 'ssa'; + $out .= sprintf "%s: %s\n", ucfirst($e->{type}), join(',' => @event{@fields}); + } + $out .= "\n"; + + return $out; +} + 1;