subtitler 0.0.5

Subtitler is a library for parsing and generating subtitles
Documentation
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use crate::model::{Subtitle, TextPart};
use crate::types::AnyResult;
use crate::utils::{format_timestamp, parse_timestamp};
use anyhow::anyhow;
use regex::Regex;
#[cfg(feature = "http")]
use reqwest;
use std::io::Cursor;
use std::sync::LazyLock;
use tokio::fs::{self, File};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};

static RE_SRT_TAG: LazyLock<Regex> =
  LazyLock::new(|| Regex::new(r"</?(?:b|i|u|font)(?:\s[^>]*)?>").unwrap());

enum Phase {
  Index,
  Timestamp,
  Text,
}

fn extract_text_parts(text: &str) -> (String, Vec<TextPart>) {
  let mut parts = Vec::new();
  let mut plain = String::new();
  let mut bold = false;
  let mut italic = false;
  let mut underline = false;
  let mut color: Option<String> = None;
  let mut last_end = 0;

  for caps in RE_SRT_TAG.find_iter(text) {
    let tag = caps.as_str();
    let start = caps.start();
    let end = caps.end();

    if start > last_end {
      let segment = &text[last_end..start];
      if !segment.is_empty() {
        plain.push_str(segment);
        if bold || italic || underline || color.is_some() {
          parts.push(TextPart {
            text: segment.to_string(),
            bold,
            italic,
            underline,
            color: color.clone(),
            voice: None,
          });
        }
      }
    }

    if tag.starts_with("</") {
      match tag {
        "</b>" => bold = false,
        "</i>" => italic = false,
        "</u>" => underline = false,
        "</font>" => color = None,
        _ => {}
      }
    } else {
      if tag.starts_with("<b") {
        bold = true;
      } else if tag.starts_with("<i") {
        italic = true;
      } else if tag.starts_with("<u") {
        underline = true;
      } else if tag.starts_with("<font")
        && let Some(c) = tag.split("color=").nth(1)
      {
        let color_val = c.trim_matches(|c: char| c == '"' || c == '\'' || c == '>' || c == '/');
        color = Some(color_val.to_string());
      }
    }

    last_end = end;
  }

  if last_end < text.len() {
    let segment = &text[last_end..];
    plain.push_str(segment);
    if bold || italic || underline || color.is_some() {
      parts.push(TextPart {
        text: segment.to_string(),
        bold,
        italic,
        underline,
        color: color.clone(),
        voice: None,
      });
    }
  }

  // If no tags were found, just return the plain text
  if parts.is_empty() {
    plain = text.to_string();
  }

  (plain, parts)
}

async fn parse<R>(reader: R) -> AnyResult<Vec<Subtitle>>
where
  R: AsyncBufReadExt + Unpin,
{
  let mut lines = reader.lines();
  let mut subtitles = Vec::new();
  let mut current_subtitle: Option<Subtitle> = None;
  let mut phase = Phase::Index;
  let mut row: usize = 0;
  let mut is_first_content_line = true;

  while let Some(line) = lines.next_line().await? {
    row += 1;
    let mut trimmed = line.trim().to_string();

    // Strip BOM from first content line (matching JS strip-bom behavior)
    if is_first_content_line && !trimmed.is_empty() {
      is_first_content_line = false;
      if trimmed.starts_with('\u{FEFF}') {
        trimmed = trimmed.trim_start_matches('\u{FEFF}').to_string();
      }
    }

    if trimmed.is_empty() {
      if let Some(sub) = current_subtitle.take() {
        subtitles.push(sub);
      }
      phase = Phase::Index;
      continue;
    }

    match phase {
      Phase::Index => {
        if let Ok(index) = trimmed.parse::<usize>() {
          current_subtitle = Some(Subtitle {
            index: Some(index),
            start: 0,
            end: 0,
            text: String::new(),
            settings: None,
            text_parts: Vec::new(),
            style: None,
            actor: None,
            layer: None,
            margin_l: None,
            margin_r: None,
            margin_v: None,
            effect: None,
            is_comment: false,
          });
          phase = Phase::Timestamp;
        } else if trimmed.contains("-->") {
          if let Some((start_str, end_str)) = trimmed.split_once(" --> ") {
            let subtitle =
              Subtitle::new(parse_timestamp(start_str)?, parse_timestamp(end_str)?, "");
            phase = Phase::Text;
            current_subtitle = Some(subtitle);
          } else {
            return Err(anyhow!(
              "expected index or timestamp at row {row}, but received: \"{}\"",
              trimmed
            ));
          }
        }
      }
      Phase::Timestamp => {
        if let Some(sub) = &mut current_subtitle {
          if let Some((start_str, end_str)) = trimmed.split_once(" --> ") {
            sub.start = parse_timestamp(start_str)?;
            sub.end = parse_timestamp(end_str)?;
            phase = Phase::Text;
          } else {
            return Err(anyhow!(
              "expected timestamp at row {row}, but received: \"{}\"",
              trimmed
            ));
          }
        }
      }
      Phase::Text => {
        if let Some(sub) = &mut current_subtitle {
          if !sub.text.is_empty() {
            sub.text.push('\n');
          }
          sub.text.push_str(&trimmed);
        }
      }
    }
  }

  if let Some(mut sub) = current_subtitle {
    let (plain, parts) = extract_text_parts(&sub.text);
    sub.text = plain;
    sub.text_parts = parts;
    subtitles.push(sub);
  }

  // Post-process: extract tags from all subtitles
  for sub in &mut subtitles {
    let (plain, parts) = extract_text_parts(&sub.text);
    sub.text = plain;
    sub.text_parts = parts;
  }

  Ok(subtitles)
}

