pub mod asciimath;
pub mod ast;
pub mod latex;
pub mod mathml;
pub mod parser;
pub mod symbols;
pub use asciimath::AsciiMathGenerator;
pub use ast::{BinaryOp, BracketType, LargeOpType, MathExpr, MathNode, MathVisitor, UnaryOp};
pub use latex::{LaTeXConfig, LaTeXGenerator};
pub use mathml::MathMLGenerator;
pub use parser::{parse_expression, Parser};
pub use symbols::{get_symbol, unicode_to_latex, MathSymbol, SymbolCategory};
pub fn parse(input: &str) -> Result<MathExpr, String> {
parse_expression(input)
}
pub fn to_latex(expr: &MathExpr) -> String {
LaTeXGenerator::new().generate(expr)
}
pub fn to_latex_with_config(expr: &MathExpr, config: LaTeXConfig) -> String {
LaTeXGenerator::with_config(config).generate(expr)
}
pub fn to_mathml(expr: &MathExpr) -> String {
MathMLGenerator::new().generate(expr)
}
pub fn to_asciimath(expr: &MathExpr) -> String {
AsciiMathGenerator::new().generate(expr)
}
pub fn to_asciimath_ascii_only(expr: &MathExpr) -> String {
AsciiMathGenerator::ascii_only().generate(expr)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_and_convert() {
let expr = parse("1 + 2").unwrap();
let latex = to_latex(&expr);
assert!(latex.contains("+"));
}
#[test]
fn test_fraction_conversion() {
let expr = parse("\\frac{1}{2}").unwrap();
let latex = to_latex(&expr);
assert!(latex.contains("\\frac"));
let mathml = to_mathml(&expr);
assert!(mathml.contains("<mfrac>"));
let asciimath = to_asciimath(&expr);
assert!(asciimath.contains("/"));
}
#[test]
fn test_sqrt_conversion() {
let expr = parse("\\sqrt{2}").unwrap();
let latex = to_latex(&expr);
assert!(latex.contains("\\sqrt"));
let mathml = to_mathml(&expr);
assert!(mathml.contains("<msqrt>"));
let asciimath = to_asciimath(&expr);
assert!(asciimath.contains("sqrt"));
}
#[test]
fn test_complex_expression() {
let expr = parse("\\frac{-b + \\sqrt{b^2 - 4*a*c}}{2*a}").unwrap();
let latex = to_latex(&expr);
assert!(latex.contains("\\frac"));
assert!(latex.contains("\\sqrt"));
let mathml = to_mathml(&expr);
assert!(mathml.contains("<mfrac>"));
assert!(mathml.contains("<msqrt>"));
}
#[test]
fn test_symbol_lookup() {
assert!(unicode_to_latex('α').is_some());
assert_eq!(unicode_to_latex('α'), Some("alpha"));
assert_eq!(unicode_to_latex('π'), Some("pi"));
assert_eq!(unicode_to_latex('∑'), Some("sum"));
}
#[test]
fn test_get_symbol() {
let sym = get_symbol('α').unwrap();
assert_eq!(sym.latex, "alpha");
assert_eq!(sym.category, SymbolCategory::Greek);
}
}