Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/braille.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3079,7 +3079,7 @@ mod tests {
</mrow>
</math>";
crate::interface::set_rules_dir(super::super::abs_rules_dir_path()).unwrap();
set_mathml(mathml_str.to_string()).unwrap();
set_mathml(mathml_str).unwrap();
set_preference("BrailleCode", "UEB").unwrap();
set_preference("BrailleNavHighlight", "All").unwrap();
let braille = get_braille("id-2")?;
Expand Down Expand Up @@ -3135,7 +3135,7 @@ mod tests {
</mrow>
</math>";
crate::interface::set_rules_dir(super::super::abs_rules_dir_path()).unwrap();
set_mathml(mathml_str.to_string()).unwrap();
set_mathml(mathml_str).unwrap();
set_preference("BrailleNavHighlight", "Off").unwrap();

set_preference("BrailleCode", "Nemeth").unwrap();
Expand Down Expand Up @@ -3187,7 +3187,7 @@ mod tests {
fn test_UEB_start_mode() -> Result<()> {
let mathml_str = "<math><msup><mi>x</mi><mi>n</mi></msup></math>";
crate::interface::set_rules_dir(super::super::abs_rules_dir_path()).unwrap();
set_mathml(mathml_str.to_string()).unwrap();
set_mathml(mathml_str).unwrap();
set_preference("BrailleCode", "UEB").unwrap();
set_preference("UEB_START_MODE", "Grade2").unwrap();
let braille = get_braille("")?;
Expand Down
34 changes: 16 additions & 18 deletions src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,14 @@ fn init_mathml_instance() -> RefCell<Package> {

/// Set the Rules directory
/// IMPORTANT: this should be the very first call to MathCAT. If 'dir' is an empty string, the environment var 'MathCATRulesDir' is tried.
pub fn set_rules_dir(dir: String) -> Result<()> {
pub fn set_rules_dir(dir: impl AsRef<str>) -> Result<()> {
enable_logs();
use std::path::PathBuf;
let dir = dir.as_ref();
let dir = if dir.is_empty() {
std::env::var_os("MathCATRulesDir")
.unwrap_or_default()
.to_str()
.unwrap()
.to_string()
std::env::var_os("MathCATRulesDir").unwrap_or_default()
} else {
dir
std::ffi::OsString::from(dir)
};
let pref_manager = crate::prefs::PreferenceManager::get();
return pref_manager.borrow_mut().initialize(PathBuf::from(dir));
Expand All @@ -92,7 +89,7 @@ pub fn get_version() -> String {
/// This will override any previous MathML that was set.
/// This returns canonical MathML with 'id's set on any node that doesn't have an id.
/// The ids can be used for sync highlighting if the `Bookmark` API preference is true.
pub fn set_mathml(mathml_str: String) -> Result<String> {
pub fn set_mathml(mathml_str: impl AsRef<str>) -> Result<String> {
enable_logs();
lazy_static! {
// if these are present when resent to MathJaX, MathJaX crashes (https://github.com/mathjax/MathJax/issues/2822)
Expand All @@ -111,6 +108,7 @@ pub fn set_mathml(mathml_str: String) -> Result<String> {
// This call reads all of them for the current preferences, but that's ok since they will likely be used
crate::speech::SPEECH_RULES.with(|rules| rules.borrow_mut().read_files())?;

let mathml_str = mathml_str.as_ref();
return MATHML_INSTANCE.with(|old_package| {
static HTML_ENTITIES_MAPPING: phf::Map<&str, &str> = include!("entities.in");

Expand Down Expand Up @@ -535,10 +533,10 @@ pub fn get_supported_languages() -> Vec<String> {
return language_paths;
}

pub fn get_supported_speech_styles(lang: String) -> Vec<String> {
pub fn get_supported_speech_styles(lang: impl AsRef<str>) -> Vec<String> {
enable_logs();
let rules_dir = crate::prefs::PreferenceManager::get().borrow().get_rules_dir();
let lang_dir = rules_dir.join("Languages").join(lang);
let lang_dir = rules_dir.join("Languages").join(lang.as_ref());
let mut speech_styles = find_files_in_dir_that_ends_with_shim(&lang_dir, "_Rules.yaml");
for file_name in &mut speech_styles {
file_name.truncate(file_name.len() - "_Rules.yaml".len())
Expand Down Expand Up @@ -1103,9 +1101,9 @@ mod tests {
// this forces initialization
set_rules_dir(super::super::abs_rules_dir_path()).unwrap();

let entity_str = set_mathml("<math><mrow><mo>&minus;</mo><mi>&mopf;</mi></mrow></math>".to_string()).unwrap();
let entity_str = set_mathml("<math><mrow><mo>&minus;</mo><mi>&mopf;</mi></mrow></math>").unwrap();
let converted_str =
set_mathml("<math><mrow><mo>&#x02212;</mo><mi>&#x1D55E;</mi></mrow></math>".to_string()).unwrap();
set_mathml("<math><mrow><mo>&#x02212;</mo><mi>&#x1D55E;</mi></mrow></math>").unwrap();

// need to remove unique ids
lazy_static! {
Expand All @@ -1116,19 +1114,19 @@ mod tests {
assert_eq!(entity_str, converted_str, "normal entity test failed");

let entity_str = set_mathml(
"<math data-quot=\"&quot;value&quot;\" data-apos='&apos;value&apos;'><mi>XXX</mi></math>".to_string(),
"<math data-quot=\"&quot;value&quot;\" data-apos='&apos;value&apos;'><mi>XXX</mi></math>",
)
.unwrap();
let converted_str =
set_mathml("<math data-quot='\"value\"' data-apos=\"'value'\"><mi>XXX</mi></math>".to_string()).unwrap();
set_mathml("<math data-quot='\"value\"' data-apos=\"'value'\"><mi>XXX</mi></math>").unwrap();
let entity_str = ID_MATCH.replace_all(&entity_str, "");
let converted_str = ID_MATCH.replace_all(&converted_str, "");
assert_eq!(entity_str, converted_str, "special entities quote test failed");

let entity_str =
set_mathml("<math><mo>&lt;</mo><mo>&gt;</mo><mtext>&amp;lt;</mtext></math>".to_string()).unwrap();
set_mathml("<math><mo>&lt;</mo><mo>&gt;</mo><mtext>&amp;lt;</mtext></math>").unwrap();
let converted_str =
set_mathml("<math><mo>&#x003C;</mo><mo>&#x003E;</mo><mtext>&#x0026;lt;</mtext></math>".to_string())
set_mathml("<math><mo>&#x003C;</mo><mo>&#x003E;</mo><mtext>&#x0026;lt;</mtext></math>")
.unwrap();
let entity_str = ID_MATCH.replace_all(&entity_str, "");
let converted_str = ID_MATCH.replace_all(&converted_str, "");
Expand All @@ -1140,13 +1138,13 @@ mod tests {
use std::env;
// MathCAT will check the env var "MathCATRulesDir" as an override, so the following test might succeed if we don't override the env var
env::set_var("MathCATRulesDir", "MathCATRulesDir");
assert!(set_rules_dir("someInvalidRulesDir".to_string()).is_err());
assert!(set_rules_dir("someInvalidRulesDir").is_err());
assert!(
set_rules_dir(super::super::abs_rules_dir_path()).is_ok(),
"\nset_rules_dir to '{}' failed",
super::super::abs_rules_dir_path()
);
assert!(set_mathml("<math><mn>1</mn></math>".to_string()).is_ok());
assert!(set_mathml("<math><mn>1</mn></math>").is_ok());
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,11 @@ fn main() {
set_preference("Bookmark", "false").unwrap();
set_preference("SpeechStyle", "ClearSpeak").unwrap();
info!("Languages: {}", libmathcat::interface::get_supported_languages().join(", "));
info!("Speech styles: {}", libmathcat::interface::get_supported_speech_styles("ClearSpeak".to_string()).join(", "));
info!("Speech styles: {}", libmathcat::interface::get_supported_speech_styles("ClearSpeak").join(", "));
info!("BrailleCodes: {}", libmathcat::interface::get_supported_braille_codes().join(", "));
// set_preference("DecimalSeparators", ",").unwrap();
// set_preference("BlockSeparators", ". ").unwrap();
if let Err(e) = set_mathml(expr.to_string()) {
if let Err(e) = set_mathml(expr) {
panic!("Error: exiting -- {}", errors_to_string(&e));
};

Expand Down Expand Up @@ -308,7 +308,7 @@ fn timing_test(expr: &str, n_loops: usize) {
let n_loops_float = n_loops as f64;
let instant = Instant::now();
for _ in 0..n_loops {
if let Err(e) = set_mathml(expr.to_string()) {
if let Err(e) = set_mathml(expr) {
panic!("Error: exiting -- {}", errors_to_string(&e));
};
match get_spoken_text() {
Expand All @@ -324,7 +324,7 @@ fn timing_test(expr: &str, n_loops: usize) {

let instant = Instant::now();
for _ in 0..n_loops {
if let Err(e) = set_mathml(expr.to_string()) {
if let Err(e) = set_mathml(expr) {
panic!("Error: exiting -- {}", errors_to_string(&e));
};
}
Expand All @@ -350,7 +350,7 @@ fn timing_test(expr: &str, n_loops: usize) {
}
info!("Time taken (time for {} braille averaged over {} loops): {}ms", get_preference("BrailleCode").unwrap(), n_loops, instant.elapsed().as_millis() as f64/n_loops_float);

if let Err(e) = set_mathml(expr.to_string()) {
if let Err(e) = set_mathml(expr) {
panic!("Error: exiting -- {}", errors_to_string(&e));
};
set_preference("BrailleCode", "Nemeth").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/navigate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ mod tests {
set_preference("SpeechStyle", "SimpleSpeak").unwrap();
set_preference("Verbosity", "Medium").unwrap();
set_preference("Overview", "False").unwrap();
set_mathml(mathml.to_string()).unwrap();
set_mathml(mathml).unwrap();
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion src/prefs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,7 +1165,7 @@ cfg_if::cfg_if! {if #[cfg(not(feature = "include-zip"))] {
assert_eq!(&pref_manager.pref_to_string("SpeechStyle"), "ClearSpeak");
assert_eq!(rel_path(&pref_manager.rules_dir, pref_manager.speech.as_path()), PathBuf::from("Languages/zz/ClearSpeak_Rules.yaml"));
});
interface::set_mathml("<math><mo>+</mo><mn>10</mn></math>".to_string()).unwrap();
interface::set_mathml("<math><mo>+</mo><mn>10</mn></math>").unwrap();
assert_eq!(interface::get_spoken_text().unwrap(), "ClearSpeak positive from zz 10");

let mut file_path = PathBuf::default();
Expand Down
2 changes: 1 addition & 1 deletion src/shim_filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ cfg_if! {
// file_name should be path name starting at Rules dir: e.g, "Rules/en/navigate.yaml"
OVERRIDE_FILE_NAME.with(|name| *name.borrow_mut() = file_name.to_string().replace("/", "\\"));
OVERRIDE_FILE_CONTENTS.with(|contents| *contents.borrow_mut() = file_contents.to_string());
crate::interface::set_rules_dir("Rules".to_string()).unwrap(); // force reinitialization after the change
crate::interface::set_rules_dir("Rules").unwrap(); // force reinitialization after the change
}
} else {
pub fn is_file_shim(path: &Path) -> bool {
Expand Down
2 changes: 1 addition & 1 deletion src/speech.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2887,7 +2887,7 @@ cfg_if::cfg_if! {if #[cfg(not(feature = "include-zip"))] {
set_rules_dir(super::super::abs_rules_dir_path()).unwrap();
set_preference("Language", "zz-aa").unwrap();
// not much is support in zz
if let Err(e) = set_mathml("<math><mi>x</mi></math>".to_string()) {
if let Err(e) = set_mathml("<math><mi>x</mi></math>") {
error!("{}", crate::errors_to_string(&e));
panic!("Should not be an error in setting MathML")
}
Expand Down
2 changes: 1 addition & 1 deletion tests/braille/Nemeth/other.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn find_baseline_indicator_bug_364() {
set_rules_dir(abs_rules_dir_path()).unwrap();
set_preference("BrailleNavHighlight", "Off").unwrap();
set_preference("BrailleCode", "Nemeth").unwrap();
if let Err(e) = set_mathml(expr.to_string()) {
if let Err(e) = set_mathml(expr) {
panic!("{}", errors_to_string(&e));
};
match get_navigation_node_from_braille_position(4) {
Expand Down
8 changes: 4 additions & 4 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn strip_spaces(str: &str) -> String {

#[allow(dead_code)] // used in testing
fn check_answer(test: &str, target: &str, failure_message: &str) {
if let Err(e) = set_mathml(test.to_string()) {
if let Err(e) = set_mathml(test) {
panic!("{}", errors_to_string(&e));
};
match get_spoken_text() {
Expand Down Expand Up @@ -123,7 +123,7 @@ pub fn test_braille(code: &str, mathml: &str, braille: &str) {
"CMU" => set_preference("Language", "es").unwrap(),
"UEB" | "Nemeth" | _ => set_preference("Language", "en").unwrap(),
}
if let Err(e) = set_mathml(mathml.to_string()) {
if let Err(e) = set_mathml(mathml) {
panic!("{}", errors_to_string(&e));
};
match get_braille("") {
Expand Down Expand Up @@ -151,7 +151,7 @@ pub fn test_braille_prefs(code: &str, test_prefs: Vec<(&str, &str)>, mathml: &st
set_preference(pref_name, pref_value).unwrap();
};

if let Err(e) = set_mathml(mathml.to_string()) {
if let Err(e) = set_mathml(mathml) {
panic!("{}", errors_to_string(&e));
};
match get_braille("") {
Expand Down Expand Up @@ -240,7 +240,7 @@ pub fn test_from_braille(code: &str, mathml: &str, braille: &str) {
"CMU" => set_preference("Language", "es").unwrap(),
"UEB" | "Nemeth" | _ => set_preference("Language", "en").unwrap(),
}
if let Err(e) = set_mathml(mathml.to_string()) {
if let Err(e) = set_mathml(mathml) {
panic!("{}", errors_to_string(&e));
};

Expand Down
Loading