use quick_xml::{
Reader,
events::{
BytesStart,
Event,
},
};
use crate::{
constants::{
attrs,
tags,
},
error::{
ParseErrorKind,
Result,
ResultExt as _,
TTMLProcessorError,
},
model::{
BackgroundVocal,
LyricLine,
},
parser::{
ext::{
BytesStartExt as _,
QNameExt as _,
ReaderExt as _,
},
span::process_span,
state::ParserContext,
utils::{
build_full_text,
is_spacing_text,
mark_last_syllable_space,
normalize_line_text,
normalize_words_spaces,
resolve_xml_entity,
strip_outer_parens,
strip_outer_parens_from_words,
},
},
};
pub fn parse_body(
reader: &mut Reader<&[u8]>,
context: &mut ParserContext,
lines: &mut Vec<LyricLine>,
) -> Result<()> {
let mut buf = Vec::new();
loop {
match reader.read_event_with_context(&mut buf, context)? {
Event::Start(ref e) => {
let qname = e.name();
let tag_name =
std::str::from_utf8(qname.as_ref()).map_err(TTMLProcessorError::Utf8Error)?;
context.tag_stack.push(tag_name.to_string());
if qname.is(tags::DIV) {
parse_section(reader, e, context, lines)?;
context.tag_stack.pop();
}
}
Event::End(ref e) => {
if e.name().is(tags::BODY) {
break;
}
context.tag_stack.pop();
}
Event::Eof => return Err(ParseErrorKind::UnexpectedEof).with_context(reader, context),
_ => (),
}
buf.clear();
}
Ok(())
}
fn parse_section(
reader: &mut Reader<&[u8]>,
div_event: &BytesStart,
context: &mut ParserContext,
lines: &mut Vec<LyricLine>,
) -> Result<()> {
let mut current_song_part =
div_event.get_attr_value(attrs::ITUNES_SONGPART, reader, context)?;
if current_song_part.is_none() {
current_song_part =
div_event.get_attr_value(attrs::ITUNES_SONGPART_KEBAB, reader, context)?;
}
context.current_block_index += 1;
if context.last_song_part != current_song_part {
context.last_song_part = current_song_part;
}
let mut buf = Vec::new();
loop {
match reader.read_event_with_context(&mut buf, context)? {
Event::Start(ref e) => {
let qname = e.name();
let tag_name =
std::str::from_utf8(qname.as_ref()).map_err(TTMLProcessorError::Utf8Error)?;
context.tag_stack.push(tag_name.to_string());
if qname.is(tags::P) {
if let Some(line) = parse_line(reader, e, context)? {
lines.push(line);
}
context.tag_stack.pop();
}
}
Event::End(ref e) => {
if e.name().is(tags::DIV) {
break;
}
context.tag_stack.pop();
}
Event::Eof => return Err(ParseErrorKind::UnexpectedEof).with_context(reader, context),
_ => (),
}
buf.clear();
}
Ok(())
}
fn parse_line(
reader: &mut Reader<&[u8]>,
p_event: &BytesStart,
context: &mut ParserContext,
) -> Result<Option<LyricLine>> {
let line_id = p_event.get_attr_value(attrs::ITUNES_KEY, reader, context)?;
context.current_line_id.clone_from(&line_id);
let start_time = p_event.get_required_timestamp_attr(attrs::BEGIN, reader, context)?;
let end_time = p_event.get_required_timestamp_attr(attrs::END, reader, context)?;
let mut line = LyricLine {
id: line_id.clone(),
agent_id: p_event.get_attr_value(attrs::TTM_AGENT, reader, context)?,
song_part: context.last_song_part.clone(),
block_index: Some(context.current_block_index),
start_time,
end_time,
..Default::default()
};
if let Some(id) = &line.id {
line.translations = context.translations_map.remove(id);
line.romanizations = context.romanizations_map.remove(id);
let bg_trans = context.bg_translations_map.remove(id);
let bg_rom = context.bg_romanizations_map.remove(id);
if bg_trans.is_some() || bg_rom.is_some() {
line.background_vocal = Some(BackgroundVocal {
translations: bg_trans,
romanizations: bg_rom,
..Default::default()
});
}
}
let mut raw_text = String::new();
let mut buf = Vec::new();
loop {
match reader.read_event_with_context(&mut buf, context)? {
Event::Start(ref e) => {
let qname = e.name();
let tag_name =
std::str::from_utf8(qname.as_ref()).map_err(TTMLProcessorError::Utf8Error)?;
context.tag_stack.push(tag_name.to_string());
if qname.is(tags::SPAN) {
process_span(reader, context, e, &mut line, false)?;
context.tag_stack.pop();
}
}
Event::Text(e) => {
let text =
std::str::from_utf8(e.as_ref()).map_err(TTMLProcessorError::Utf8Error)?;
if is_spacing_text(text) {
mark_last_syllable_space(&mut line, false);
}
raw_text.push_str(text);
}
Event::GeneralRef(reference) => {
raw_text.push_str(&resolve_xml_entity(&reference).with_context(reader, context)?);
}
Event::End(ref e) => {
if e.name().is(tags::P) {
break;
}
context.tag_stack.pop();
}
Event::Eof => return Err(ParseErrorKind::UnexpectedEof).with_context(reader, context),
_ => (),
}
buf.clear();
}
post_process_line(&mut line, raw_text);
context.current_line_id = None;
Ok(Some(line))
}
fn post_process_line(line: &mut LyricLine, raw_line_text: String) {
if let Some(words) = &mut line.words
&& let Some(last) = words.last_mut()
{
last.ends_with_space = None;
}
if let Some(bg) = &mut line.background_vocal
&& let Some(bg_words) = &mut bg.words
&& let Some(last) = bg_words.last_mut()
{
last.ends_with_space = None;
}
match &mut line.words {
Some(words) if !words.is_empty() => {
normalize_words_spaces(words);
line.text = build_full_text(words, false);
}
_ => {
line.words = None;
line.text = raw_line_text;
normalize_line_text(&mut line.text);
}
}
if let Some(translations) = &mut line.translations {
for t in translations {
if let Some(t_words) = &mut t.words {
normalize_words_spaces(t_words);
t.text = build_full_text(t_words, false);
} else {
normalize_line_text(&mut t.text);
}
}
}
if let Some(romanizations) = &mut line.romanizations {
for r in romanizations {
if let Some(r_words) = &mut r.words {
normalize_words_spaces(r_words);
r.text = build_full_text(r_words, true);
} else {
normalize_line_text(&mut r.text);
}
}
}
if let Some(bg) = &mut line.background_vocal {
if let Some(bg_words) = &mut bg.words {
strip_outer_parens_from_words(bg_words);
normalize_words_spaces(bg_words);
bg.text = build_full_text(bg_words, false);
} else {
strip_outer_parens(&mut bg.text);
normalize_line_text(&mut bg.text);
}
if let Some(translations) = &mut bg.translations {
for t in translations {
if let Some(t_words) = &mut t.words {
strip_outer_parens_from_words(t_words);
normalize_words_spaces(t_words);
t.text = build_full_text(t_words, false);
} else {
strip_outer_parens(&mut t.text);
normalize_line_text(&mut t.text);
}
}
}
if let Some(romanizations) = &mut bg.romanizations {
for r in romanizations {
if let Some(r_words) = &mut r.words {
strip_outer_parens_from_words(r_words);
normalize_words_spaces(r_words);
r.text = build_full_text(r_words, true);
} else {
strip_outer_parens(&mut r.text);
normalize_line_text(&mut r.text);
}
}
}
}
}
#[cfg(test)]
mod tests {
use quick_xml::{
Reader,
events::Event,
};
use super::*;
use crate::model::{
BackgroundVocal,
SubLyricContent,
Syllable,
};
fn advance_to_start_tag(reader: &mut Reader<&[u8]>, tag_name: &str) {
let mut buf = Vec::new();
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Start(ref e)) if e.name().is(tag_name) => break,
Ok(Event::Eof) => panic!("Reached EOF before finding tag: {tag_name}"),
_ => (),
}
buf.clear();
}
}
fn get_start_event<'a>(reader: &mut Reader<&'a [u8]>, tag: &str) -> BytesStart<'a> {
let mut buf = Vec::new();
loop {
match reader
.read_event_into(&mut buf)
.expect("Failed to read XML")
{
Event::Start(e) if e.name().as_ref() == tag.as_bytes() => return e.into_owned(),
Event::Eof => panic!("Reached EOF before finding start tag"),
_ => (),
}
}
}
#[test]
fn test_post_process_line() {
let mut line = LyricLine {
start_time: 1000,
end_time: 2000,
background_vocal: Some(BackgroundVocal {
words: Some(vec![
Syllable {
text: "(Ah".to_string(),
ends_with_space: Some(true),
..Default::default()
},
Syllable {
text: "ha)".to_string(),
..Default::default()
},
]),
..Default::default()
}),
..Default::default()
};
post_process_line(&mut line, " raw line text ".to_string());
assert_eq!(line.text, "raw line text");
assert_eq!(line.start_time, 1000);
assert_eq!(line.end_time, 2000);
let bg = line.background_vocal.as_ref().unwrap();
assert_eq!(bg.text, "Ah ha");
let bg_words = bg.words.as_ref().unwrap();
assert_eq!(bg_words[0].text, "Ah");
assert_eq!(bg_words[1].text, "ha");
}
#[test]
fn test_parse_line() {
let xml = r#"<p begin="10.522" end="13.518" itunes:key="L3" ttm:agent="v1">
<span begin="10.522" end="11.000">Hello</span>
</p>"#;
let mut reader = Reader::from_str(xml);
let start_event = get_start_event(&mut reader, "p");
let mut context = ParserContext::default();
context.translations_map.insert(
"L3".to_string(),
vec![SubLyricContent {
language: Some("zh".to_string()),
text: "你好".to_string(),
words: None,
}],
);
context.last_song_part = Some("Verse".to_string());
context.current_block_index = 1;
let line = parse_line(&mut reader, &start_event, &mut context)
.expect("Failed to parse line")
.unwrap();
assert_eq!(line.id.as_deref(), Some("L3"));
assert_eq!(line.agent_id.as_deref(), Some("v1"));
assert_eq!(line.song_part.as_deref(), Some("Verse"));
assert_eq!(line.block_index, Some(1));
assert_eq!(line.start_time, 10522);
assert_eq!(line.end_time, 13518);
assert_eq!(line.text, "Hello");
assert_eq!(line.words.unwrap().len(), 1);
let translations = line.translations.unwrap();
assert_eq!(translations.len(), 1);
assert_eq!(translations[0].text, "你好");
assert!(context.translations_map.is_empty());
}
#[test]
fn test_parse_section() {
let xml = r#"<div itunes:songPart="Chorus">
<p begin="0.0" end="1.0" itunes:key="L1">Line 1</p>
<p begin="1.0" end="2.0" itunes:key="L2">Line 2</p>
</div>"#;
let mut reader = Reader::from_str(xml);
let start_event = get_start_event(&mut reader, "div");
let mut context = ParserContext::default();
let mut lines = Vec::new();
parse_section(&mut reader, &start_event, &mut context, &mut lines)
.expect("Failed to parse section");
assert_eq!(context.last_song_part.as_deref(), Some("Chorus"));
assert_eq!(context.current_block_index, 1);
assert_eq!(lines.len(), 2);
assert_eq!(lines[0].id.as_deref(), Some("L1"));
assert_eq!(lines[0].song_part.as_deref(), Some("Chorus"));
assert_eq!(lines[0].block_index, Some(1));
assert_eq!(lines[1].id.as_deref(), Some("L2"));
assert_eq!(lines[1].song_part.as_deref(), Some("Chorus"));
assert_eq!(lines[1].block_index, Some(1));
}
#[test]
fn test_parse_body() {
let xml = r#"<body>
<div itunes:songPart="Verse">
<p begin="0.0" end="1.0">V1</p>
</div>
<div itunes:songPart="Chorus">
<p begin="1.0" end="2.0">C1</p>
</div>
</body>"#;
let mut reader = Reader::from_str(xml);
advance_to_start_tag(&mut reader, "body");
let mut context = ParserContext::default();
let mut lines = Vec::new();
parse_body(&mut reader, &mut context, &mut lines).expect("Failed to parse body");
assert_eq!(lines.len(), 2);
assert_eq!(lines[0].song_part.as_deref(), Some("Verse"));
assert_eq!(lines[0].block_index, Some(1));
assert_eq!(lines[1].song_part.as_deref(), Some("Chorus"));
assert_eq!(lines[1].block_index, Some(2));
assert_eq!(context.current_block_index, 2);
}
}