NSoiffer / MathCAT

MathCAT: Math Capable Assistive Technology for generating speech, braille, and navigation.
MIT License
54 stars 33 forks source link

Add Javascript interface for MathCAT #157

Open NSoiffer opened 1 year ago

NSoiffer commented 1 year ago

Currently, MathCATDemo uses a webassembly rust build and wraps stuff around that using whatever interface to it needs. It would be much better to make the interface explicit and then build the demo on that in Javascript.

This would parallel what is done for C, Python, etc.

This should be a relatively easy project. The basics are that for every MathCAT interface function, a Javascript callable function should be defined. This is done via

For example

pub fn set_mathml(mathml_str: String) -> Result<String> 

probably wants to have some code like:

#[wasm_bindgen]
pub fn setMathML(mathml: &str) -> Result<String> {
    return libmathcat::set_mathml(mathml);
}

Note that Javascript convention seems to be camelCase whereas rust convention is snake_case. Hence the exposed function is called setMathML and that is what Javascript should call.

Near the top of the file there needs to be

use wasm_bindgen::prelude::*;
use libmathcat::*;

From the little I've read, it seems that the Result<String> return is magically handled by wasm-bindgen and the JavaScript code and if there is an error, standard JS exception handling works.

A good reference is The wasm-bindgen Guide. Among other things, it mentions using how to use webpack to package it all up.

brichwin commented 2 weeks ago

I started a project as an effort to add a Javascript interface. The effort can be found at: https://github.com/brichwin/MathCATForWeb

Is there an existing MathCAT function or public structure that returns supported locales? Something like:

fn get_supported_locales() -> Vec<(&'static str, &'static str)>

I was searching for the equivalent for the MathJax SRE's window.MathJax._.a11y.sre.Sre.locales.forEach((language, isoCode)...

NSoiffer commented 2 weeks ago

There isn't one, although I could add it. It's a little less clear in MathCAT because there are spoken languages and braille languages. I suspect you want the spoken language. Even then it is a little tricky because (for example), there can be zh-tw and zh-cn. Although they share the same ISO code, they really are separate languages.

From the WASM build, because there is no exposed file system, you can't just look in Rules/Languages to find the dirs. At the moment, the best you can do is to build in a list of ISO codes and call is_dir_shim(path), where path is Rules/Languages/iso-code and see if it returns true or false. Not very efficient, but if you start with ISO code list of size less than 100, it probably won't be noticeably slower.

Note: in the MathCATForPython repo, you can find a list of ISO codes and their mapping to a language in addon\globalPlugins\MathCAT\MathCATPreferences.py. You can look through that list and easily throw out 2/3 of them as not likely to be implemented (e.g, "uz": "Ўзбек" Uzbek).

brichwin commented 2 weeks ago

Maybe this is a task for build.rs; The list not going to change once the wasm is built, so it could build a structure along the lines of: ...("zh-hans", "Chinese, Simplified (简体中文)"), ("zh-hant", "Chinese, Traditional (繁體中文)"), ("zh-tw", "Chinese, Traditional (Taiwan)"),...