pub async fn parse_file(path: impl AsRef<std::path::Path>) -> AnyResult<Vec<Subtitle>> {
  let file = File::open(path).await?;
  let reader = BufReader::new(file);
  parse(reader).await
}

pub async fn parse_bytes(data: &[u8]) -> AnyResult<Vec<Subtitle>> {
  let text = String::from_utf8(data.to_vec()).map_err(|e| anyhow!("Invalid UTF-8: {}", e))?;
  let cursor = Cursor::new(text);
  let reader = BufReader::new(cursor);
  parse(reader).await
}

#[cfg(feature = "http")]
pub async fn parse_url(url: &str) -> AnyResult<Vec<Subtitle>> {
  let response = reqwest::get(url).await?;
  let content = response.text().await?;
  let cursor = Cursor::new(content);
  let reader = BufReader::new(cursor);
  parse(reader).await
}

pub async fn parse_content(content: &str) -> AnyResult<Vec<Subtitle>> {
  let cursor = Cursor::new(content);
  let reader = BufReader::new(cursor);
  parse(reader).await
}

pub fn detect_format(data: &[u8]) -> Option<crate::model::SubtitleFormat> {
  if let Ok(text) = String::from_utf8(data.to_vec()) {
    let trimmed = text.trim();
    if !trimmed.is_empty() {
      if trimmed.starts_with("WEBVTT") {
        return Some(crate::model::SubtitleFormat::Vtt);
      }
      if Regex::new(r"^\d+\s*\n\d{2}:\d{2}:\d{2}[,.]\d{3}\s*-->")
        .unwrap()
        .is_match(trimmed)
      {
        return Some(crate::model::SubtitleFormat::Srt);
      }
      if trimmed.contains("-->") {
        return Some(crate::model::SubtitleFormat::Srt);
      }
    }
  }
  None
}

pub fn to_string(subtitles: &[Subtitle]) -> String {
  let mut content = String::new();
  for (i, subtitle) in subtitles.iter().enumerate() {
    if let Some(index) = subtitle.index {
      content.push_str(&index.to_string());
      content.push('\n');
    }
    let timestamp = format!(
      "{} --> {}",
      format_timestamp(subtitle.start, "srt"),
      format_timestamp(subtitle.end, "srt")
    );
    content.push_str(&timestamp);
    content.push('\n');
    content.push_str(&subtitle.text);
    if i != subtitles.len() - 1 {
      content.push('\n');
      content.push('\n');
    }
  }
  if !subtitles.is_empty() {
    content.push('\n');
  }
  content
}

