From f1190ab15f7f0d9e455d8dcabaae1e3ffdaf88dc Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 13 Apr 2024 01:24:37 -0500 Subject: [PATCH] reorder currency symbols in numbat responses --- src/engines/answer/numbat.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/engines/answer/numbat.rs b/src/engines/answer/numbat.rs index e7cffc4..4af9f36 100644 --- a/src/engines/answer/numbat.rs +++ b/src/engines/answer/numbat.rs @@ -105,6 +105,7 @@ fn interpret(query: &str) -> Option<(Statement, Markup)> { if res_markup.to_string().trim() == query { return None; } + let res_markup = fix_markup(res_markup); Some((statements.into_iter().last()?, res_markup)) } @@ -123,7 +124,7 @@ pub struct NumbatResponse { fn evaluate(query: &str) -> Option { let (statement, res_markup) = interpret(query)?; - let statement_markup = statement.pretty_print(); + let statement_markup = fix_markup(statement.pretty_print()); let query_html = markup_to_html(statement_markup); let result_html = markup_to_html(res_markup); @@ -133,9 +134,28 @@ fn evaluate(query: &str) -> Option { }) } +fn fix_markup(markup: Markup) -> Markup { + let mut reordered_markup: Vec = Vec::new(); + const LEFT_SIDE_UNITS: &[&str] = &["$", "€", "£", "¥"]; + for s in markup.0 { + let FormattedString(_output_type, format_type, content) = s.clone(); + + if format_type == FormatType::Unit && LEFT_SIDE_UNITS.contains(&content.as_str()) { + // remove the last markup if it's whitespace + if let Some(FormattedString(_, FormatType::Whitespace, _)) = reordered_markup.last() { + reordered_markup.pop(); + } + reordered_markup.insert(reordered_markup.len() - 1, s); + } else { + reordered_markup.push(s); + } + } + Markup(reordered_markup) +} + fn markup_to_html(markup: Markup) -> String { let mut html = String::new(); - for FormattedString(_output_type, format_type, content) in markup.0 { + for FormattedString(_, format_type, content) in markup.0 { let class = match format_type { FormatType::Value => "answer-calc-constant", FormatType::String => "answer-calc-string", @@ -170,7 +190,7 @@ pub static NUMBAT_CTX: Lazy = Lazy::new(|| { ("tb", "TB"), ("pb", "PB"), ] { - let _ = ctx.interpret(&format!("unit {alias} = {canonical}"), CodeSource::Internal); + let _ = ctx.interpret(&format!("let {alias} = {canonical}"), CodeSource::Internal); } // lowercase aliases (so for example usd and USD are the same unit) @@ -186,7 +206,7 @@ pub static NUMBAT_CTX: Lazy = Lazy::new(|| { // add every lowercase aliases for every unit as long as that alias isn't // already taken if !unit_names.contains(&name_lower) { - let _ = ctx.interpret(&format!("unit {name_lower} = {name}"), CodeSource::Internal); + let _ = ctx.interpret(&format!("let {name_lower} = {name}"), CodeSource::Internal); } }