//! The angle-bracket markup SubRip and WebVTT share, plus the byte-level
//! tolerances every text format here needs.
//!
//! Both formats carry a subset of HTML — ``, ``, ``, ``, ``,
//! and WebVTT's ``, `` and inline timestamps — written by a
//! thousand tools with a thousand degrees of care. So the rule is that nothing
//! here fails: an unclosed tag closes at the end of the cue, a stray ``
//! with nothing open is dropped, and a `<` that begins no tag at all is the
//! character it looks like.
use crate::ir::{Rgb, Segment};
/// Bytes to text, the way a subtitle file has to be read: a UTF-8 or UTF-16 BOM
/// consumed, invalid sequences replaced rather than refused, and CRLF (or a
/// lone CR, which classic Mac tools still write) folded to LF so every parser
/// after this sees one line ending.
pub fn decode(bytes: &[u8]) -> String {
let text = if let Some(rest) = bytes.strip_prefix(&[0xEF, 0xBB, 0xBF]) {
String::from_utf8_lossy(rest).into_owned()
} else if let Some(rest) = bytes.strip_prefix(&[0xFF, 0xFE]) {
utf16(rest, u16::from_le_bytes)
} else if let Some(rest) = bytes.strip_prefix(&[0xFE, 0xFF]) {
utf16(rest, u16::from_be_bytes)
} else {
String::from_utf8_lossy(bytes).into_owned()
};
if text.contains('\r') {
text.replace("\r\n", "\n").replace('\r', "\n")
} else {
text
}
}
fn utf16(bytes: &[u8], unit: fn([u8; 2]) -> u16) -> String {
let (pairs, _) = bytes.as_chunks::<2>();
let units: Vec = pairs.iter().map(|&c| unit(c)).collect();
String::from_utf16_lossy(&units)
}
/// `hh:mm:ss,mmm` in microseconds — the timing both text formats state, with
/// every part they are written with in practice: comma or dot before the
/// fraction, hours optional, a fraction of one to six digits, a leading minus
/// on a file whose author shifted it too far.
pub fn parse_clock(field: &str) -> Option {
let field = field.trim();
let (sign, field) = match field.strip_prefix('-') {
Some(rest) => (-1, rest),
None => (1, field),
};
let (clock, fraction) = match field.split_once([',', '.']) {
Some((clock, fraction)) => (clock, fraction),
None => (field, ""),
};
let mut parts = clock.split(':').rev();
let seconds: i64 = parts.next()?.trim().parse().ok()?;
let minutes: i64 = parts.next().map_or(Ok(0), |m| m.trim().parse()).ok()?;
let hours: i64 = parts.next().map_or(Ok(0), |h| h.trim().parse()).ok()?;
if parts.next().is_some() {
return None;
}
// A fraction is a fraction whatever its length: `.5` is half a second and
// `.500000` is the same instant.
let digits: String = fraction
.chars()
.take_while(char::is_ascii_digit)
.take(6)
.collect();
let micros = if digits.is_empty() {
0
} else {
let value: i64 = digits.parse().ok()?;
value * 10i64.pow(6 - digits.len() as u32)
};
// A field can be any integer the parser accepts, so the sums are checked: a
// clock too large for an i64 of microseconds is not a clock.
let whole = hours
.checked_mul(3600)?
.checked_add(minutes.checked_mul(60)?)?
.checked_add(seconds)?;
let total = whole.checked_mul(1_000_000)?.checked_add(micros)?;
Some(sign * total)
}
/// How deep open tags may nest. Real markup nests a handful deep; past this an
/// open tag is dropped, so a cue of a million `` cannot make the stack
/// quadratic to close or the tree too deep to walk.
const MAX_NEST: usize = 64;
/// Which markup a cue body is written in.
#[derive(Clone, Copy, PartialEq, Eq)]
pub(crate) enum Dialect {
Srt,
WebVtt,
}
/// The cue being built: its top-level runs, and the open tags above them, innermost last.
#[derive(Default)]
struct Tree {
root: Vec,
open: Vec<(Open, Vec)>,
}
impl Tree {
fn top(&mut self) -> &mut Vec {
match self.open.last_mut() {
Some((_, children)) => children,
None => &mut self.root,
}
}
fn close_top(&mut self) {
let Some((open, children)) = self.open.pop() else {
return;
};
let parent = self.top();
match open.name.as_str() {
"b" => parent.push(Segment::Bold(children)),
"i" => parent.push(Segment::Italic(children)),
"u" => parent.push(Segment::Underline(children)),
"s" => parent.push(Segment::Strike(children)),
"v" => parent.push(Segment::Voice {
name: open.annotation,
children,
}),
"c" => parent.push(Segment::Class {
name: open.annotation,
children,
}),
_ => {
if let Some(rgb) = open.color {
parent.push(Segment::Color { rgb, children });
} else if open.family.is_some() || open.size.is_some() {
parent.push(Segment::Font {
family: open.family,
size: open.size,
children,
});
} else {
parent.extend(children);
}
}
}
}
}
/// One cue body's markup as runs. `WebVtt` turns on the two tags only WebVTT
/// has — `` and `` — and the inline timestamps that go with them.
pub(crate) fn parse_inline(body: &str, dialect: Dialect) -> Vec {
let mut tree = Tree::default();
let mut text = String::new();
let mut rest = body;
while let Some(at) = rest.find('<') {
text.push_str(&unescape(&rest[..at]));
let after = &rest[at + 1..];
let Some(end) = after.find('>') else {
text.push_str(&unescape(&rest[at..]));
rest = "";
break;
};
let tag = &after[..end];
rest = &after[end + 1..];
match classify(tag, dialect) {
Tag::Text => {
text.push('<');
text.push_str(&unescape(tag));
text.push('>');
}
Tag::Timestamp(offset_us) => {
flush(&mut text, &mut tree);
tree.top().push(Segment::Timestamp { offset_us });
}
Tag::Raw => {
flush(&mut text, &mut tree);
tree.top().push(Segment::Raw(format!("<{tag}>")));
}
Tag::Open(open) => {
flush(&mut text, &mut tree);
if tree.open.len() < MAX_NEST {
tree.open.push((open, Vec::new()));
}
}
Tag::Close(name) => {
flush(&mut text, &mut tree);
// Close the innermost frame with that name, and everything
// inside it: `` closes both, which is what a browser
// does with the same bytes.
let found = tree.open.iter().rposition(|(open, _)| open.name == name);
if let Some(depth) = found {
while tree.open.len() > depth {
tree.close_top();
}
}
}
}
}
text.push_str(&unescape(rest));
flush(&mut text, &mut tree);
while !tree.open.is_empty() {
tree.close_top();
}
tree.root
}
/// An open tag: its name and the arguments the closing wrapper will need.
struct Open {
name: String,
color: Option<(u8, u8, u8)>,
family: Option,
size: Option,
annotation: String,
}
enum Tag {
Open(Open),
Close(String),
Timestamp(i64),
Raw,
/// Not a tag at all — a `<` that means `<`.
Text,
}
fn classify(tag: &str, dialect: Dialect) -> Tag {
let trimmed = tag.trim();
if trimmed.is_empty() {
return Tag::Text;
}
if let Some(name) = trimmed.strip_prefix('/') {
let name = name.trim().to_ascii_lowercase();
let name = name.split(['.', ' ']).next().unwrap_or("").to_owned();
return Tag::Close(name);
}
// `<00:00:01.500>` is a cue timestamp, not an element.
if dialect == Dialect::WebVtt && trimmed.starts_with(|c: char| c.is_ascii_digit()) {
return match parse_clock(trimmed) {
Some(offset_us) => Tag::Timestamp(offset_us),
None => Tag::Text,
};
}
let (head, attrs) = match trimmed.split_once(char::is_whitespace) {
Some((head, attrs)) => (head, attrs),
None => (trimmed, ""),
};
let (head, classes) = match head.split_once('.') {
Some((head, classes)) => (head, classes),
None => (head, ""),
};
let name = head.trim_end_matches('/').to_ascii_lowercase();
match name.as_str() {
"b" | "i" | "u" | "s" => Tag::Open(Open {
name,
color: None,
family: None,
size: None,
annotation: String::new(),
}),
"font" => Tag::Open(Open {
name,
color: attribute(attrs, "color").and_then(|v| parse_color(&v)),
family: attribute(attrs, "face"),
size: attribute(attrs, "size").and_then(|v| v.trim().parse().ok()),
annotation: String::new(),
}),
"v" | "c" if dialect == Dialect::WebVtt => Tag::Open(Open {
annotation: match name.as_str() {
"v" => attrs.trim().to_owned(),
_ => classes.replace('.', " "),
},
name,
color: None,
family: None,
size: None,
}),
_ => Tag::Raw,
}
}
/// `name="value"` / `name=value` out of a tag's attribute text.
fn attribute(attrs: &str, name: &str) -> Option {
let lower = attrs.to_ascii_lowercase();
let mut from = 0;
loop {
let at = lower[from..].find(name)? + from;
let after = attrs[at + name.len()..].trim_start();
match after.strip_prefix('=') {
Some(value) => {
let value = value.trim_start();
let quoted = value.strip_prefix(['"', '\'']);
return Some(match quoted {
Some(v) => v.split(['"', '\'']).next().unwrap_or(v).to_owned(),
None => value
.split_whitespace()
.next()
.unwrap_or_default()
.trim_end_matches('/')
.to_owned(),
});
}
None => from = at + name.len(),
}
}
}
/// `#rrggbb`, `#rgb`, or one of the sixteen names HTML has always had.
fn parse_color(value: &str) -> Option {
let value = value.trim();
if let Some(hex) = value.strip_prefix('#') {
let digits: String = hex.chars().take_while(char::is_ascii_hexdigit).collect();
let byte = |s: &str| u8::from_str_radix(s, 16).ok();
return match digits.len() {
3 => {
let mut c = digits.chars().map(|c| byte(&format!("{c}{c}")));
Some((c.next()??, c.next()??, c.next()??))
}
6 | 8 => Some((
byte(&digits[0..2])?,
byte(&digits[2..4])?,
byte(&digits[4..6])?,
)),
_ => None,
};
}
let named = [
("black", (0, 0, 0)),
("silver", (192, 192, 192)),
("gray", (128, 128, 128)),
("grey", (128, 128, 128)),
("white", (255, 255, 255)),
("maroon", (128, 0, 0)),
("red", (255, 0, 0)),
("purple", (128, 0, 128)),
("fuchsia", (255, 0, 255)),
("green", (0, 128, 0)),
("lime", (0, 255, 0)),
("olive", (128, 128, 0)),
("yellow", (255, 255, 0)),
("navy", (0, 0, 128)),
("blue", (0, 0, 255)),
("teal", (0, 128, 128)),
("aqua", (0, 255, 255)),
("cyan", (0, 255, 255)),
("magenta", (255, 0, 255)),
];
let lower = value.to_ascii_lowercase();
named
.iter()
.find(|(name, _)| *name == lower)
.map(|(_, rgb)| *rgb)
}
/// The handful of entities subtitle text is written with, plus numeric ones.
fn unescape(text: &str) -> String {
if !text.contains('&') {
return text.to_owned();
}
let mut out = String::with_capacity(text.len());
let mut rest = text;
while let Some(at) = rest.find('&') {
out.push_str(&rest[..at]);
let after = &rest[at + 1..];
// An entity name is at most ten bytes, so the search stops there; an
// unbounded search made a run of bare `&` quadratic.
let window = &after.as_bytes()[..after.len().min(11)];
let end = window.iter().position(|&b| b == b';');
let Some(end) = end else {
out.push('&');
rest = after;
continue;
};
let name = &after[..end];
let replacement = match name.to_ascii_lowercase().as_str() {
"amp" => Some('&'),
"lt" => Some('<'),
"gt" => Some('>'),
"quot" => Some('"'),
"apos" => Some('\''),
"nbsp" => Some('\u{a0}'),
"lrm" => Some('\u{200e}'),
"rlm" => Some('\u{200f}'),
other => other
.strip_prefix('#')
.and_then(|n| match n.strip_prefix(['x', 'X']) {
Some(hex) => u32::from_str_radix(hex, 16).ok(),
None => n.parse().ok(),
})
.and_then(char::from_u32),
};
match replacement {
Some(c) => {
out.push(c);
rest = &after[end + 1..];
}
None => {
out.push('&');
rest = after;
}
}
}
out.push_str(rest);
out
}
fn flush(text: &mut String, tree: &mut Tree) {
if !text.is_empty() {
let taken = std::mem::take(text);
let mut lines = taken.split('\n');
if let Some(first) = lines.next()
&& !first.is_empty()
{
tree.top().push(Segment::Text(first.into()));
}
for line in lines {
let children = tree.top();
children.push(Segment::LineBreak);
if !line.is_empty() {
children.push(Segment::Text(line.into()));
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ir::plain_text;
#[test]
fn clocks_are_read_in_every_shape_a_file_writes_them() {
assert_eq!(parse_clock("00:00:01,500"), Some(1_500_000));
assert_eq!(parse_clock("00:00:01.5"), Some(1_500_000));
assert_eq!(parse_clock("1:02:03.250"), Some(3_723_250_000));
// Hours omitted, as WebVTT allows.
assert_eq!(parse_clock("02:03.000"), Some(123_000_000));
// Six digits and beyond.
assert_eq!(parse_clock("0:00:00.123456"), Some(123_456));
assert_eq!(parse_clock("0:00:00.1234569"), Some(123_456));
assert_eq!(parse_clock("-0:00:01.000"), Some(-1_000_000));
assert_eq!(parse_clock("not a time"), None);
assert_eq!(parse_clock("1:2:3:4.0"), None);
}
#[test]
fn decode_takes_the_bom_and_folds_the_line_endings() {
assert_eq!(decode(b"\xef\xbb\xbfa\r\nb\rc"), "a\nb\nc");
assert_eq!(decode(&[0xFF, 0xFE, b'h', 0, b'i', 0]), "hi");
// Invalid UTF-8 is replaced, never refused.
assert_eq!(decode(b"a\xffb"), "a\u{fffd}b");
}
#[test]
fn markup_nests_and_forgives() {
let segments = parse_inline("a b", Dialect::Srt);
assert_eq!(plain_text(&segments), "a b");
assert!(matches!(segments[0], Segment::Italic(_)));
assert!(matches!(segments[2], Segment::Bold(_)));
// Mismatched close, stray close, and a `<` that is just a `<`.
assert_eq!(plain_text(&parse_inline("xy", Dialect::Srt)), "xy");
assert_eq!(plain_text(&parse_inline("plain", Dialect::Srt)), "plain");
assert_eq!(plain_text(&parse_inline("a < b", Dialect::Srt)), "a < b");
assert_eq!(
plain_text(&parse_inline("2 < 3 & 4", Dialect::Srt)),
"2 < 3 & 4"
);
}
#[test]
fn webvtt_only_tags_are_off_for_srt() {
let vtt = parse_inline("hi", Dialect::WebVtt);
assert!(matches!(vtt[0], Segment::Voice { .. }));
assert_eq!(plain_text(&vtt), "hi");
let srt = parse_inline("hi", Dialect::Srt);
assert_eq!(plain_text(&srt), "hi");
assert!(matches!(srt[0], Segment::Raw(_)));
}
#[test]
fn font_colour_is_read_by_hex_and_by_name() {
let segments = parse_inline("red", Dialect::Srt);
assert!(matches!(
segments[0],
Segment::Color {
rgb: (255, 0, 0),
..
}
));
let named = parse_inline("green", Dialect::Srt);
assert!(matches!(
named[0],
Segment::Color {
rgb: (0, 255, 0),
..
}
));
}
}