pub async fn generate(
  subtitles: &[Subtitle],
  file_path: impl AsRef<std::path::Path>,
) -> AnyResult<String> {
  let path = file_path.as_ref();
  let mut dest = fs::OpenOptions::new()
    .create(true)
    .write(true)
    .truncate(true)
    .open(path)
    .await?;
  let content = to_string(subtitles);
  dest.write_all(content.as_bytes()).await?;
  dest.flush().await?;

  Ok(path.to_string_lossy().into_owned())
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::model::Subtitle;

  fn make_subtitle(index: usize, start: u64, end: u64, text: &str) -> Subtitle {
    Subtitle {
      index: Some(index),
      start,
      end,
      text: text.to_string(),
      settings: None,
      text_parts: Vec::new(),
      style: None,
      actor: None,
      layer: None,
      margin_l: None,
      margin_r: None,
      margin_v: None,
      effect: None,
      is_comment: false,
    }
  }

  #[tokio::test]
  async fn test_parse_basic_srt() {
    let content =
      "1\n00:00:01,000 --> 00:00:03,500\nHello!\n\n2\n00:00:04,000 --> 00:00:06,500\nWorld!\n\n";
    let result = parse_content(content).await.unwrap();
    assert_eq!(result.len(), 2);
    assert_eq!(result[0], make_subtitle(1, 1000, 3500, "Hello!"));
    assert_eq!(result[1], make_subtitle(2, 4000, 6500, "World!"));
  }

  #[tokio::test]
  async fn test_parse_multiline_text() {
    let content = "1\n00:00:01,000 --> 00:00:03,500\nLine one\nLine two\n\n";
    let result = parse_content(content).await.unwrap();
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].text, "Line one\nLine two");
  }

  #[tokio::test]
  async fn test_parse_start_at_zero() {
    let content = "1\n00:00:00,000 --> 00:00:03,500\nFrom zero\n\n";
    let result = parse_content(content).await.unwrap();
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].start, 0);
    assert_eq!(result[0].end, 3500);
    assert_eq!(result[0].text, "From zero");
  }

  #[tokio::test]
  async fn test_parse_numeric_text_not_mistaken_for_index() {
    let content = "1\n00:00:01,000 --> 00:00:03,500\n42\n\n";
    let result = parse_content(content).await.unwrap();
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].text, "42");
  }

  #[tokio::test]
  async fn test_parse_no_trailing_blank_line() {
    let content = "1\n00:00:01,000 --> 00:00:03,500\nHello!";
    let result = parse_content(content).await.unwrap();
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].text, "Hello!");
  }

  #[tokio::test]
  async fn test_round_trip() {
    let original =
      "1\n00:00:01,000 --> 00:00:03,500\nHello\n\n2\n00:00:04,000 --> 00:00:06,500\nWorld\n\n";
    let subtitles = parse_content(original).await.unwrap();
    let path = "test_round_trip_srt.srt";
    generate(&subtitles, path).await.unwrap();
    let parsed_back = parse_file(path).await.unwrap();
    let _ = std::fs::remove_file(path);
    assert_eq!(subtitles, parsed_back);
  }

  #[tokio::test]
  async fn test_parse_missing_index() {
    let content = "00:00:01,000 --> 00:00:03,500\nNo index\n\n";
    let result = parse_content(content).await.unwrap();
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].index, None);
    assert_eq!(result[0].start, 1000);
    assert_eq!(result[0].text, "No index");
  }

  #[tokio::test]
  async fn test_parse_missing_timestamp_error() {
    let content = "1\nnot a timestamp\n";
    let result = parse_content(content).await;
    assert!(result.is_err());
  }

  #[tokio::test]
  async fn test_parse_bold_tag() {
    let content = "1\n00:00:01,000 --> 00:00:03,500\n<b>Bold text</b>\n\n";
    let result = parse_content(content).await.unwrap();
    assert_eq!(result[0].text, "Bold text");
    assert_eq!(result[0].text_parts.len(), 1);
    assert!(result[0].text_parts[0].bold);
    assert_eq!(result[0].text_parts[0].text, "Bold text");
  }

  #[tokio::test]
  async fn test_parse_italic_tag() {
    let content = "1\n00:00:01,000 --> 00:00:03,500\n<i>Italic</i> plain\n\n";
    let result = parse_content(content).await.unwrap();
    assert_eq!(result[0].text, "Italic plain");
    assert_eq!(result[0].text_parts.len(), 1);
    assert!(result[0].text_parts[0].italic);
    assert_eq!(result[0].text_parts[0].text, "Italic");
  }

  #[tokio::test]
  async fn test_parse_underline_tag() {
    let content = "1\n00:00:01,000 --> 00:00:03,500\n<u>underline</u>\n\n";
    let result = parse_content(content).await.unwrap();
    assert!(result[0].text_parts[0].underline);
  }

  #[tokio::test]
  async fn test_parse_font_color_tag() {
    let content = "1\n00:00:01,000 --> 00:00:03,500\n<font color=\"#ff0000\">red</font>\n\n";
    let result = parse_content(content).await.unwrap();
    assert_eq!(result[0].text_parts[0].color, Some("#ff0000".to_string()));
  }

  #[tokio::test]
  async fn test_parse_bytes() {
    let data = b"1\n00:00:01,000 --> 00:00:03,500\nHello\n\n";
    let result = parse_bytes(data.as_ref()).await.unwrap();
    assert_eq!(result.len(), 1);
    assert_eq!(result[0].text, "Hello");
  }

  #[test]
  fn test_detect_format_srt() {
    let data = b"1\n00:00:01,000 --> 00:00:03,500\nHello\n\n";
    assert_eq!(detect_format(data), Some(crate::model::SubtitleFormat::Srt));
  }

  #[test]
  fn test_detect_format_vtt() {
    let data = b"WEBVTT\n\n1\n00:00:01.000 --> 00:00:03.500\nHello\n\n";
    assert_eq!(detect_format(data), Some(crate::model::SubtitleFormat::Vtt));
  }

  #[test]
  fn test_subtitle_shift() {
    let mut sub = Subtitle::new(1000, 5000, "test");
    sub.shift(500);
    assert_eq!(sub.start, 1500);
    assert_eq!(sub.end, 5500);
    sub.shift(-2000);
    assert_eq!(sub.start, 0); // clamped
    assert_eq!(sub.end, 3500);
  }

  #[test]
  fn test_subtitle_duration() {
    let sub = Subtitle::new(1000, 5000, "test");
    assert_eq!(sub.duration_ms(), 4000);
  }
}