S13: ec-subs markup Tree struct replaces the Option-bottom stack and its unwraps; Dialect enum replaces the webvtt bool (same operations)
Browse files at this commit5442b3dcca9ac32eaf862e6ef47a96959d2bbb58Parent: f75aa7d3 changed files +95 -93
- crates/ec-subs/src/markup.rs +91 -89
- crates/ec-subs/src/srt.rs +2 -2
- crates/ec-subs/src/webvtt.rs +2 -2
crates/ec-subs/src/markup.rsmodified+91 -89
| @@ -86,70 +86,119 @@ pub fn parse_clock(field: &str) -> Option<i64> { | ||
| 86 | 86 | /// quadratic to close or the tree too deep to walk. |
| 87 | 87 | const MAX_NEST: usize = 64; |
| 88 | 88 | |
| 89 | -/// One cue body's markup as runs. `webvtt` turns on the two tags only WebVTT | |
| 89 | +/// Which markup a cue body is written in. | |
| 90 | +#[derive(Clone, Copy, PartialEq, Eq)] | |
| 91 | +pub(crate) enum Dialect { | |
| 92 | + Srt, | |
| 93 | + WebVtt, | |
| 94 | +} | |
| 95 | + | |
| 96 | +/// The cue being built: its top-level runs, and the open tags above them, innermost last. | |
| 97 | +#[derive(Default)] | |
| 98 | +struct Tree { | |
| 99 | + root: Vec<Segment>, | |
| 100 | + open: Vec<(Open, Vec<Segment>)>, | |
| 101 | +} | |
| 102 | + | |
| 103 | +impl Tree { | |
| 104 | + fn top(&mut self) -> &mut Vec<Segment> { | |
| 105 | + match self.open.last_mut() { | |
| 106 | + Some((_, children)) => children, | |
| 107 | + None => &mut self.root, | |
| 108 | + } | |
| 109 | + } | |
| 110 | + | |
| 111 | + fn close_top(&mut self) { | |
| 112 | + let Some((open, children)) = self.open.pop() else { | |
| 113 | + return; | |
| 114 | + }; | |
| 115 | + let parent = self.top(); | |
| 116 | + match open.name.as_str() { | |
| 117 | + "b" => parent.push(Segment::Bold(children)), | |
| 118 | + "i" => parent.push(Segment::Italic(children)), | |
| 119 | + "u" => parent.push(Segment::Underline(children)), | |
| 120 | + "s" => parent.push(Segment::Strike(children)), | |
| 121 | + "v" => parent.push(Segment::Voice { | |
| 122 | + name: open.annotation, | |
| 123 | + children, | |
| 124 | + }), | |
| 125 | + "c" => parent.push(Segment::Class { | |
| 126 | + name: open.annotation, | |
| 127 | + children, | |
| 128 | + }), | |
| 129 | + _ => { | |
| 130 | + if let Some(rgb) = open.color { | |
| 131 | + parent.push(Segment::Color { rgb, children }); | |
| 132 | + } else if open.family.is_some() || open.size.is_some() { | |
| 133 | + parent.push(Segment::Font { | |
| 134 | + family: open.family, | |
| 135 | + size: open.size, | |
| 136 | + children, | |
| 137 | + }); | |
| 138 | + } else { | |
| 139 | + parent.extend(children); | |
| 140 | + } | |
| 141 | + } | |
| 142 | + } | |
| 143 | + } | |
| 144 | +} | |
| 145 | + | |
| 146 | +/// One cue body's markup as runs. `WebVtt` turns on the two tags only WebVTT | |
| 90 | 147 | /// has — `<v>` and `<c>` — and the inline timestamps that go with them. |
| 91 | -pub(crate) fn parse_inline(body: &str, webvtt: bool) -> Vec<Segment> { | |
| 92 | - // A stack of open tags, each with the children collected inside it so far; | |
| 93 | - // the bottom frame is the cue itself and can never be popped. | |
| 94 | - let mut stack: Vec<(Option<Open>, Vec<Segment>)> = vec![(None, Vec::new())]; | |
| 148 | +pub(crate) fn parse_inline(body: &str, dialect: Dialect) -> Vec<Segment> { | |
| 149 | + let mut tree = Tree::default(); | |
| 95 | 150 | let mut text = String::new(); |
| 96 | 151 | let mut rest = body; |
| 97 | 152 | while let Some(at) = rest.find('<') { |
| 98 | 153 | text.push_str(&unescape(&rest[..at])); |
| 99 | 154 | let after = &rest[at + 1..]; |
| 100 | 155 | let Some(end) = after.find('>') else { |
| 101 | - // No closing bracket anywhere: the rest of the cue is text. | |
| 102 | 156 | text.push_str(&unescape(&rest[at..])); |
| 103 | 157 | rest = ""; |
| 104 | 158 | break; |
| 105 | 159 | }; |
| 106 | 160 | let tag = &after[..end]; |
| 107 | 161 | rest = &after[end + 1..]; |
| 108 | - match classify(tag, webvtt) { | |
| 162 | + match classify(tag, dialect) { | |
| 109 | 163 | Tag::Text => { |
| 110 | 164 | text.push('<'); |
| 111 | 165 | text.push_str(&unescape(tag)); |
| 112 | 166 | text.push('>'); |
| 113 | 167 | } |
| 114 | 168 | Tag::Timestamp(offset_us) => { |
| 115 | - flush(&mut text, &mut stack); | |
| 116 | - push(&mut stack, Segment::Timestamp { offset_us }); | |
| 169 | + flush(&mut text, &mut tree); | |
| 170 | + tree.top().push(Segment::Timestamp { offset_us }); | |
| 117 | 171 | } |
| 118 | 172 | Tag::Raw => { |
| 119 | - flush(&mut text, &mut stack); | |
| 120 | - push(&mut stack, Segment::Raw(format!("<{tag}>"))); | |
| 173 | + flush(&mut text, &mut tree); | |
| 174 | + tree.top().push(Segment::Raw(format!("<{tag}>"))); | |
| 121 | 175 | } |
| 122 | 176 | Tag::Open(open) => { |
| 123 | - flush(&mut text, &mut stack); | |
| 124 | - if stack.len() <= MAX_NEST { | |
| 125 | - stack.push((Some(open), Vec::new())); | |
| 177 | + flush(&mut text, &mut tree); | |
| 178 | + if tree.open.len() < MAX_NEST { | |
| 179 | + tree.open.push((open, Vec::new())); | |
| 126 | 180 | } |
| 127 | 181 | } |
| 128 | 182 | Tag::Close(name) => { |
| 129 | - flush(&mut text, &mut stack); | |
| 183 | + flush(&mut text, &mut tree); | |
| 130 | 184 | // Close the innermost frame with that name, and everything |
| 131 | 185 | // inside it: `<b><i></b>` closes both, which is what a browser |
| 132 | 186 | // does with the same bytes. |
| 133 | - let found = stack | |
| 134 | - .iter() | |
| 135 | - .rposition(|(open, _)| open.as_ref().is_some_and(|o| o.name == name)); | |
| 187 | + let found = tree.open.iter().rposition(|(open, _)| open.name == name); | |
| 136 | 188 | if let Some(depth) = found { |
| 137 | - while stack.len() > depth { | |
| 138 | - close_top(&mut stack); | |
| 189 | + while tree.open.len() > depth { | |
| 190 | + tree.close_top(); | |
| 139 | 191 | } |
| 140 | 192 | } |
| 141 | 193 | } |
| 142 | 194 | } |
| 143 | 195 | } |
| 144 | 196 | text.push_str(&unescape(rest)); |
| 145 | - flush(&mut text, &mut stack); | |
| 146 | - while stack.len() > 1 { | |
| 147 | - close_top(&mut stack); | |
| 197 | + flush(&mut text, &mut tree); | |
| 198 | + while !tree.open.is_empty() { | |
| 199 | + tree.close_top(); | |
| 148 | 200 | } |
| 149 | - stack | |
| 150 | - .pop() | |
| 151 | - .map(|(_, children)| children) | |
| 152 | - .unwrap_or_default() | |
| 201 | + tree.root | |
| 153 | 202 | } |
| 154 | 203 | |
| 155 | 204 | /// An open tag: its name and the arguments the closing wrapper will need. |
| @@ -170,7 +219,7 @@ enum Tag { | ||
| 170 | 219 | Text, |
| 171 | 220 | } |
| 172 | 221 | |
| 173 | -fn classify(tag: &str, webvtt: bool) -> Tag { | |
| 222 | +fn classify(tag: &str, dialect: Dialect) -> Tag { | |
| 174 | 223 | let trimmed = tag.trim(); |
| 175 | 224 | if trimmed.is_empty() { |
| 176 | 225 | return Tag::Text; |
| @@ -181,7 +230,7 @@ fn classify(tag: &str, webvtt: bool) -> Tag { | ||
| 181 | 230 | return Tag::Close(name); |
| 182 | 231 | } |
| 183 | 232 | // `<00:00:01.500>` is a cue timestamp, not an element. |
| 184 | - if webvtt && trimmed.starts_with(|c: char| c.is_ascii_digit()) { | |
| 233 | + if dialect == Dialect::WebVtt && trimmed.starts_with(|c: char| c.is_ascii_digit()) { | |
| 185 | 234 | return match parse_clock(trimmed) { |
| 186 | 235 | Some(offset_us) => Tag::Timestamp(offset_us), |
| 187 | 236 | None => Tag::Text, |
| @@ -211,7 +260,7 @@ fn classify(tag: &str, webvtt: bool) -> Tag { | ||
| 211 | 260 | size: attribute(attrs, "size").and_then(|v| v.trim().parse().ok()), |
| 212 | 261 | annotation: String::new(), |
| 213 | 262 | }), |
| 214 | - "v" | "c" if webvtt => Tag::Open(Open { | |
| 263 | + "v" | "c" if dialect == Dialect::WebVtt => Tag::Open(Open { | |
| 215 | 264 | annotation: match name.as_str() { |
| 216 | 265 | "v" => attrs.trim().to_owned(), |
| 217 | 266 | _ => classes.replace('.', " "), |
| @@ -350,22 +399,17 @@ fn unescape(text: &str) -> String { | ||
| 350 | 399 | out |
| 351 | 400 | } |
| 352 | 401 | |
| 353 | -fn flush(text: &mut String, stack: &mut [(Option<Open>, Vec<Segment>)]) { | |
| 402 | +fn flush(text: &mut String, tree: &mut Tree) { | |
| 354 | 403 | if !text.is_empty() { |
| 355 | 404 | let taken = std::mem::take(text); |
| 356 | - // A newline inside a cue body is a line break, not whitespace. | |
| 357 | 405 | let mut lines = taken.split('\n'); |
| 358 | 406 | if let Some(first) = lines.next() |
| 359 | 407 | && !first.is_empty() |
| 360 | 408 | { |
| 361 | - stack | |
| 362 | - .last_mut() | |
| 363 | - .unwrap() | |
| 364 | - .1 | |
| 365 | - .push(Segment::Text(first.into())); | |
| 409 | + tree.top().push(Segment::Text(first.into())); | |
| 366 | 410 | } |
| 367 | 411 | for line in lines { |
| 368 | - let children = &mut stack.last_mut().unwrap().1; | |
| 412 | + let children = tree.top(); | |
| 369 | 413 | children.push(Segment::LineBreak); |
| 370 | 414 | if !line.is_empty() { |
| 371 | 415 | children.push(Segment::Text(line.into())); |
| @@ -374,48 +418,6 @@ fn flush(text: &mut String, stack: &mut [(Option<Open>, Vec<Segment>)]) { | ||
| 374 | 418 | } |
| 375 | 419 | } |
| 376 | 420 | |
| 377 | -fn push(stack: &mut [(Option<Open>, Vec<Segment>)], segment: Segment) { | |
| 378 | - stack.last_mut().unwrap().1.push(segment); | |
| 379 | -} | |
| 380 | - | |
| 381 | -fn close_top(stack: &mut Vec<(Option<Open>, Vec<Segment>)>) { | |
| 382 | - let Some((open, children)) = stack.pop() else { | |
| 383 | - return; | |
| 384 | - }; | |
| 385 | - let Some(open) = open else { return }; | |
| 386 | - let parent = &mut stack.last_mut().unwrap().1; | |
| 387 | - match open.name.as_str() { | |
| 388 | - "b" => parent.push(Segment::Bold(children)), | |
| 389 | - "i" => parent.push(Segment::Italic(children)), | |
| 390 | - "u" => parent.push(Segment::Underline(children)), | |
| 391 | - "s" => parent.push(Segment::Strike(children)), | |
| 392 | - "v" => parent.push(Segment::Voice { | |
| 393 | - name: open.annotation, | |
| 394 | - children, | |
| 395 | - }), | |
| 396 | - "c" => parent.push(Segment::Class { | |
| 397 | - name: open.annotation, | |
| 398 | - children, | |
| 399 | - }), | |
| 400 | - // `<font>`: whichever of colour and face/size it stated. One that | |
| 401 | - // stated neither is a frame with nothing to say, so it is unwrapped | |
| 402 | - // rather than kept as an empty node. | |
| 403 | - _ => { | |
| 404 | - if let Some(rgb) = open.color { | |
| 405 | - parent.push(Segment::Color { rgb, children }); | |
| 406 | - } else if open.family.is_some() || open.size.is_some() { | |
| 407 | - parent.push(Segment::Font { | |
| 408 | - family: open.family, | |
| 409 | - size: open.size, | |
| 410 | - children, | |
| 411 | - }); | |
| 412 | - } else { | |
| 413 | - parent.extend(children); | |
| 414 | - } | |
| 415 | - } | |
| 416 | - } | |
| 417 | -} | |
| 418 | - | |
| 419 | 421 | #[cfg(test)] |
| 420 | 422 | mod tests { |
| 421 | 423 | use super::*; |
| @@ -446,33 +448,33 @@ mod tests { | ||
| 446 | 448 | |
| 447 | 449 | #[test] |
| 448 | 450 | fn markup_nests_and_forgives() { |
| 449 | - let segments = parse_inline("<i>a</i> <b>b", false); | |
| 451 | + let segments = parse_inline("<i>a</i> <b>b", Dialect::Srt); | |
| 450 | 452 | assert_eq!(plain_text(&segments), "a b"); |
| 451 | 453 | assert!(matches!(segments[0], Segment::Italic(_))); |
| 452 | 454 | assert!(matches!(segments[2], Segment::Bold(_))); |
| 453 | 455 | // Mismatched close, stray close, and a `<` that is just a `<`. |
| 454 | - assert_eq!(plain_text(&parse_inline("<b><i>x</b>y", false)), "xy"); | |
| 455 | - assert_eq!(plain_text(&parse_inline("</i>plain", false)), "plain"); | |
| 456 | - assert_eq!(plain_text(&parse_inline("a < b", false)), "a < b"); | |
| 456 | + assert_eq!(plain_text(&parse_inline("<b><i>x</b>y", Dialect::Srt)), "xy"); | |
| 457 | + assert_eq!(plain_text(&parse_inline("</i>plain", Dialect::Srt)), "plain"); | |
| 458 | + assert_eq!(plain_text(&parse_inline("a < b", Dialect::Srt)), "a < b"); | |
| 457 | 459 | assert_eq!( |
| 458 | - plain_text(&parse_inline("2 < 3 & 4", false)), | |
| 460 | + plain_text(&parse_inline("2 < 3 & 4", Dialect::Srt)), | |
| 459 | 461 | "2 < 3 & 4" |
| 460 | 462 | ); |
| 461 | 463 | } |
| 462 | 464 | |
| 463 | 465 | #[test] |
| 464 | 466 | fn webvtt_only_tags_are_off_for_srt() { |
| 465 | - let vtt = parse_inline("<v Ann>hi</v>", true); | |
| 467 | + let vtt = parse_inline("<v Ann>hi</v>", Dialect::WebVtt); | |
| 466 | 468 | assert!(matches!(vtt[0], Segment::Voice { .. })); |
| 467 | 469 | assert_eq!(plain_text(&vtt), "hi"); |
| 468 | - let srt = parse_inline("<v Ann>hi", false); | |
| 470 | + let srt = parse_inline("<v Ann>hi", Dialect::Srt); | |
| 469 | 471 | assert_eq!(plain_text(&srt), "hi"); |
| 470 | 472 | assert!(matches!(srt[0], Segment::Raw(_))); |
| 471 | 473 | } |
| 472 | 474 | |
| 473 | 475 | #[test] |
| 474 | 476 | fn font_colour_is_read_by_hex_and_by_name() { |
| 475 | - let segments = parse_inline("<font color=\"#ff0000\">red</font>", false); | |
| 477 | + let segments = parse_inline("<font color=\"#ff0000\">red</font>", Dialect::Srt); | |
| 476 | 478 | assert!(matches!( |
| 477 | 479 | segments[0], |
| 478 | 480 | Segment::Color { |
| @@ -480,7 +482,7 @@ mod tests { | ||
| 480 | 482 | .. |
| 481 | 483 | } |
| 482 | 484 | )); |
| 483 | - let named = parse_inline("<font color=lime>green</font>", false); | |
| 485 | + let named = parse_inline("<font color=lime>green</font>", Dialect::Srt); | |
| 484 | 486 | assert!(matches!( |
| 485 | 487 | named[0], |
| 486 | 488 | Segment::Color { |
crates/ec-subs/src/srt.rsmodified+2 -2
| @@ -10,7 +10,7 @@ | ||
| 10 | 10 | use ec_core::{Error, Result}; |
| 11 | 11 | |
| 12 | 12 | use crate::ir::{SourceFormat, SubtitleCue, SubtitleTrack}; |
| 13 | -use crate::markup::{decode, parse_clock, parse_inline}; | |
| 13 | +use crate::markup::{decode, parse_clock, parse_inline, Dialect}; | |
| 14 | 14 | |
| 15 | 15 | /// Parse a SubRip document. |
| 16 | 16 | /// |
| @@ -40,7 +40,7 @@ pub fn parse(bytes: &[u8]) -> Result<SubtitleTrack> { | ||
| 40 | 40 | end_us: end_us.max(start_us), |
| 41 | 41 | style_ref: None, |
| 42 | 42 | positioning: None, |
| 43 | - segments: parse_inline(body.trim_end(), false), | |
| 43 | + segments: parse_inline(body.trim_end(), Dialect::Srt), | |
| 44 | 44 | }); |
| 45 | 45 | } |
| 46 | 46 | if track.cues.is_empty() && text.split_whitespace().next().is_some() { |
crates/ec-subs/src/webvtt.rsmodified+2 -2
| @@ -9,7 +9,7 @@ | ||
| 9 | 9 | use ec_core::{Error, Result}; |
| 10 | 10 | |
| 11 | 11 | use crate::ir::{CuePosition, SourceFormat, SubtitleCue, SubtitleTrack, TextAlign}; |
| 12 | -use crate::markup::{decode, parse_inline}; | |
| 12 | +use crate::markup::{decode, parse_inline, Dialect}; | |
| 13 | 13 | use crate::srt::timing; |
| 14 | 14 | |
| 15 | 15 | /// Parse a WebVTT document. |
| @@ -57,7 +57,7 @@ pub fn parse(bytes: &[u8]) -> Result<SubtitleTrack> { | ||
| 57 | 57 | end_us: end_us.max(start_us), |
| 58 | 58 | style_ref: None, |
| 59 | 59 | positioning: settings(lines[at]), |
| 60 | - segments: parse_inline(body.trim_end(), true), | |
| 60 | + segments: parse_inline(body.trim_end(), Dialect::WebVtt), | |
| 61 | 61 | }); |
| 62 | 62 | } |
| 63 | 63 | if track.cues.is_empty() && text.split_whitespace().next().is_some() { |