You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

288 lines
9.3 KiB
Rust

use crate::elements::literal::{Literal, Number, PlainText, Symbol};
use crate::tokens::{Arrow, FontCommand, Function, Greek, Logical, Misc, Operation, Relation};
use htmlescape::encode_minimal;
pub trait ToMathML {
fn to_mathml(&self) -> String;
}
impl ToMathML for Literal {
fn to_mathml(&self) -> String {
match self {
Literal::Text(t) => t.to_mathml(),
Literal::Symbol(s) => s.to_mathml(),
Literal::Number(n) => n.to_mathml(),
Literal::Greek(g) => g.to_mathml(),
Literal::FontCommand(f) => f.to_mathml(),
Literal::Relation(r) => r.to_mathml(),
Literal::Function(f) => f.to_mathml(),
Literal::Logical(l) => l.to_mathml(),
Literal::Arrow(a) => a.to_mathml(),
Literal::Misc(m) => m.to_mathml(),
Literal::Operation(o) => o.to_mathml(),
}
}
}
impl ToMathML for Greek {
fn to_mathml(&self) -> String {
let inner = match self {
Greek::Alpha => "α",
Greek::Beta => "β",
Greek::Gamma => "γ",
Greek::BigGamma => "Γ",
Greek::Delta => "δ",
Greek::BigDelta => "Δ",
Greek::Epsilon => "ε",
Greek::VarEpsilon => "ε",
Greek::Zeta => "ζ",
Greek::Eta => "η",
Greek::Theta => "θ",
Greek::BigTheta => "Θ",
Greek::VarTheta => "θ",
Greek::Iota => "ι",
Greek::Kappa => "κ",
Greek::Lambda => "λ",
Greek::BigLambda => "Λ",
Greek::Mu => "μ",
Greek::Nu => "ν",
Greek::Xi => "ξ",
Greek::BigXi => "Ξ",
Greek::Pi => "π",
Greek::BigPi => "Π",
Greek::Rho => "ρ",
Greek::Sigma => "σ",
Greek::BigSigma => "Σ",
Greek::Tau => "τ",
Greek::Upsilon => "υ",
Greek::Phi => "φ",
Greek::BigPhi => "Φ",
Greek::VarPhi => "φ",
Greek::Chi => "χ",
Greek::Psi => "ψ",
Greek::BigPsi => "Ψ",
Greek::Omega => "ω",
Greek::BigOmega => "Ω",
};
format!("<mi>{}</mi>", inner)
}
}
impl ToMathML for PlainText {
fn to_mathml(&self) -> String {
if let Some(formatting) = &self.formatting {
format!(
"<mtext mathvariant='{}'/>{}</mtext>",
formatting.to_mathml(),
encode_minimal(self.text.as_str())
)
} else {
format!("<mtext/>{}</mtext>", encode_minimal(self.text.as_str()))
}
}
}
impl ToMathML for FontCommand {
fn to_mathml(&self) -> String {
match self {
FontCommand::Big => "bold".to_string(),
FontCommand::BigOutline => "double-struck",
FontCommand::Cursive => "italic",
FontCommand::TText => "script",
FontCommand::Fr => "bold-fraktur",
FontCommand::SansSerif => "sans-serif",
}
}
}
impl ToMathML for Symbol {
fn to_mathml(&self) -> String {
format!("<mi>{}</mi>", encode_minimal(self.symbol.as_str()))
}
}
impl ToMathML for Number {
fn to_mathml(&self) -> String {
format!("<mn>{}</mn>", encode_minimal(self.number.as_str()))
}
}
impl ToMathML for Relation {
fn to_mathml(&self) -> String {
let inner = match self {
Relation::Eq => "=",
Relation::Ne => "&ne;",
Relation::Lt => "&lt;",
Relation::Gt => "&gt;",
Relation::Le => "&le;",
Relation::Ge => "&ge;",
Relation::Prec => "&pr;",
Relation::Succ => "&sc;",
Relation::PrecEq => "&prcue;",
Relation::SuccEq => "&sccue;",
Relation::In => "&isin;",
Relation::NotIn => "&notin;",
Relation::SubSet => "&sub;",
Relation::SupSet => "&sup;",
Relation::SubSetEq => "&sube;",
Relation::SupSetEq => "&supe;",
Relation::Equiv => "&equiv;",
Relation::Cong => "&cong;",
Relation::Approx => "&asymp;",
Relation::PropTo => "&prop;",
};
format!("<mo>{}</mo>", inner)
}
}
impl ToMathML for Function {
fn to_mathml(&self) -> String {
let inner = match self {
Function::Exp => "exp",
Function::Sin => "sin",
Function::Max => "max",
Function::Min => "min",
Function::Glb => "glb",
Function::G => "g",
Function::Lub => "lub",
Function::Lcm => "lcm",
Function::Gcd => "gcd",
Function::Mod => "mod",
Function::Dim => "dim",
Function::Det => "det",
Function::Ln => "ln",
Function::Log => "log",
Function::Cot => "cot",
Function::Csc => "csc",
Function::Sech => "sech",
Function::Tanh => "tanh",
Function::Cosh => "cosh",
Function::ArcSin => "arcsin",
Function::ArcCos => "arccos",
Function::ArcTan => "arctan",
Function::Tan => "tan",
Function::Cos => "cos",
Function::F => "f",
Function::Sec => "sec",
Function::Sinh => "sinh",
Function::Csch => "csch",
Function::Coth => "coth",
};
format!("<mi>{}</mi>", inner)
}
}
impl ToMathML for Logical {
fn to_mathml(&self) -> String {
let inner = match self {
Logical::And => "and",
Logical::Or => "or",
Logical::Not => "&not;",
Logical::Implies => "&rArr;",
Logical::If => "if",
Logical::Iff => "&hArr;",
Logical::ForAll => "&forall;",
Logical::Exists => "exists;",
Logical::Bot => "&perp;",
Logical::Top => "&top;",
Logical::VDash => "&vdash;",
Logical::Models => "&vDash;",
};
format!("<mo>{}</mo>", inner)
}
}
impl ToMathML for Arrow {
fn to_mathml(&self) -> String {
let inner = match self {
Arrow::UpArrow => "&#8593;",
Arrow::DownArrow => "&#8595;",
Arrow::RightArrow => "&#8594;",
Arrow::To => "&#8594;",
Arrow::RightArrowTail => "&#8611;",
Arrow::TwoHeadRightArrow => "&#8608;",
Arrow::TwoHeadRightArrowTail => "&Rarrtl;",
Arrow::MapsTo => "&#8614;",
Arrow::LeftArrow => "&#8592;",
Arrow::LeftRightArrow => "&#10231;",
Arrow::BigRightArrow => "&#8680;",
Arrow::BigLeftArrow => "&#8678;",
Arrow::BigLeftRightArrow => "&#11012;",
};
format!("<mo>{}</mo>", inner)
}
}
impl ToMathML for Misc {
fn to_mathml(&self) -> String {
let inner = match self {
Misc::Del => "&part;",
Misc::Grad => "&nabla;",
Misc::PlusMinus => "&plusmn;",
Misc::EmptySet => "&empty;",
Misc::Infty => "&infin;",
Misc::Aleph => "&alefsym;",
Misc::Therefore => "&there4;",
Misc::Because => "&because;",
Misc::PLDots => "|&hellip;|",
Misc::PCDots => "|&middot;&middot;&middot;|",
Misc::VDots => "&#65049;",
Misc::DDots => "&dtdot;",
Misc::EPipes => "||",
Misc::EQuad => "| |",
Misc::Angle => "&ang;",
Misc::Frown => "&#8994;",
Misc::Triangle => "&#9651;",
Misc::Diamond => "&diamond;",
Misc::Square => "&#9633;",
Misc::LFloor => "&lfloor;",
Misc::RFloor => "&rfloor;",
Misc::LCeiling => "&lceil;",
Misc::RCeiling => "&rceil;",
Misc::Complex => "&Copf;",
Misc::Natural => "&Nopf;",
Misc::Rational => "&Qopf;",
Misc::Real => "&Ropf;",
Misc::Integer => "&Zopf;",
_ => "",
};
format!("<mi>{}</mi>", inner)
}
}
impl ToMathML for Operation {
fn to_mathml(&self) -> String {
let inner = match self {
Operation::Plus => "&plus;",
Operation::Minus => "&minus;",
Operation::CDot => "&#8729;",
Operation::Ast => "&lowast;",
Operation::Star => "&Star;",
Operation::Slash => "/",
Operation::Backslash => "&setminus;",
Operation::Times => "&times;",
Operation::Div => "&divide;",
Operation::LTimes => "&ltimes;",
Operation::RTimes => "&rtimes;",
Operation::Bowtie => "&bowtie;",
Operation::Circ => "&compfn;",
Operation::OPlus => "&oplus;",
Operation::OTimes => "&otimes;",
Operation::ODot => "&odot;",
Operation::Wedge => "&and;",
Operation::BidWedge => "&xwedge;",
Operation::Vee => "&or;",
Operation::BigVee => "&xvee;",
Operation::Cap => "&cap;",
Operation::BigCap => "&xcap;",
Operation::Cup => "&cup;",
Operation::BigCup => "&xcup;",
_ => "",
};
format!("<mo>{}</mo>", inner)
}
}