Skip to content

Commit

Permalink
Add X-TIMESTAMP-MAP header support
Browse files Browse the repository at this point in the history
  • Loading branch information
philipgiuliani committed Nov 23, 2023
1 parent 6eeb754 commit 2d4b16f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/glubs/webvtt.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ fn metadata_to_string(metadata: List(#(String, String))) -> StringBuilder {
False ->
metadata
|> list.map(fn(item) {
string_builder.from_strings([item.0, ": ", item.1])
let separator = case item.0 == "X-TIMESTAMP-MAP" {
True -> "="
False -> ": "
}

string_builder.from_strings([item.0, separator, item.1])
})
|> string_builder.join("\n")
}
Expand Down Expand Up @@ -146,9 +151,14 @@ fn parse_metadata(
) -> Result(List(#(String, String)), String) {
metadata
|> list.try_map(fn(meta) {
case string.split_once(meta, ": ") {
Ok(entry) -> Ok(entry)
Error(Nil) -> Error("Invalid metadata item")
case meta {
"X-TIMESTAMP-MAP=" <> header -> Ok(#("X-TIMESTAMP-MAP", header))
_other -> {
case string.split_once(meta, ": ") {
Ok(entry) -> Ok(entry)
Error(Nil) -> Error("Invalid metadata item")
}
}
}
})
}
Expand Down
18 changes: 18 additions & 0 deletions test/glubs/webvtt_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ pub fn parse_header_with_metadata_test() {
|> should.equal(text)
}

pub fn parse_header_with_timestamp_map_test() {
let text = "WEBVTT\nX-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:284722\n"
let structured =
WebVTT(
comment: None,
metadata: [#("X-TIMESTAMP-MAP", "LOCAL:00:00:00.000,MPEGTS:284722")],
items: [],
)

text
|> webvtt.parse()
|> should.equal(Ok(structured))

structured
|> webvtt.to_string()
|> should.equal(text)
}

pub fn parse_cue_test() {
"WEBVTT\n\n1\n00:00.123 --> 00:00.456\nTest"
|> webvtt.parse()
Expand Down

0 comments on commit 2d4b16f

Please sign in to comment.