From a334ae5457bce2f3a9b15ef02c4d4998cfcfa457 Mon Sep 17 00:00:00 2001 From: Sam Nystrom Date: Fri, 23 Jun 2023 17:30:26 -0400 Subject: Fall back to plain text if XML parsing fails --- main.ha | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/main.ha b/main.ha index f66c67f..1782cc5 100644 --- a/main.ha +++ b/main.ha @@ -203,7 +203,7 @@ fn parse_srt(file: io::handle) []subtitle = { let current = subtitle { ... }; let content = strio::dynamic(); defer io::close(&content)!; - fmt::fprint(&content, "\n\n")!; + strio::concat(&content, "\n\n")!; let state = state::INDEX; for (let nr = 0; true; nr += 1) { @@ -240,29 +240,27 @@ fn parse_srt(file: io::handle) []subtitle = { state = state::TEXT; case state::TEXT => if (len(line) > 0) { - fmt::fprintf(&content, "{}\n", line)!; + strio::concat(&content, line, "\n")!; continue; }; - fmt::fprint(&content, "")!; - let buf = bufio::fixed( - strings::toutf8(strio::string(&content)), - io::mode::READ, - ); + strio::concat(&content, "")!; + let content_str = strio::string(&content); + let buf = bufio::fixed(strings::toutf8(content_str), io::mode::READ); current.text = match (parse_text(&buf)) { case let t: []text => yield t; - case let err: io::error => - fmt::fatal("Error parsing subtitles:", io::strerror(err)); - case let err: xml::error => - fmt::fatal("Error parsing subtitles:", xml::strerror(err)); + case => + let content = strings::trimprefix(content_str, "\n"); + let content = strings::trimsuffix(content, ""); + yield alloc([strings::replace(content, "\n", "\r\n")]); }; append(subtitles, current); current = subtitle { ... }; strio::reset(&content); - fmt::fprint(&content, "\n\n")!; + strio::concat(&content, "\n\n")!; state = state::INDEX; }; -- cgit v1.2.3