Skip to content

Commit

Permalink
substudy: Give a better error for overlapping subs
Browse files Browse the repository at this point in the history
Fixes #37.
  • Loading branch information
emk committed May 4, 2024
1 parent f10160b commit 3d21735
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion substudy/src/clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

use std::borrow::Cow;

use anyhow::Context;
use regex::Regex;

use crate::{
srt::{Subtitle, SubtitleFile},
time::seconds_to_hhmmss,
Result,
};

Expand All @@ -28,6 +30,7 @@ fn clean_line(line: &str) -> String {
// Used to compress and normalize consecutive whitespace.
let whitespace = Regex::new(r"\s+").unwrap();

// Note that `replace_all` may take O(N^2) time in pathological cases.
whitespace
.replace_all(&clutter.replace_all(line, ""), " ")
.trim()
Expand Down Expand Up @@ -69,7 +72,19 @@ pub fn clean_subtitle_file(file: &SubtitleFile) -> Result<SubtitleFile> {
if subs.len() >= 2 {
for i in 0..subs.len() - 1 {
let limit = subs[i + 1].period.begin();
subs[i].period.end_before(limit)?;
// Give a nice error, because users tend to report this a lot.
//
// We might want to create some strategy for fixing this
// automatically at some point.
subs[i].period.end_before(limit).with_context(|| {
format!(
"Unable to fix overlapping subtitles:\n- {}: {:?}\n- {}: {:?}",
seconds_to_hhmmss(subs[i].period.begin()),
subs[i].lines,
seconds_to_hhmmss(subs[i + 1].period.begin()),
subs[i + 1].lines,
)
})?;
}
}

Expand Down

0 comments on commit 3d21735

Please sign in to comment.