-
Notifications
You must be signed in to change notification settings - Fork 0
/
srt.gleam
67 lines (57 loc) · 1.65 KB
/
srt.gleam
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import gleam/string
import gleam/list
import gleam/result
import gleam/int
import gleam/string_builder.{type StringBuilder}
import glubs/internal/timestamp
pub type Srt {
Srt(cues: List(Cue))
}
/// Cue represents a single cue in a srt file.
pub type Cue {
Cue(id: Int, start_time: Int, end_time: Int, payload: String)
}
/// Parses a Srt string and returns a Result containing the parsed Srt structure or a parsing error.
pub fn parse(input: String) -> Result(Srt, String) {
input
|> string.replace("\r\n", "\n")
|> string.trim_right()
|> string.split("\n\n")
|> list.try_map(parse_cue)
|> result.map(Srt(cues: _))
}
/// Converts a Srt type to a string.
pub fn to_string(srt: Srt) -> String {
srt.cues
|> list.map(cue_to_string)
|> string_builder.join("\n\n")
|> string_builder.append("\n")
|> string_builder.to_string()
}
fn cue_to_string(cue: Cue) -> StringBuilder {
let start_time = timestamp.to_string(cue.start_time, ",")
let end_time = timestamp.to_string(cue.end_time, ",")
[
string_builder.from_string(int.to_string(cue.id)),
start_time
|> string_builder.append(" --> ")
|> string_builder.append_builder(end_time),
string_builder.from_string(cue.payload),
]
|> string_builder.join("\n")
}
fn parse_cue(input: String) -> Result(Cue, String) {
let assert [id, ts, ..lines] = string.split(input, "\n")
use id <- result.try(
id
|> int.parse()
|> result.replace_error("Cannot parse identifier"),
)
use #(start_time, end_time, _) <- result.try(timestamp.parse_range(ts, ","))
Ok(Cue(
id: id,
start_time: start_time,
end_time: end_time,
payload: string.join(lines, "\n"),
))
}