From 56c11921977757dded5c80b3988f1d4d6d0d348b Mon Sep 17 00:00:00 2001 From: Sam Nystrom Date: Fri, 23 Jun 2023 17:38:15 -0400 Subject: Properly handle nested formatting tags --- main.ha | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'main.ha') diff --git a/main.ha b/main.ha index a056a4d..5ad650b 100644 --- a/main.ha +++ b/main.ha @@ -202,12 +202,14 @@ type state = enum { TEXT, }; +def XML_PROLOG: str = "\n\n"; + fn parse_srt(file: io::handle) []subtitle = { let subtitles: []subtitle = []; let current = subtitle { ... }; let content = strio::dynamic(); defer io::close(&content)!; - strio::concat(&content, "\n\n")!; + strio::concat(&content, XML_PROLOG)!; let state = state::INDEX; for (let nr = 0; true; nr += 1) { @@ -255,7 +257,7 @@ fn parse_srt(file: io::handle) []subtitle = { case let t: []text => yield t; case => - let content = strings::trimprefix(content_str, "\n"); + let content = strings::trimprefix(content_str, XML_PROLOG); let content = strings::trimsuffix(content, ""); yield alloc([strings::replace(content, "\n", "\r\n")]); }; @@ -264,7 +266,7 @@ fn parse_srt(file: io::handle) []subtitle = { current = subtitle { ... }; strio::reset(&content); - strio::concat(&content, "\n\n")!; + strio::concat(&content, XML_PROLOG)!; state = state::INDEX; }; @@ -299,9 +301,9 @@ fn parse_text(in: io::handle) ([]text | io::error | xml::error) = { defer xml::parser_free(parser); let text: []text = []; - let bold = false; - let italic = false; - let underline = false; + let bold = 0; + let italic = 0; + let underline = 0; for (true) { let tok = match (xml::scan(parser)?) { case let tok: xml::token => @@ -314,39 +316,39 @@ fn parse_text(in: io::handle) ([]text | io::error | xml::error) = { case let start: xml::elementstart => switch (start) { case "b" => - if (!bold) { + if (bold == 0) { append(text, true: setbold); - bold = true; }; + bold += 1; case "i" => - if (!italic) { + if (italic == 0) { append(text, true: setitalic); - italic = true; }; + italic += 1; case "u" => - if (!underline) { + if (underline == 0) { append(text, true: setunderline); - underline = true; }; + underline += 1; case => void; }; case let end: xml::elementend => switch (end) { case "b" => - if (bold) { + if (bold == 1) { append(text, false: setbold); - bold = false; }; + bold -= 1; case "i" => - if (italic) { + if (italic == 1) { append(text, false: setitalic); - italic = false; }; + italic -= 1; case "u" => - if (underline) { + if (underline == 1) { append(text, false: setunderline); - underline = false; }; + underline -= 1; case => void; }; case xml::attribute => void; -- cgit v1.2.3