Skip to main content

alpha2_model/
caption_import.rs

1//! SRT and WebVTT import: subtitle files to caption cues.
2//!
3//! A millisecond is exactly 120 ticks, so import is lossless. Markup that says who speaks or
4//! what a word is becomes the cue's speaker and runs: a WebVTT voice (`<v Name>`) sets the
5//! speaker and gives classes `voice` plus the voice's classes, `<c.a.b>` gives `a` and `b`,
6//! `<b>`, `<i>` and `<u>` (also in SRT) give `b`, `i` and `u`, and WebVTT inline timestamps
7//! split the text into runs timed from the cue's start. Other markup is dropped: SRT's other
8//! tags and `{\...}` overrides, WebVTT's `ruby`, `rt` and `lang` tags (text kept) and cue
9//! settings. WebVTT character references are decoded. A `<` in SRT that opens no tag is
10//! text. Blocks that do not parse are skipped and counted.
11//!
12//! A line ends at LF, CR or CRLF, as WebVTT defines; repeated CRs before an LF (line endings
13//! converted twice) end one line.
14//!
15//! Reading is linear in file size. Files over [`MAX_CAPTION_FILE_BYTES`] or with more than
16//! [`MAX_CAPTION_FILE_CUES`] cues are refused early. A block over [`MAX_CUE_BLOCK_BYTES`] or
17//! [`MAX_CUE_BLOCK_LINES`] is skipped without being held. One pass over a cue's markup gives both
18//! its text and its runs; a cue with more than [`MAX_OPEN_TAGS`] tags open, [`MAX_HELD_CLASSES`]
19//! classes held or [`MAX_TEXT_RUNS`] runs is imported as plain text.
20
21use std::cmp::Reverse;
22use std::collections::{BTreeSet, BinaryHeap};
23
24use crate::{
25    CaptionCue, MAX_CLIPS_PER_TRACK, MAX_PROJECT_JSON_BYTES, MAX_RUN_CLASSES,
26    MAX_TEXT_CONTENT_BYTES, MAX_TEXT_RUNS, MAX_TRACKS, RunTime, TextContent, TextRun,
27    valid_class_name,
28};
29use alpha2_time::{MediaTime, TICKS_PER_SECOND, TimeRange};
30
31/// The largest subtitle file read, in bytes. A cue's JSON is longer than its subtitle block,
32/// so a larger file holds more cues than a project can.
33pub const MAX_CAPTION_FILE_BYTES: usize = MAX_PROJECT_JSON_BYTES;
34
35/// The most cues read from one subtitle file: a day of continuous captions at one cue every
36/// 1.3 s.
37pub const MAX_CAPTION_FILE_CUES: usize = 65_536;
38
39/// The longest block read as a cue, in bytes; a longer one is skipped.
40pub const MAX_CUE_BLOCK_BYTES: usize = 65_536;
41
42/// The most lines of a block read as a cue; a longer one is skipped.
43pub const MAX_CUE_BLOCK_LINES: usize = 1_024;
44
45/// The most WebVTT tags open at once in a cue whose markup is kept.
46pub const MAX_OPEN_TAGS: usize = 32;
47
48/// The most distinct classes the open tags of a cue whose markup is kept give at once.
49pub const MAX_HELD_CLASSES: usize = 64;
50
51/// Cues read from a subtitle file.
52#[derive(Clone, Debug, Default, PartialEq)]
53pub struct ImportedCaptions {
54    /// Cues in file order, sorted by start time. They may overlap; see [`caption_rows`].
55    pub cues: Vec<CaptionCue>,
56    /// Blocks that were not cues and could not be read as one.
57    pub skipped: usize,
58    /// Markup read but not kept.
59    pub notes: ImportNotes,
60}
61
62/// What an import read but could not keep, counted.
63#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
64pub struct ImportNotes {
65    /// Voices after a cue's first, whose names are not its speaker. Their text still takes the
66    /// class `voice`.
67    pub extra_voices: usize,
68    /// Inline timestamps out of order or outside their cue, which WebVTT renderers ignore.
69    pub dropped_timestamps: usize,
70    /// Class names a run cannot carry (not `A-Z a-z 0-9 _ -`, longer than 64 bytes, or past
71    /// [`MAX_RUN_CLASSES`]).
72    pub dropped_classes: usize,
73    /// Cues imported as plain text because their markup had more than [`MAX_TEXT_RUNS`] runs,
74    /// 32 tags open or 64 classes held.
75    pub flattened: usize,
76}
77
78/// Why a file is not a subtitle file at all.
79#[derive(Clone, Copy, Debug, Eq, PartialEq)]
80#[non_exhaustive]
81pub enum CaptionImportError {
82    /// A WebVTT file does not begin with `WEBVTT`.
83    MissingWebVttHeader,
84    /// The file is larger than [`MAX_CAPTION_FILE_BYTES`].
85    FileTooLarge {
86        /// The file's size in bytes.
87        bytes: usize,
88        /// The largest file read.
89        maximum: usize,
90    },
91    /// The file holds more than [`MAX_CAPTION_FILE_CUES`] cues.
92    TooManyCues {
93        /// The most cues read from a file.
94        maximum: usize,
95    },
96    /// The cues need more caption tracks than a project holds: too many overlap at once, or there
97    /// are too many to fit [`MAX_CLIPS_PER_TRACK`] to a track.
98    TooManyRows {
99        /// Caption tracks the cues need.
100        rows: usize,
101        /// Tracks a project holds.
102        maximum: usize,
103    },
104}
105
106impl std::fmt::Display for CaptionImportError {
107    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
108        match self {
109            Self::MissingWebVttHeader => formatter.write_str("a WebVTT file begins with WEBVTT"),
110            Self::FileTooLarge { bytes, maximum } => write!(
111                formatter,
112                "the subtitle file is {bytes} bytes; at most {maximum} are read"
113            ),
114            Self::TooManyCues { maximum } => write!(
115                formatter,
116                "the subtitle file holds more than {maximum} cues"
117            ),
118            Self::TooManyRows { rows, maximum } => write!(
119                formatter,
120                "the captions need {rows} caption tracks; a project holds at most {maximum} tracks"
121            ),
122        }
123    }
124}
125
126impl std::error::Error for CaptionImportError {}
127
128const TICKS_PER_MILLISECOND: i64 = TICKS_PER_SECOND / 1_000;
129
130/// Reads SubRip (`.srt`) text.
131///
132/// # Errors
133/// Returns [`CaptionImportError::FileTooLarge`] or [`CaptionImportError::TooManyCues`] for a
134/// file past the import limits.
135pub fn parse_srt(text: &str) -> Result<ImportedCaptions, CaptionImportError> {
136    check_file_size(text)?;
137    let mut imported = ImportedCaptions::default();
138    for block in Blocks::new(text) {
139        let Some(block) = block else {
140            imported.skipped += 1;
141            continue;
142        };
143        let mut lines = block.iter().copied();
144        let Some(mut first) = lines.next() else {
145            continue;
146        };
147        // An optional sequence number before the timing line.
148        if !first.contains("-->") {
149            match lines.next() {
150                Some(line) if first.trim().chars().all(|c| c.is_ascii_digit()) => first = line,
151                _ => {
152                    imported.skipped += 1;
153                    continue;
154                }
155            }
156        }
157        let body = lines.collect::<Vec<_>>();
158        let range = timing(first);
159        let marked = range.map(|_| srt_markup(&body));
160        imported.read(range, marked)?;
161    }
162    imported.cues.sort_by_key(|cue| cue.timeline_range.start());
163    Ok(imported)
164}
165
166/// Reads WebVTT (`.vtt`) text. `NOTE`, `STYLE`, and `REGION` blocks are ignored.
167///
168/// # Errors
169/// Returns [`CaptionImportError::MissingWebVttHeader`] unless the text begins with `WEBVTT`,
170/// and [`CaptionImportError::FileTooLarge`] or [`CaptionImportError::TooManyCues`] for a
171/// file past the import limits.
172pub fn parse_webvtt(text: &str) -> Result<ImportedCaptions, CaptionImportError> {
173    check_file_size(text)?;
174    let text = text.strip_prefix('\u{feff}').unwrap_or(text);
175    let header_ok = text
176        .strip_prefix("WEBVTT")
177        .is_some_and(|rest| rest.is_empty() || rest.starts_with([' ', '\t', '\n', '\r']));
178    if !header_ok {
179        return Err(CaptionImportError::MissingWebVttHeader);
180    }
181    let mut imported = ImportedCaptions::default();
182    let mut blocks = Blocks::new(text);
183    // The header is the first block up to a timing line; as in the WebVTT parser, a cue may
184    // follow it without a blank line. An oversized header holds no cue that is read.
185    let header = blocks.next().flatten().unwrap_or_default();
186    let cue_under_header = header
187        .iter()
188        .position(|line| line.contains("-->"))
189        .map(|start| Some(header[start..].to_vec()));
190    for block in cue_under_header.into_iter().chain(blocks) {
191        let Some(block) = block else {
192            imported.skipped += 1;
193            continue;
194        };
195        let first = block[0];
196        if ["NOTE", "STYLE", "REGION"].iter().any(|keyword| {
197            first
198                .strip_prefix(keyword)
199                .is_some_and(|rest| rest.is_empty() || rest.starts_with([' ', '\t']))
200        }) {
201            continue;
202        }
203        // An optional cue identifier before the timing line.
204        let (timing_line, body) = if first.contains("-->") {
205            (first, &block[1..])
206        } else if block.len() > 1 && block[1].contains("-->") {
207            (block[1], &block[2..])
208        } else {
209            imported.skipped += 1;
210            continue;
211        };
212        let range = timing(timing_line);
213        let marked = range.map(|range| vtt_markup(&body.join("\n"), range));
214        imported.read(range, marked)?;
215    }
216    imported.cues.sort_by_key(|cue| cue.timeline_range.start());
217    Ok(imported)
218}
219
220impl ImportedCaptions {
221    /// Keeps a block's cue, or counts it skipped when its timing or text is invalid. `marked` is
222    /// the cue's text with the speaker and runs its markup gives.
223    fn read(
224        &mut self,
225        range: Option<TimeRange>,
226        marked: Option<Marked>,
227    ) -> Result<(), CaptionImportError> {
228        let (Some(range), Some(marked)) = (range, marked) else {
229            self.skipped += 1;
230            return Ok(());
231        };
232        let Some(text) = cue_text(&marked.text) else {
233            self.skipped += 1;
234            return Ok(());
235        };
236        if self.cues.len() == MAX_CAPTION_FILE_CUES {
237            return Err(CaptionImportError::TooManyCues {
238                maximum: MAX_CAPTION_FILE_CUES,
239            });
240        }
241        let mut cue = CaptionCue::new(range, text);
242        let notes = &mut self.notes;
243        notes.extra_voices += marked.notes.extra_voices;
244        notes.dropped_timestamps += marked.notes.dropped_timestamps;
245        notes.dropped_classes += marked.notes.dropped_classes;
246        notes.flattened += marked.notes.flattened;
247        cue.speaker = marked.speaker;
248        if let Some(runs) = marked.runs.filter(|runs| !runs.is_empty()) {
249            cue.text = TextContent::Runs(runs);
250        }
251        self.cues.push(cue);
252        Ok(())
253    }
254}
255
256fn check_file_size(text: &str) -> Result<(), CaptionImportError> {
257    if text.len() > MAX_CAPTION_FILE_BYTES {
258        return Err(CaptionImportError::FileTooLarge {
259            bytes: text.len(),
260            maximum: MAX_CAPTION_FILE_BYTES,
261        });
262    }
263    Ok(())
264}
265
266/// Splits cues onto rows, one caption track each. Every cue goes to the first row with room
267/// (a track holds [`MAX_CLIPS_PER_TRACK`]) whose last cue has ended by its start.
268/// O(n log n) in the cues, however many overlap.
269///
270/// # Errors
271/// Returns [`CaptionImportError::TooManyRows`] for cues that need more rows than a project
272/// holds tracks.
273pub fn caption_rows(cues: &[CaptionCue]) -> Result<Vec<Vec<CaptionCue>>, CaptionImportError> {
274    let mut sorted = cues.to_vec();
275    sorted.sort_by_key(|cue| cue.timeline_range.start());
276    let mut rows: Vec<Vec<CaptionCue>> = Vec::new();
277    // Rows with room, split into those whose last cue has ended by the cue being placed and the
278    // others keyed by when theirs ends. A full row is in neither.
279    let mut ended = BTreeSet::<usize>::new();
280    let mut ending = BinaryHeap::<Reverse<(MediaTime, usize)>>::new();
281    for cue in sorted {
282        let start = cue.timeline_range.start();
283        while let Some(&Reverse((end, row))) = ending.peek()
284            && end <= start
285        {
286            ending.pop();
287            ended.insert(row);
288        }
289        let row = ended.pop_first().unwrap_or_else(|| {
290            rows.push(Vec::new());
291            rows.len() - 1
292        });
293        let end = cue.timeline_range.end();
294        rows[row].push(cue);
295        if rows[row].len() < MAX_CLIPS_PER_TRACK {
296            ending.push(Reverse((end, row)));
297        }
298    }
299    if rows.len() > MAX_TRACKS {
300        return Err(CaptionImportError::TooManyRows {
301            rows: rows.len(),
302            maximum: MAX_TRACKS,
303        });
304    }
305    Ok(rows)
306}
307
308/// Blank-line-separated blocks of lines, one at a time. `None` stands for a block over
309/// [`MAX_CUE_BLOCK_BYTES`] or [`MAX_CUE_BLOCK_LINES`], whose lines are not kept.
310struct Blocks<'a> {
311    lines: Lines<'a>,
312}
313
314impl<'a> Blocks<'a> {
315    fn new(text: &'a str) -> Self {
316        Self {
317            lines: Lines {
318                text: text.strip_prefix('\u{feff}').unwrap_or(text),
319                at: 0,
320                pending_blanks: 0,
321                done: false,
322            },
323        }
324    }
325}
326
327impl<'a> Iterator for Blocks<'a> {
328    type Item = Option<Vec<&'a str>>;
329
330    fn next(&mut self) -> Option<Self::Item> {
331        let mut block = Vec::new();
332        let mut bytes = 0_usize;
333        let mut oversized = false;
334        for line in self.lines.by_ref() {
335            if line.trim().is_empty() {
336                if oversized || !block.is_empty() {
337                    break;
338                }
339                continue;
340            }
341            if oversized {
342                continue;
343            }
344            bytes += line.len() + 1;
345            if bytes > MAX_CUE_BLOCK_BYTES || block.len() == MAX_CUE_BLOCK_LINES {
346                oversized = true;
347                block = Vec::new();
348                continue;
349            }
350            block.push(line);
351        }
352        if oversized {
353            Some(None)
354        } else {
355            (!block.is_empty()).then_some(Some(block))
356        }
357    }
358}
359
360/// The lines of a text. LF, CR or CRLF ends a line, as does a run of CRs followed by LF. A CR
361/// run without a following LF ends a line per CR.
362struct Lines<'a> {
363    text: &'a str,
364    at: usize,
365    /// Empty lines still owed by a CR run.
366    pending_blanks: usize,
367    done: bool,
368}
369
370impl<'a> Iterator for Lines<'a> {
371    type Item = &'a str;
372
373    fn next(&mut self) -> Option<&'a str> {
374        if self.pending_blanks > 0 {
375            self.pending_blanks -= 1;
376            return Some("");
377        }
378        if self.done {
379            return None;
380        }
381        let bytes = self.text.as_bytes();
382        let start = self.at;
383        let Some(end) = bytes[start..]
384            .iter()
385            .position(|&byte| matches!(byte, b'\n' | b'\r'))
386            .map(|offset| start + offset)
387        else {
388            self.done = true;
389            return Some(&self.text[start..]);
390        };
391        if bytes[end] == b'\n' {
392            self.at = end + 1;
393        } else {
394            let returns = bytes[end..]
395                .iter()
396                .take_while(|&&byte| byte == b'\r')
397                .count();
398            self.at = end + returns;
399            if bytes.get(self.at) == Some(&b'\n') {
400                self.at += 1;
401            } else {
402                self.pending_blanks = returns - 1;
403            }
404        }
405        Some(&self.text[start..end])
406    }
407}
408
409/// `start --> end`, ignoring anything after the end time (WebVTT settings).
410fn timing(line: &str) -> Option<TimeRange> {
411    let (start, rest) = line.split_once("-->")?;
412    let end = rest.split_whitespace().next()?;
413    let (start, end) = (timestamp(start.trim())?, timestamp(end)?);
414    TimeRange::new(start, end)
415        .ok()
416        .filter(|range| !range.is_empty())
417}
418
419/// `[hh:]mm:ss[,.]mmm`, hours any number of digits, up to
420/// [`alpha2_time::MAX_SERIALIZED_TICKS`] (a later cue could not be saved).
421fn timestamp(text: &str) -> Option<MediaTime> {
422    let (clock, millis) = text.split_once([',', '.'])?;
423    if millis.len() != 3 || !millis.bytes().all(|byte| byte.is_ascii_digit()) {
424        return None;
425    }
426    let fields = clock.split(':').collect::<Vec<_>>();
427    let number = |field: &str| {
428        (!field.is_empty() && field.bytes().all(|byte| byte.is_ascii_digit()))
429            .then(|| field.parse::<i64>().ok())
430            .flatten()
431    };
432    let (hours, minutes, seconds) = match fields.as_slice() {
433        [hours, minutes, seconds] => (number(hours)?, number(minutes)?, number(seconds)?),
434        [minutes, seconds] => (0, number(minutes)?, number(seconds)?),
435        _ => return None,
436    };
437    if minutes > 59 || seconds > 59 {
438        return None;
439    }
440    let milliseconds = ((hours.checked_mul(60)?.checked_add(minutes)?)
441        .checked_mul(60)?
442        .checked_add(seconds)?)
443    .checked_mul(1_000)?
444    .checked_add(millis.parse::<i64>().ok()?)?;
445    let ticks = milliseconds.checked_mul(TICKS_PER_MILLISECOND)?;
446    (ticks <= alpha2_time::MAX_SERIALIZED_TICKS).then(|| MediaTime::from_ticks(ticks))
447}
448
449/// Cue text with surrounding blank lines trimmed, or `None` when empty or too long.
450fn cue_text(text: &str) -> Option<String> {
451    let text = text.trim_matches('\n').to_owned();
452    (!text.trim().is_empty() && text.len() <= MAX_TEXT_CONTENT_BYTES).then_some(text)
453}
454
455/// Decodes WebVTT's named and numeric character references; unknown ones stay as written.
456///
457/// A reference is letters, digits, `#` and `+` closed by `;`, so each `&` looks only that
458/// far and the text is read in linear time.
459fn decode_references(text: &str) -> String {
460    let mut output = String::with_capacity(text.len());
461    let mut rest = text;
462    while let Some(start) = rest.find('&') {
463        output.push_str(&rest[..start]);
464        rest = &rest[start + 1..];
465        let length = rest
466            .bytes()
467            .take_while(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'#' | b'+'))
468            .count();
469        let decoded = (rest.as_bytes().get(length) == Some(&b';'))
470            .then(|| reference(&rest[..length]))
471            .flatten();
472        match decoded {
473            Some(character) => {
474                output.push(character);
475                rest = &rest[length + 1..];
476            }
477            None => output.push('&'),
478        }
479    }
480    output.push_str(rest);
481    output
482}
483
484/// The character a reference's name, between `&` and `;`, stands for.
485fn reference(name: &str) -> Option<char> {
486    match name {
487        "amp" => Some('&'),
488        "lt" => Some('<'),
489        "gt" => Some('>'),
490        "nbsp" => Some('\u{a0}'),
491        "lrm" => Some('\u{200e}'),
492        "rlm" => Some('\u{200f}'),
493        _ => name
494            .strip_prefix("#x")
495            .or_else(|| name.strip_prefix("#X"))
496            .and_then(|hex| u32::from_str_radix(hex, 16).ok())
497            .or_else(|| name.strip_prefix('#').and_then(|dec| dec.parse().ok()))
498            .and_then(char::from_u32),
499    }
500}
501
502/// A cue's text and what its markup says: its speaker, its runs when any carries a class or a
503/// time (else the plain text stands), and what was dropped.
504struct Marked {
505    text: String,
506    speaker: Option<String>,
507    runs: Option<Vec<TextRun>>,
508    notes: ImportNotes,
509}
510
511/// Builds a cue's text and runs from text pieces, each with its open classes and time segment.
512/// Markup stops being tracked (the cue is imported as plain text) past [`MAX_TEXT_RUNS`] pieces
513/// beyond what trimming can remove.
514struct Runs {
515    text: String,
516    pieces: Vec<(String, Vec<String>, usize)>,
517    tracking: bool,
518    notes: ImportNotes,
519}
520
521impl Runs {
522    fn new() -> Self {
523        Self {
524            text: String::new(),
525            pieces: Vec::new(),
526            tracking: true,
527            notes: ImportNotes::default(),
528        }
529    }
530
531    /// Stops tracking markup: the cue is imported as plain text.
532    fn flatten(&mut self) {
533        if self.tracking {
534            self.tracking = false;
535            self.pieces = Vec::new();
536            self.notes.flattened += 1;
537        }
538    }
539
540    fn push(&mut self, text: &str, classes: &[String], segment: usize) {
541        self.text.push_str(text);
542        if !self.tracking {
543            return;
544        }
545        // Leading line feeds are trimmed from the runs as `cue_text` trims them from the text.
546        let text = if self.pieces.is_empty() {
547            text.trim_start_matches('\n')
548        } else {
549            text
550        };
551        if text.is_empty() {
552            return;
553        }
554        match self.pieces.last_mut() {
555            Some((held, open, at)) if open.as_slice() == classes && *at == segment => {
556                held.push_str(text);
557            }
558            _ => {
559                // Two more than the limit: trailing line feeds can still remove one.
560                if self.pieces.len() > MAX_TEXT_RUNS + 1 {
561                    self.flatten();
562                    return;
563                }
564                self.pieces
565                    .push((text.to_owned(), classes.to_vec(), segment));
566            }
567        }
568    }
569
570    /// The text and runs, timed by `bounds` (each segment's start, then the end, in ticks from
571    /// the cue's start), with trailing line feeds trimmed as for plain cue text. No runs when
572    /// none carries a class or a time.
573    fn finish(mut self, speaker: Option<String>, bounds: &[i64]) -> Marked {
574        while let Some(last) = self.pieces.last_mut() {
575            let kept = last.0.trim_end_matches('\n').len();
576            last.0.truncate(kept);
577            if last.0.is_empty() {
578                self.pieces.pop();
579            } else {
580                break;
581            }
582        }
583        if self.pieces.len() > MAX_TEXT_RUNS {
584            self.flatten();
585        }
586        let timed = bounds.len() > 2;
587        let runs = (self.tracking
588            && (timed
589                || self
590                    .pieces
591                    .iter()
592                    .any(|(_, classes, _)| !classes.is_empty())))
593        .then(|| {
594            self.pieces
595                .into_iter()
596                .map(|(text, mut class, segment)| {
597                    if class.len() > MAX_RUN_CLASSES {
598                        self.notes.dropped_classes += class.len() - MAX_RUN_CLASSES;
599                        class.truncate(MAX_RUN_CLASSES);
600                    }
601                    TextRun {
602                        text,
603                        class,
604                        time: timed.then(|| RunTime {
605                            start: MediaTime(bounds[segment]),
606                            end: MediaTime(bounds[segment + 1]),
607                        }),
608                    }
609                })
610                .collect()
611        });
612        Marked {
613            text: self.text,
614            speaker,
615            runs,
616            notes: self.notes,
617        }
618    }
619}
620
621/// The classes a tag's dot-separated `names` give; ones a run cannot carry are counted and
622/// dropped.
623fn tag_classes(names: &str, notes: &mut ImportNotes) -> Vec<String> {
624    names
625        .split('.')
626        .filter(|name| !name.is_empty())
627        .filter_map(|name| {
628            if valid_class_name(name) {
629                Some(name.to_owned())
630            } else {
631                notes.dropped_classes += 1;
632                None
633            }
634        })
635        .collect()
636}
637
638/// A WebVTT cue body's text and markup for a cue over `range`: voices, classes, bold, italic and
639/// underline as run classes, and inline timestamps splitting the runs. A `<` with no `>` after it
640/// ends the cue text, as the WebVTT cue text tokenizer does. Linear in the body: each open tag
641/// records how many classes it added, and closing it truncates them.
642fn vtt_markup(body: &str, range: TimeRange) -> Marked {
643    let start = range.start().ticks();
644    let duration = range.duration().map_or(0, MediaTime::ticks);
645    let mut runs = Runs::new();
646    let mut speaker = None;
647    // The tags open, each with the length of `classes` before it opened, innermost last.
648    let mut open: Vec<(&str, usize)> = Vec::new();
649    // The distinct classes the open tags give, in order.
650    let mut classes: Vec<String> = Vec::new();
651    let mut bounds = vec![0_i64];
652    let mut rest = body;
653    loop {
654        let Some(at) = rest.find('<') else {
655            runs.push(&decode_references(rest), &classes, bounds.len() - 1);
656            break;
657        };
658        runs.push(&decode_references(&rest[..at]), &classes, bounds.len() - 1);
659        let Some(end) = rest[at..].find('>') else {
660            break;
661        };
662        let tag = &rest[at + 1..at + end];
663        rest = &rest[at + end + 1..];
664        if let Some(name) = tag.strip_prefix('/') {
665            let name = name.trim();
666            if let Some(index) = open.iter().rposition(|(open, _)| *open == name) {
667                classes.truncate(open[index].1);
668                open.truncate(index);
669            }
670            continue;
671        }
672        if tag.starts_with(|character: char| character.is_ascii_digit()) {
673            // An inline timestamp: kept only in order and inside the cue.
674            match timestamp(tag.trim()) {
675                Some(time)
676                    if time.ticks() - start > *bounds.last().unwrap_or(&0)
677                        && time.ticks() - start < duration =>
678                {
679                    if runs.tracking {
680                        bounds.push(time.ticks() - start);
681                    }
682                }
683                _ => runs.notes.dropped_timestamps += 1,
684            }
685            continue;
686        }
687        // `name.classes annotation`.
688        let (head, annotation) = tag
689            .split_once([' ', '\t'])
690            .map_or((tag, ""), |(head, annotation)| (head, annotation.trim()));
691        let (name, names) = head.split_once('.').unwrap_or((head, ""));
692        let given = match name {
693            "v" => {
694                if speaker.is_none() && !annotation.is_empty() {
695                    speaker = Some(annotation.to_owned());
696                } else if !annotation.is_empty() {
697                    runs.notes.extra_voices += 1;
698                }
699                let mut given = vec!["voice".to_owned()];
700                given.extend(tag_classes(names, &mut runs.notes));
701                given
702            }
703            "c" => tag_classes(names, &mut runs.notes),
704            "b" | "i" | "u" => {
705                let mut given = vec![name.to_owned()];
706                given.extend(tag_classes(names, &mut runs.notes));
707                given
708            }
709            _ => Vec::new(),
710        };
711        if !runs.tracking {
712            continue;
713        }
714        if open.len() == MAX_OPEN_TAGS {
715            runs.flatten();
716            continue;
717        }
718        open.push((name, classes.len()));
719        for class in given {
720            if !classes.contains(&class) {
721                if classes.len() == MAX_HELD_CLASSES {
722                    runs.flatten();
723                    break;
724                }
725                classes.push(class);
726            }
727        }
728    }
729    bounds.push(duration);
730    let speaker = speaker.filter(|speaker| speaker.len() <= crate::MAX_CAPTION_NAME_BYTES);
731    runs.finish(speaker, &bounds)
732}
733
734/// An SRT cue body's markup: `<b>`, `<i>` and `<u>` as run classes; other tags and override
735/// blocks dropped.
736fn srt_markup(body: &[&str]) -> Marked {
737    let mut runs = Runs::new();
738    let mut open: Vec<String> = Vec::new();
739    for (index, line) in body.iter().enumerate() {
740        if index > 0 {
741            runs.push("\n", &open, 0);
742        }
743        let line = strip_overrides(line);
744        let mut rest = line.as_str();
745        while let Some(start) = rest.find('<') {
746            runs.push(&rest[..start], &open, 0);
747            let tag = &rest[start + 1..];
748            let closing = tag.starts_with('/');
749            let name = tag.strip_prefix('/').unwrap_or(tag);
750            match tag.find(['<', '>']) {
751                Some(end)
752                    if tag.as_bytes()[end] == b'>'
753                        && name.starts_with(|character: char| character.is_ascii_alphabetic()) =>
754                {
755                    let tag_name = name[..name.len().min(end - usize::from(closing))]
756                        .split([' ', '\t'])
757                        .next()
758                        .unwrap_or_default()
759                        .to_ascii_lowercase();
760                    if matches!(tag_name.as_str(), "b" | "i" | "u") {
761                        if closing {
762                            if let Some(index) = open.iter().rposition(|open| *open == tag_name) {
763                                open.remove(index);
764                            }
765                        } else if !open.contains(&tag_name) {
766                            open.push(tag_name);
767                        }
768                    }
769                    rest = &tag[end + 1..];
770                }
771                _ => {
772                    runs.push("<", &open, 0);
773                    rest = tag;
774                }
775            }
776        }
777        runs.push(rest, &open, 0);
778    }
779    runs.finish(None, &[0, 0])
780}
781
782/// SRT text without its `{\...}` override blocks.
783fn strip_overrides(line: &str) -> String {
784    let mut output = String::with_capacity(line.len());
785    let mut rest = line;
786    while let Some(start) = rest.find("{\\") {
787        output.push_str(&rest[..start]);
788        match rest[start..].find('}') {
789            Some(end) => rest = &rest[start + end + 1..],
790            None => {
791                rest = "";
792            }
793        }
794    }
795    output.push_str(rest);
796    output
797}
798
799#[cfg(test)]
800#[path = "caption_import_tests.rs"]
801mod tests;