Current section

Files

Jump to
glubs src glubs@srt.erl
Raw

src/glubs@srt.erl

-module(glubs@srt).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([to_string/1, parse/1]).
-export_type([srt/0, cue/0]).
-type srt() :: {srt, list(cue())}.
-type cue() :: {cue, integer(), integer(), integer(), binary()}.
-spec cue_to_string(cue()) -> gleam@string_builder:string_builder().
cue_to_string(Cue) ->
Start_time = glubs@internal@timestamp:to_string(
erlang:element(3, Cue),
<<","/utf8>>
),
End_time = glubs@internal@timestamp:to_string(
erlang:element(4, Cue),
<<","/utf8>>
),
_pipe@2 = [gleam@string_builder:from_string(
gleam@int:to_string(erlang:element(2, Cue))
),
begin
_pipe = Start_time,
_pipe@1 = gleam@string_builder:append(_pipe, <<" --> "/utf8>>),
gleam@string_builder:append_builder(_pipe@1, End_time)
end,
gleam@string_builder:from_string(erlang:element(5, Cue))],
gleam@string_builder:join(_pipe@2, <<"\n"/utf8>>).
-spec to_string(srt()) -> binary().
to_string(Srt) ->
_pipe = erlang:element(2, Srt),
_pipe@1 = gleam@list:map(_pipe, fun cue_to_string/1),
_pipe@2 = gleam@string_builder:join(_pipe@1, <<"\n\n"/utf8>>),
_pipe@3 = gleam@string_builder:append(_pipe@2, <<"\n"/utf8>>),
gleam@string_builder:to_string(_pipe@3).
-spec parse_cue(binary()) -> {ok, cue()} | {error, binary()}.
parse_cue(Input) ->
_assert_subject = gleam@string:split(Input, <<"\n"/utf8>>),
[Id, Ts | Lines] = case _assert_subject of
[_, _ | _] -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"glubs/srt"/utf8>>,
function => <<"parse_cue"/utf8>>,
line => 51})
end,
gleam@result:'try'(
begin
_pipe = Id,
_pipe@1 = gleam@int:parse(_pipe),
gleam@result:replace_error(
_pipe@1,
<<"Cannot parse identifier"/utf8>>
)
end,
fun(Id@1) ->
gleam@result:'try'(
glubs@internal@timestamp:parse_range(Ts, <<","/utf8>>),
fun(_use0) ->
{Start_time, End_time, _} = _use0,
{ok,
{cue,
Id@1,
Start_time,
End_time,
gleam@string:join(Lines, <<"\n"/utf8>>)}}
end
)
end
).
-spec parse(binary()) -> {ok, srt()} | {error, binary()}.
parse(Input) ->
_pipe = Input,
_pipe@1 = gleam@string:replace(_pipe, <<"\r\n"/utf8>>, <<"\n"/utf8>>),
_pipe@2 = gleam@string:trim_right(_pipe@1),
_pipe@3 = gleam@string:split(_pipe@2, <<"\n\n"/utf8>>),
_pipe@4 = gleam@list:try_map(_pipe@3, fun parse_cue/1),
gleam@result:map(_pipe@4, fun(_capture) -> {srt, _capture} end).