use std::fmt::Write as _;
use super::xml::{Kind, escape};
pub(super) const INSERT_START: char = '\u{2E0E}';
pub(super) const INSERT_END: char = '\u{2E0F}';
pub(super) const DELETE_START: char = '\u{2E10}';
pub(super) const DELETE_END: char = '\u{2E11}';
const MARKERS: [char; 4] = [INSERT_START, INSERT_END, DELETE_START, DELETE_END];
pub(super) fn literal_markers(text: &str, out: &mut String) {
for c in text.chars() {
if MARKERS.contains(&c) {
out.push('\\');
}
out.push(c);
}
}
pub(super) const M_NS: &str = "http://schemas.openxmlformats.org/officeDocument/2006/math";
const FONT: &str = "<w:rPr><w:rFonts w:ascii=\"Cambria Math\" w:hAnsi=\"Cambria Math\"/></w:rPr>";
pub(super) const FUNCTIONS: [&str; 33] = [
"sin", "cos", "tan", "cot", "sec", "csc", "sinh", "cosh", "tanh", "coth", "arcsin", "arccos",
"arctan", "log", "ln", "lg", "exp", "lim", "liminf", "limsup", "max", "min", "sup", "inf",
"det", "gcd", "Pr", "arg", "deg", "dim", "hom", "ker", "mod",
];
const LIMIT_FUNCTIONS: [&str; 9] = [
"lim", "liminf", "limsup", "max", "min", "sup", "inf", "det", "gcd",
];
pub(super) const NARY: [(&str, char); 30] = [
("int", 'ā«'),
("sum", 'ā'),
("prod", 'ā'),
("coprod", 'ā'),
("iint", 'ā¬'),
("iiint", 'ā'),
("oint", 'ā®'),
("oiint", 'āÆ'),
("oiiint", 'ā°'),
("bigcup", 'ā'),
("bigcap", 'ā'),
("bigwedge", 'ā'),
("bigvee", 'ā'),
("bigodot", 'āØ'),
("bigoplus", 'āØ'),
("bigotimes", 'āØ'),
("biguplus", 'āØ'),
("bigsqcup", 'āØ'),
("intop", 'ā«'),
("smallint", 'ā«'),
("bigsqcap", 'āØ
'),
("bigtimes", 'āØ'),
("fint", 'āØ'),
("iiiint", 'āØ'),
("intBar", 'āØ'),
("intbar", 'āØ'),
("ointctrclockwise", 'ā³'),
("sqint", 'āØ'),
("sumint", 'āØ'),
("varointclockwise", 'ā²'),
];
pub(super) const ACCENTS: [(&str, char); 15] = [
("hat", '\u{302}'),
("widehat", '\u{302}'),
("tilde", '\u{303}'),
("widetilde", '\u{303}'),
("bar", '\u{305}'),
("dot", '\u{307}'),
("ddot", '\u{308}'),
("dddot", '\u{20db}'),
("vec", '\u{20d7}'),
("overrightarrow", '\u{20d7}'),
("overleftarrow", '\u{20d6}'),
("check", '\u{30c}'),
("breve", '\u{306}'),
("acute", '\u{301}'),
("grave", '\u{300}'),
];
const SYMBOLS: &[(&str, &str)] = &[
("alpha", "α"),
("beta", "β"),
("gamma", "γ"),
("delta", "Ī“"),
("epsilon", "ϵ"),
("varepsilon", "ε"),
("zeta", "ζ"),
("eta", "Ī·"),
("theta", "Īø"),
("vartheta", "Ļ"),
("iota", "ι"),
("kappa", "Īŗ"),
("lambda", "Ī»"),
("mu", "μ"),
("nu", "ν"),
("xi", "ξ"),
("omicron", "Īæ"),
("pi", "Ļ"),
("varpi", "Ļ"),
("rho", "Ļ"),
("varrho", "ϱ"),
("sigma", "Ļ"),
("varsigma", "Ļ"),
("tau", "Ļ"),
("upsilon", "Ļ
"),
("phi", "Ļ"),
("varphi", "Ļ"),
("chi", "Ļ"),
("psi", "Ļ"),
("omega", "Ļ"),
("Gamma", "Ī"),
("Delta", "Ī"),
("Theta", "Ī"),
("Lambda", "Ī"),
("Xi", "Ī"),
("Pi", "Ī "),
("Sigma", "Ī£"),
("Upsilon", "Ī„"),
("Phi", "Φ"),
("Psi", "ĪØ"),
("Omega", "Ī©"),
("pm", "±"),
("mp", "ā"),
("times", "Ć"),
("div", "Ć·"),
("cdot", "ā
"),
("ast", "ā"),
("star", "ā"),
("circ", "ā"),
("bullet", "ā"),
("oplus", "ā"),
("ominus", "ā"),
("otimes", "ā"),
("odot", "ā"),
("setminus", "ā"),
("cup", "āŖ"),
("cap", "ā©"),
("wedge", "ā§"),
("land", "ā§"),
("vee", "āØ"),
("lor", "āØ"),
("neg", "¬"),
("lnot", "¬"),
("le", "ā¤"),
("leq", "ā¤"),
("ge", "ā„"),
("geq", "ā„"),
("ne", "ā "),
("neq", "ā "),
("ll", "āŖ"),
("gg", "ā«"),
("approx", "ā"),
("equiv", "ā”"),
("sim", "ā¼"),
("simeq", "ā"),
("cong", "ā
"),
("propto", "ā"),
("prec", "āŗ"),
("succ", "ā»"),
("preceq", "āŖÆ"),
("succeq", "āŖ°"),
("in", "ā"),
("notin", "ā"),
("ni", "ā"),
("subset", "ā"),
("supset", "ā"),
("subseteq", "ā"),
("supseteq", "ā"),
("perp", "ā„"),
("parallel", "ā„"),
("mid", "ā£"),
("nmid", "ā¤"),
("vdash", "ā¢"),
("models", "āØ"),
("to", "ā"),
("rightarrow", "ā"),
("leftarrow", "ā"),
("gets", "ā"),
("leftrightarrow", "ā"),
("Rightarrow", "ā"),
("Leftarrow", "ā"),
("Leftrightarrow", "ā"),
("implies", "ā¹"),
("iff", "āŗ"),
("mapsto", "ā¦"),
("uparrow", "ā"),
("downarrow", "ā"),
("longrightarrow", "ā¶"),
("longleftarrow", "āµ"),
("infty", "ā"),
("partial", "ā"),
("nabla", "ā"),
("forall", "ā"),
("exists", "ā"),
("nexists", "ā"),
("emptyset", "ā
"),
("varnothing", "ā
"),
("angle", "ā "),
("triangle", "ā³"),
("hbar", "ā"),
("ell", "ā"),
("Re", "ā"),
("Im", "ā"),
("aleph", "āµ"),
("wp", "ā"),
("prime", "ā²"),
("degree", "°"),
("ldots", "ā¦"),
("dots", "ā¦"),
("cdots", "āÆ"),
("vdots", "ā®"),
("ddots", "ā±"),
("therefore", "ā“"),
("because", "āµ"),
("langle", "āØ"),
("rangle", "ā©"),
("lfloor", "ā"),
("rfloor", "ā"),
("lceil", "ā"),
("rceil", "ā"),
("lvert", "|"),
("rvert", "|"),
("vert", "|"),
("Vert", "ā"),
("lparen", "("),
("rparen", ")"),
("lbrack", "["),
("rbrack", "]"),
("lbrace", "{"),
("rbrace", "}"),
("backslash", "\\"),
("quad", "\u{2003}"),
("qquad", "\u{2003}\u{2003}"),
("Box", "ā”"),
("Bumpeq", "ā"),
("Cap", "ā"),
("Cup", "ā"),
("Diamond", "ā"),
("Digamma", "Ļ"),
("Downarrow", "ā"),
("Finv", "ā²"),
("Game", "ā
"),
("Longleftarrow", "āø"),
("Longleftrightarrow", "āŗ"),
("Longrightarrow", "ā¹"),
("Lsh", "ā°"),
("Rsh", "ā±"),
("Subset", "ā"),
("Supset", "ā"),
("Uparrow", "ā"),
("VDash", "ā«"),
("Vdash", "ā©"),
("Vvdash", "āŖ"),
("adots", "ā°"),
("amalg", "⨿"),
("approxeq", "ā"),
("asymp", "ā"),
("backepsilon", "϶"),
("backprime", "āµ"),
("backsim", "ā½"),
("backsimeq", "ā"),
("barwedge", "ā¼"),
("beth", "ā¶"),
("between", "ā¬"),
("bigcirc", "āÆ"),
("bigstar", "ā
"),
("bigtriangledown", "ā½"),
("bigtriangleup", "ā³"),
("blacklozenge", "ā§«"),
("blacksquare", "ā "),
("blacktriangle", "ā“"),
("blacktriangledown", "ā¾"),
("bot", "ā„"),
("boxdot", "ā”"),
("boxminus", "ā"),
("boxplus", "ā"),
("boxtimes", "ā "),
("bumpeq", "ā"),
("cdotp", "Ā·"),
("checkmark", "ā"),
("circeq", "ā"),
("circlearrowleft", "āŗ"),
("circlearrowright", "ā»"),
("circledR", "Ā®"),
("circledS", "ā"),
("circledast", "ā"),
("circledcirc", "ā"),
("circleddash", "ā"),
("clubsuit", "ā£"),
("coloneq", "ā"),
("complement", "ā"),
("curlyvee", "ā"),
("curlywedge", "ā"),
("curvearrowleft", "ā¶"),
("curvearrowright", "ā·"),
("daleth", "āø"),
("dashleftarrow", "ā "),
("dashrightarrow", "ā¢"),
("dashv", "ā£"),
("diamond", "ā"),
("diamondsuit", "ā¢"),
("digamma", "Ļ"),
("divideontimes", "ā"),
("doteq", "ā"),
("dotminus", "āø"),
("dotplus", "ā"),
("dotsb", "āÆ"),
("dotsc", "ā¦"),
("dotsi", "āÆ"),
("dotsm", "āÆ"),
("dotso", "ā¦"),
("doublebarwedge", "ā©"),
("doublecap", "ā"),
("doublecup", "ā"),
("downdownarrows", "ā"),
("downharpoonleft", "ā"),
("downharpoonright", "ā"),
("empty", "ā
"),
("emsp", "\u{2003}"),
("enskip", "\u{2002}"),
("enspace", "\u{2002}"),
("eqcirc", "ā"),
("eqcolon", "ā"),
("eqqsim", "⩳"),
("eqsim", "ā"),
("eqslantgtr", "āŖ"),
("eqslantless", "āŖ"),
("eth", "ư"),
("fallingdotseq", "ā"),
("flat", "ā"),
("frown", "ā¢"),
("geqq", "ā§"),
("geqslant", "⩾"),
("ggg", "ā"),
("gggtr", "ā"),
("gimel", "ā·"),
("gneq", "āŖ"),
("gneqq", "ā©"),
("gtrapprox", "āŖ"),
("gtrdot", "ā"),
("gtreqless", "ā"),
("gtreqqless", "āŖ"),
("gtrless", "ā·"),
("gtrsim", "ā³"),
("heartsuit", "ā”"),
("hookleftarrow", "ā©"),
("hookrightarrow", "āŖ"),
("imath", "š¤"),
("intercal", "āŗ"),
("jmath", "š„"),
("leadsto", "ā"),
("leftarrowtail", "ā¢"),
("leftharpoondown", "ā½"),
("leftharpoonup", "ā¼"),
("leftrightharpoons", "ā"),
("leftrightsquigarrow", "ā"),
("leftsquigarrow", "ā"),
("leftthreetimes", "ā"),
("leqq", "ā¦"),
("leqslant", "⩽"),
("lessapprox", "āŖ
"),
("lessdot", "ā"),
("lesseqgtr", "ā"),
("lesseqqgtr", "āŖ"),
("lessgtr", "ā¶"),
("lesssim", "ā²"),
("lgroup", "ā®"),
("lhd", "ā²"),
("lll", "ā"),
("llless", "ā"),
("lmoustache", "ā°"),
("lneq", "āŖ"),
("lneqq", "āØ"),
("longleftrightarrow", "ā·"),
("longmapsfrom", "ā»"),
("longmapsto", "ā¼"),
("looparrowleft", "ā«"),
("looparrowright", "ā¬"),
("lozenge", "ā"),
("ltimes", "ā"),
("maltese", "ā "),
("mapsfrom", "ā¤"),
("measuredangle", "ā”"),
("medspace", "\u{205F}"),
("mho", "ā§"),
("multimap", "āø"),
("nLeftarrow", "ā"),
("nLeftrightarrow", "ā"),
("nRightarrow", "ā"),
("nVDash", "āÆ"),
("nVdash", "ā®"),
("napprox", "ā"),
("nasymp", "ā"),
("natural", "ā®"),
("ncong", "ā"),
("nearrow", "ā"),
("ngeq", "ā±"),
("ngtr", "āÆ"),
("nleftarrow", "ā"),
("nleftrightarrow", "ā®"),
("nleq", "ā°"),
("nless", "ā®"),
("nparallel", "ā¦"),
("nprec", "ā"),
("npreceq", "ā "),
("nrightarrow", "ā"),
("nshortmid", "ā¤"),
("nshortparallel", "ā¦"),
("nsim", "ā"),
("nsqsubseteq", "ā¢"),
("nsqsupseteq", "ā£"),
("nsubset", "ā"),
("nsubseteq", "ā"),
("nsucc", "ā"),
("nsucceq", "ā”"),
("nsupset", "ā
"),
("nsupseteq", "ā"),
("ntriangleleft", "āŖ"),
("ntrianglelefteq", "ā¬"),
("ntriangleright", "ā«"),
("ntrianglerighteq", "ā"),
("nvDash", "ā"),
("nvdash", "ā¬"),
("nwarrow", "ā"),
("pitchfork", "ā"),
("preccurlyeq", "ā¼"),
("precnapprox", "āŖ¹"),
("precnsim", "āØ"),
("precsim", "ā¾"),
("rgroup", "āÆ"),
("rhd", "ā³"),
("rightarrowtail", "ā£"),
("rightharpoondown", "ā"),
("rightharpoonup", "ā"),
("rightleftharpoons", "ā"),
("rightsquigarrow", "ā"),
("rightthreetimes", "ā"),
("risingdotseq", "ā"),
("rmoustache", "ā±"),
("rtimes", "ā"),
("searrow", "ā"),
("sharp", "āÆ"),
("shortmid", "ā£"),
("shortparallel", "ā„"),
("smallfrown", "ā¢"),
("smallsetminus", "ā"),
("smallsmile", "ā£"),
("smile", "ā£"),
("spadesuit", "ā "),
("sphericalangle", "ā¢"),
("sqcap", "ā"),
("sqcup", "ā"),
("sqsubset", "ā"),
("sqsubseteq", "ā"),
("sqsupset", "ā"),
("sqsupseteq", "ā"),
("square", "ā”"),
("subseteqq", "ā«
"),
("subsetneq", "ā"),
("subsetneqq", "ā«"),
("succcurlyeq", "ā½"),
("succnapprox", "āŖŗ"),
("succnsim", "ā©"),
("succsim", "āæ"),
("supseteqq", "ā«"),
("supsetneq", "ā"),
("supsetneqq", "ā«"),
("surd", "ā"),
("swarrow", "ā"),
("thickspace", "\u{2005}"),
("thinspace", "\u{2009}"),
("top", "ā¤"),
("triangledown", "āæ"),
("triangleleft", "ā"),
("trianglelefteq", "ā“"),
("triangleq", "ā"),
("triangleright", "ā·"),
("trianglerighteq", "āµ"),
("twoheadleftarrow", "ā"),
("twoheadmapsto", "ā¤
"),
("twoheadrightarrow", "ā "),
("unlhd", "ā“"),
("unrhd", "āµ"),
("updownarrow", "ā"),
("upharpoonleft", "āæ"),
("upharpoonright", "ā¾"),
("uplus", "ā"),
("upuparrows", "ā"),
("vDash", "āØ"),
("varpropto", "ā"),
("varsubsetneq", "ā"),
("varsubsetneqq", "ā«"),
("varsupsetneq", "ā"),
("varsupsetneqq", "ā«"),
("vartriangle", "āµ"),
("vartriangleleft", "ā²"),
("vartriangleright", "ā³"),
("veebar", "ā»"),
("wedgebar", "ā©"),
("wr", "ā"),
];
pub(super) fn nary_command(chr: char) -> Option<&'static str> {
NARY.iter().find(|(_, c)| *c == chr).map(|(name, _)| *name)
}
pub(super) fn accent_command(chr: char) -> Option<&'static str> {
ACCENTS
.iter()
.find(|(_, c)| *c == chr)
.map(|(name, _)| *name)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Style {
Math,
Upright,
Bold,
BoldItalic,
Text,
Script(&'static str),
}
#[derive(Clone, Debug, PartialEq, Eq)]
enum Node {
Text(String, Style, Option<Kind>),
Group(Vec<Node>),
Fraction {
num: Vec<Node>,
den: Vec<Node>,
bar: bool,
},
Radical {
degree: Vec<Node>,
body: Vec<Node>,
},
Scripts {
base: Vec<Node>,
sub: Option<Vec<Node>>,
sup: Option<Vec<Node>>,
},
Pre {
sub: Vec<Node>,
sup: Vec<Node>,
body: Vec<Node>,
},
Delimiter {
open: String,
close: String,
separator: String,
items: Vec<Vec<Node>>,
},
Function {
name: Vec<Node>,
body: Vec<Node>,
},
Nary {
chr: char,
sub: Vec<Node>,
sup: Vec<Node>,
body: Vec<Node>,
},
Accent {
chr: char,
body: Vec<Node>,
},
Bar {
top: bool,
body: Vec<Node>,
},
Limit {
upper: bool,
base: Vec<Node>,
limit: Vec<Node>,
},
Brace {
top: bool,
body: Vec<Node>,
},
Matrix(Vec<Vec<Vec<Node>>>),
Array(Vec<Vec<Node>>),
Boxed {
operator: bool,
body: Vec<Node>,
},
}
#[derive(Clone, Debug, PartialEq, Eq)]
enum Token {
Char(char),
Command(String),
Open,
Close,
Sup,
Sub,
Amp,
Change(Option<Kind>),
}
fn tokens(latex: &str) -> Vec<Token> {
let chars: Vec<char> = latex.chars().collect();
let mut out = Vec::new();
let mut i = 0;
while i < chars.len() {
let c = chars[i];
i += 1;
out.push(match c {
'\\' => match chars.get(i) {
Some(&n) if MARKERS.contains(&n) => {
i += 1;
Token::Char(n)
}
Some(n) if n.is_ascii_alphabetic() => {
let start = i;
while chars.get(i).is_some_and(char::is_ascii_alphabetic) {
i += 1;
}
let name: String = chars[start..i].iter().collect();
if chars.get(i) == Some(&'*') {
i += 1;
}
Token::Command(name)
}
Some(&n) => {
i += 1;
Token::Command(n.to_string())
}
None => Token::Char('\\'),
},
'{' => Token::Open,
'}' => Token::Close,
'^' => Token::Sup,
'_' => Token::Sub,
'&' => Token::Amp,
INSERT_START => Token::Change(Some(Kind::Insert)),
DELETE_START => Token::Change(Some(Kind::Delete)),
INSERT_END | DELETE_END => Token::Change(None),
c => Token::Char(c),
});
}
out
}
fn closer(token: &Token) -> Option<Token> {
match token {
Token::Char('(') => Some(Token::Char(')')),
Token::Char('[') => Some(Token::Char(']')),
Token::Command(c) if c == "{" => Some(Token::Command("}".into())),
_ => None,
}
}
fn is_command(token: &Token, name: &str) -> bool {
matches!(token, Token::Command(c) if c == name)
}
fn pairs(tokens: &[Token]) -> Vec<Option<usize>> {
enum Frame {
Bracket(usize),
Wall,
}
let mut out = vec![None; tokens.len()];
let mut stack: Vec<Frame> = Vec::new();
let unwind = |stack: &mut Vec<Frame>| {
while let Some(Frame::Bracket(_)) = stack.last() {
stack.pop();
}
};
for (i, token) in tokens.iter().enumerate() {
match token {
Token::Open => stack.push(Frame::Wall),
Token::Close => {
unwind(&mut stack);
stack.pop();
}
Token::Command(c) if c == "left" || c == "begin" => stack.push(Frame::Wall),
Token::Command(c) if c == "right" || c == "end" => {
unwind(&mut stack);
stack.pop();
}
Token::Amp => unwind(&mut stack),
Token::Command(c) if c == "\\" || c == "middle" => unwind(&mut stack),
_ if closer(token).is_some() => {
let taken = i > 0
&& (is_command(&tokens[i - 1], "left")
|| is_command(&tokens[i - 1], "right")
|| is_command(&tokens[i - 1], "middle"));
if !taken {
stack.push(Frame::Bracket(i));
}
}
_ => {
if let Some(Frame::Bracket(open)) = stack.last()
&& closer(&tokens[*open]).as_ref() == Some(token)
{
out[*open] = Some(i);
stack.pop();
}
}
}
}
out
}
#[derive(Clone, Copy, Default)]
struct Stops {
group: bool,
at: Option<usize>,
right: bool,
rows: bool,
cells: bool,
}
struct Parser {
tokens: Vec<Token>,
pairs: Vec<Option<usize>>,
at: usize,
change: Option<Kind>,
depth: usize,
}
const MOST_NESTING: usize = 64;
fn token_latex(token: &Token) -> String {
match token {
Token::Char(c) => c.to_string(),
Token::Command(name) => format!("\\{name}"),
Token::Open => "{".into(),
Token::Close => "}".into(),
Token::Sup => "^".into(),
Token::Sub => "_".into(),
Token::Amp => "&".into(),
Token::Change(_) => String::new(),
}
}
impl Parser {
fn new(latex: &str) -> Self {
let tokens = tokens(latex);
let pairs = pairs(&tokens);
Self {
tokens,
pairs,
at: 0,
change: None,
depth: 0,
}
}
fn peek(&self) -> Option<&Token> {
self.tokens.get(self.at)
}
fn text(&self, text: &str, style: Style) -> Node {
Node::Text(text.into(), style, self.change)
}
fn stopped(&self, stops: Stops) -> bool {
let Some(token) = self.peek() else {
return true;
};
stops.at == Some(self.at)
|| (stops.group && *token == Token::Close)
|| (stops.right && (is_command(token, "right") || is_command(token, "middle")))
|| (stops.rows && (is_command(token, "\\") || is_command(token, "end")))
|| (stops.cells && *token == Token::Amp)
}
fn skip_spaces(&mut self) {
while matches!(self.peek(), Some(Token::Char(c)) if c.is_whitespace()) {
self.at += 1;
}
}
fn list(&mut self, stops: Stops) -> Vec<Node> {
if self.depth >= MOST_NESTING {
return self.flat(stops);
}
self.depth += 1;
let nodes = self.nested_list(stops);
self.depth -= 1;
nodes
}
fn flat(&mut self, stops: Stops) -> Vec<Node> {
let mut latex = String::new();
let mut open = 0usize;
while let Some(token) = self.peek() {
if open == 0 && self.stopped(stops) {
break;
}
match token {
Token::Open => open += 1,
Token::Close => open = open.saturating_sub(1),
_ => {}
}
latex.push_str(&token_latex(token));
self.at += 1;
}
if latex.is_empty() {
Vec::new()
} else {
vec![self.text(&latex, Style::Math)]
}
}
fn nested_list(&mut self, stops: Stops) -> Vec<Node> {
let mut out: Vec<Node> = Vec::new();
while !self.stopped(stops) {
match self.peek() {
Some(Token::Change(change)) => {
self.change = *change;
self.at += 1;
}
Some(Token::Sub | Token::Sup) => {
let base = take_base(&mut out);
let node = self.scripts(base);
out.push(node);
}
Some(Token::Char('\'')) if !out.is_empty() => {
let base = take_base(&mut out);
let mut count = 0;
while self.peek() == Some(&Token::Char('\'')) {
self.at += 1;
count += 1;
}
let mut sup = vec![self.text(&primes(count), Style::Math)];
if self.peek() == Some(&Token::Sup) {
self.at += 1;
sup.extend(self.argument());
}
let sub = if self.peek() == Some(&Token::Sub) {
self.at += 1;
Some(self.argument())
} else {
None
};
out.push(Node::Scripts {
base,
sub,
sup: Some(merge(sup)),
});
}
_ => {
if let Some(node) = self.atom(stops) {
out.push(node);
}
}
}
}
merge(out)
}
fn group(&mut self) -> Vec<Node> {
self.at += 1;
let nodes = self.list(Stops {
group: true,
..Stops::default()
});
if self.peek() == Some(&Token::Close) {
self.at += 1;
}
nodes
}
fn argument(&mut self) -> Vec<Node> {
self.skip_spaces();
while let Some(Token::Change(change)) = self.peek() {
self.change = *change;
self.at += 1;
}
match self.peek() {
Some(Token::Open) => self.group(),
None | Some(Token::Close | Token::Amp | Token::Sub | Token::Sup) => Vec::new(),
Some(_) => self.atom(Stops::default()).into_iter().collect(),
}
}
fn raw_argument(&mut self) -> String {
self.skip_spaces();
if self.peek() != Some(&Token::Open) {
return self.argument().iter().map(plain).collect();
}
self.at += 1;
let mut depth = 0usize;
let mut out = String::new();
while let Some(token) = self.peek().cloned() {
self.at += 1;
match token {
Token::Open => {
depth += 1;
out.push('{');
}
Token::Close if depth == 0 => break,
Token::Close => {
depth -= 1;
out.push('}');
}
Token::Char(c) => out.push(c),
Token::Sup => out.push('^'),
Token::Sub => out.push('_'),
Token::Amp => out.push('&'),
Token::Command(name) => {
out.push_str(&escaped(&name));
if name.starts_with(|c: char| c.is_ascii_alphabetic()) {
self.skip_spaces();
}
}
Token::Change(change) => self.change = change,
}
}
out
}
fn optional(&mut self) -> Option<Vec<Node>> {
self.skip_spaces();
if self.peek() != Some(&Token::Char('[')) {
return None;
}
let end = self.pairs[self.at]?;
self.at += 1;
let nodes = self.list(Stops {
at: Some(end),
..Stops::default()
});
self.at = end + 1;
Some(nodes)
}
fn scripts(&mut self, base: Vec<Node>) -> Node {
let (mut sub, mut sup) = (None, None);
loop {
match self.peek() {
Some(Token::Sub) if sub.is_none() => {
self.at += 1;
sub = Some(self.argument());
}
Some(Token::Sup) if sup.is_none() => {
self.at += 1;
sup = Some(self.argument());
}
_ => break,
}
}
Node::Scripts { base, sub, sup }
}
fn operand(&mut self, stops: Stops) -> Vec<Node> {
self.skip_spaces();
if self.stopped(stops) || matches!(self.peek(), Some(Token::Sub | Token::Sup)) {
return Vec::new();
}
if self.peek() == Some(&Token::Open) {
let nodes = self.group();
return if matches!(self.peek(), Some(Token::Sub | Token::Sup)) {
vec![self.scripts(vec![Node::Group(nodes)])]
} else {
nodes
};
}
let Some(node) = self.atom(stops) else {
return Vec::new();
};
if matches!(self.peek(), Some(Token::Sub | Token::Sup)) {
vec![self.scripts(vec![node])]
} else {
vec![node]
}
}
fn atom(&mut self, stops: Stops) -> Option<Node> {
if self.depth >= MOST_NESTING {
let token = self.peek()?.clone();
self.at += 1;
return Some(self.text(&token_latex(&token), Style::Math));
}
self.depth += 1;
let node = self.nested_atom(stops);
self.depth -= 1;
node
}
fn nested_atom(&mut self, stops: Stops) -> Option<Node> {
let token = self.peek()?.clone();
let start = self.at;
self.at += 1;
match token {
Token::Open => {
self.at = start;
Some(Node::Group(self.group()))
}
Token::Close => Some(self.text("}", Style::Math)),
Token::Amp => Some(self.text("&", Style::Math)),
Token::Sub | Token::Sup => None,
Token::Change(change) => {
self.change = change;
None
}
Token::Char(c) => {
if let Some(end) = self.pairs[start] {
let close = if c == '(' { ")" } else { "]" };
return Some(self.bracketed(&c.to_string(), close, end, stops));
}
Some(self.text(&c.to_string(), Style::Math))
}
Token::Command(name) => self.command(&name, start, stops),
}
}
fn bracketed(&mut self, open: &str, close: &str, end: usize, stops: Stops) -> Node {
let body = self.list(Stops {
at: Some(end),
..stops
});
self.at = end + 1;
Node::Delimiter {
open: open.into(),
close: close.into(),
separator: "|".into(),
items: vec![body],
}
}
fn command(&mut self, name: &str, start: usize, stops: Stops) -> Option<Node> {
let change = self.change;
let text = |s: &str| Some(Node::Text(s.into(), Style::Math, change));
let word = name.starts_with(|c: char| c.is_ascii_alphabetic());
let spaced = word && matches!(self.peek(), Some(Token::Char(c)) if c.is_whitespace());
if word {
self.skip_spaces();
}
match name {
"{" => {
if let Some(end) = self.pairs[start] {
return Some(self.bracketed("{", "}", end, stops));
}
text("{")
}
"frac" | "dfrac" | "tfrac" | "cfrac" => {
let num = self.argument();
let den = self.argument();
Some(Node::Fraction {
num,
den,
bar: true,
})
}
"binom" | "dbinom" | "tbinom" => {
let num = self.argument();
let den = self.argument();
Some(delimiter(
"(",
")",
vec![Node::Fraction {
num,
den,
bar: false,
}],
))
}
"genfrac" => {
let open = self.raw_argument();
let close = self.raw_argument();
let thickness = self.raw_argument();
let _style = self.raw_argument();
let num = self.argument();
let den = self.argument();
let fraction = Node::Fraction {
num,
den,
bar: !thickness.trim().starts_with('0'),
};
if open.is_empty() && close.is_empty() {
Some(fraction)
} else {
Some(delimiter(&open, &close, vec![fraction]))
}
}
"sqrt" => {
let degree = self.optional().unwrap_or_default();
let body = self.argument();
Some(Node::Radical { degree, body })
}
"left" => Some(self.left()),
"prescript" => {
let sub = self.argument();
let sup = self.argument();
let body = self.argument();
Some(Node::Pre { sub, sup, body })
}
"overline" | "underline" => Some(Node::Bar {
top: name == "overline",
body: self.argument(),
}),
"overbrace" | "underbrace" => {
let top = name == "overbrace";
let brace = Node::Brace {
top,
body: self.argument(),
};
let script = if top { Token::Sup } else { Token::Sub };
if self.peek() == Some(&script) {
self.at += 1;
let limit = self.argument();
return Some(Node::Limit {
upper: top,
base: vec![brace],
limit,
});
}
Some(brace)
}
"underset" | "overset" | "stackrel" => {
let limit = self.argument();
let base = self.argument();
Some(Node::Limit {
upper: name != "underset",
base,
limit,
})
}
"operatorname" => {
let word = self.raw_argument();
Some(self.function(&word, true, stops))
}
"begin" => Some(self.environment()),
"text" | "textrm" | "textup" | "mbox" | "textnormal" => {
let raw = self.raw_argument();
Some(self.text(&raw, Style::Text))
}
"mathord" | "mathop" => Some(Node::Boxed {
operator: name == "mathop",
body: self.argument(),
}),
"mathrm" | "mathup" | "rm" => Some(styled(self.argument(), Style::Upright)),
"mathbf" | "bf" => Some(styled(self.argument(), Style::Bold)),
"boldsymbol" | "bm" | "mathbfit" => Some(styled(self.argument(), Style::BoldItalic)),
"mathit" => Some(styled(self.argument(), Style::Math)),
"mathbb" => Some(styled(self.argument(), Style::Script("double-struck"))),
"mathcal" | "mathscr" => Some(styled(self.argument(), Style::Script("script"))),
"mathfrak" => Some(styled(self.argument(), Style::Script("fraktur"))),
"mathsf" => Some(styled(self.argument(), Style::Script("sans-serif"))),
"mathtt" => Some(styled(self.argument(), Style::Script("monospace"))),
"displaystyle" | "textstyle" | "scriptstyle" | "limits" | "nolimits" | "!" => None,
"big" | "bigl" | "bigr" | "bigm" | "Big" | "Bigl" | "Bigr" | "Bigm" | "bigg"
| "biggl" | "biggr" | "biggm" | "Bigg" | "Biggl" | "Biggr" | "Biggm" => None,
"not" => match self.peek().cloned() {
Some(Token::Char(c)) if !c.is_whitespace() => {
self.at += 1;
text(&negated(&c.to_string()))
}
Some(Token::Command(next)) => match SYMBOLS.iter().find(|(n, _)| *n == next) {
Some((_, symbol)) => {
self.at += 1;
if next.starts_with(|c: char| c.is_ascii_alphabetic()) {
self.skip_spaces();
}
text(&negated(symbol))
}
None => text(&escaped(name)),
},
_ => text(&escaped(name)),
},
"bmod" => Some(Node::Group(vec![
Node::Text("mod".into(), Style::Upright, change),
Node::Text(" ".into(), Style::Math, change),
])),
"pmod" => {
let mut body = vec![
Node::Text("mod".into(), Style::Upright, change),
Node::Text(" ".into(), Style::Math, change),
];
body.extend(self.argument());
Some(delimiter("(", ")", merge(body)))
}
"hspace" | "hskip" | "kern" | "mspace" => {
let _ = self.raw_argument();
text(" ")
}
"," | ":" | ">" => text("\u{2009}"),
";" => text("\u{2005}"),
" " => text(" "),
"\\" | "end" | "right" | "middle" => text(&format!("\\{name}")),
_ => {
if FUNCTIONS.contains(&name) {
return Some(self.function(name, false, stops));
}
if let Some(&(_, chr)) = NARY.iter().find(|(n, _)| *n == name) {
return Some(self.nary(chr, stops));
}
if let Some(&(_, chr)) = ACCENTS.iter().find(|(n, _)| *n == name) {
return Some(Node::Accent {
chr,
body: self.argument(),
});
}
if let Some((_, symbol)) = SYMBOLS.iter().find(|(n, _)| *n == name) {
return text(symbol);
}
if !spaced && self.peek() == Some(&Token::Open) {
let mut nodes = vec![Node::Text(
format!("{}{{", escaped(name)),
Style::Math,
change,
)];
nodes.extend(self.group());
nodes.push(Node::Text("}".into(), Style::Math, self.change));
return Some(Node::Group(nodes));
}
let space = if spaced { " " } else { "" };
text(&format!("{}{space}", escaped(name)))
}
}
}
fn left(&mut self) -> Node {
let open = self.fence();
let mut items = Vec::new();
let mut separator = String::from("|");
loop {
items.push(trimmed(self.list(Stops {
right: true,
..Stops::default()
})));
match self.peek() {
Some(token) if is_command(token, "middle") => {
self.at += 1;
separator = self.fence();
}
Some(token) if is_command(token, "right") => {
self.at += 1;
let close = self.fence();
return Node::Delimiter {
open,
close,
separator,
items,
};
}
_ => {
return Node::Delimiter {
open,
close: String::new(),
separator,
items,
};
}
}
}
}
fn fence(&mut self) -> String {
self.skip_spaces();
let Some(token) = self.peek().cloned() else {
return String::new();
};
self.at += 1;
match token {
Token::Char('.') => String::new(),
Token::Char(c) => c.to_string(),
Token::Command(name) => match name.as_str() {
"{" | "lbrace" => "{".into(),
"}" | "rbrace" => "}".into(),
"|" => "ā".into(),
name => SYMBOLS
.iter()
.find(|(n, _)| *n == name)
.map_or_else(|| escaped(name), |(_, s)| (*s).to_string()),
},
_ => String::new(),
}
}
fn function(&mut self, word: &str, operator: bool, stops: Stops) -> Node {
let mut name = vec![self.text(word, Style::Upright)];
if matches!(self.peek(), Some(Token::Sub | Token::Sup)) {
let limits = operator || LIMIT_FUNCTIONS.contains(&word);
name = vec![if limits {
let upper = self.peek() == Some(&Token::Sup);
self.at += 1;
let limit = self.argument();
Node::Limit {
upper,
base: name,
limit,
}
} else {
self.scripts(name)
}];
}
let body = self.operand(stops);
Node::Function { name, body }
}
fn nary(&mut self, chr: char, stops: Stops) -> Node {
let (mut sub, mut sup) = (Vec::new(), Vec::new());
loop {
match self.peek() {
Some(Token::Sub) => {
self.at += 1;
sub = self.argument();
}
Some(Token::Sup) => {
self.at += 1;
sup = self.argument();
}
Some(token) if is_command(token, "limits") || is_command(token, "nolimits") => {
self.at += 1;
}
_ => break,
}
}
let body = self.operand(stops);
Node::Nary {
chr,
sub,
sup,
body,
}
}
fn environment(&mut self) -> Node {
let name = self.raw_argument();
self.skip_spaces();
if name == "array" && self.peek() == Some(&Token::Open) {
let _ = self.raw_argument();
}
let matrix = name.ends_with("matrix") || name == "array";
let mut rows: Vec<Vec<Vec<Node>>> = vec![Vec::new()];
loop {
let cell = trimmed(self.list(Stops {
rows: true,
cells: matrix,
..Stops::default()
}));
if let Some(row) = rows.last_mut() {
row.push(cell);
}
match self.peek() {
Some(Token::Amp) => self.at += 1,
Some(token) if is_command(token, "\\") => {
self.at += 1;
rows.push(Vec::new());
}
Some(token) if is_command(token, "end") => {
self.at += 1;
let _ = self.raw_argument();
break;
}
_ => break,
}
}
if matrix {
let node = Node::Matrix(rows);
let fences = match name.as_str() {
"pmatrix" => Some(("(", ")")),
"bmatrix" => Some(("[", "]")),
"Bmatrix" => Some(("{", "}")),
"vmatrix" => Some(("|", "|")),
"Vmatrix" => Some(("ā", "ā")),
_ => None,
};
return match fences {
Some((open, close)) => delimiter(open, close, vec![node]),
None => node,
};
}
let lines = rows
.into_iter()
.map(|row| trimmed(row.into_iter().flatten().collect()))
.collect();
let node = Node::Array(lines);
if name == "cases" {
delimiter("{", "", vec![node])
} else {
node
}
}
}
pub(super) fn primes(count: usize) -> String {
match count {
1 => "ā²".into(),
2 => "ā³".into(),
3 => "ā“".into(),
4 => "ā".into(),
n => "ā²".repeat(n),
}
}
pub(super) fn prime_count(text: &str) -> Option<usize> {
let mut count = 0;
for c in text.chars() {
count += match c {
'ā²' => 1,
'ā³' => 2,
'ā“' => 3,
'ā' => 4,
_ => return None,
};
}
(count > 0).then_some(count)
}
fn negated(relation: &str) -> String {
const NEGATED: [(&str, &str); 24] = [
("=", "ā "),
("<", "ā®"),
(">", "āÆ"),
("ā", "ā"),
("ā", "ā"),
("ā", "ā"),
("ā", "ā
"),
("ā", "ā"),
("ā", "ā"),
("ā”", "ā¢"),
("ā¼", "ā"),
("ā¤", "ā°"),
("ā„", "ā±"),
("ā", "ā"),
("ā
", "ā"),
("ā", "ā"),
("ā£", "ā¤"),
("ā„", "ā¦"),
("āŗ", "ā"),
("ā»", "ā"),
("ā", "ā¢"),
("ā", "ā£"),
("ā", "ā"),
("ā", "ā"),
];
NEGATED
.iter()
.find(|(from, _)| *from == relation)
.map_or_else(|| format!("{relation}\u{338}"), |(_, to)| (*to).to_string())
}
fn delimiter(open: &str, close: &str, body: Vec<Node>) -> Node {
Node::Delimiter {
open: open.into(),
close: close.into(),
separator: "|".into(),
items: vec![body],
}
}
fn escaped(name: &str) -> String {
match name {
"{" | "}" | "_" | "&" | "$" | "%" | "#" | "^" => name.into(),
"|" => "ā".into(),
_ => SYMBOLS
.iter()
.find(|(n, _)| *n == name)
.map_or_else(|| format!("\\{name}"), |(_, s)| (*s).to_string()),
}
}
fn plain(node: &Node) -> String {
match node {
Node::Text(text, ..) => text.clone(),
Node::Group(nodes) => nodes.iter().map(plain).collect(),
_ => String::new(),
}
}
fn styled(nodes: Vec<Node>, style: Style) -> Node {
fn restyle(node: Node, style: Style) -> Node {
match node {
Node::Text(text, _, change) => Node::Text(text, style, change),
Node::Group(nodes) => {
Node::Group(nodes.into_iter().map(|n| restyle(n, style)).collect())
}
other => other,
}
}
Node::Group(nodes.into_iter().map(|n| restyle(n, style)).collect())
}
fn take_base(out: &mut Vec<Node>) -> Vec<Node> {
match out.pop() {
None => Vec::new(),
Some(Node::Group(nodes)) => nodes,
Some(Node::Text(text, style, change)) => {
let mut chars: Vec<char> = text.chars().collect();
let last = chars.pop().map(String::from).unwrap_or_default();
if !chars.is_empty() {
out.push(Node::Text(chars.into_iter().collect(), style, change));
}
vec![Node::Text(last, style, change)]
}
Some(node) => vec![node],
}
}
fn trimmed(mut nodes: Vec<Node>) -> Vec<Node> {
if let Some(Node::Text(text, Style::Math, None)) = nodes.first_mut() {
*text = text.trim_start().to_string();
}
if let Some(Node::Text(text, Style::Math, None)) = nodes.last_mut() {
*text = text.trim_end().to_string();
}
nodes.retain(|node| !matches!(node, Node::Text(text, ..) if text.is_empty()));
nodes
}
fn merge(nodes: Vec<Node>) -> Vec<Node> {
let mut out: Vec<Node> = Vec::new();
for node in nodes {
let nodes = match node {
Node::Group(inner) => inner,
other => vec![other],
};
for node in nodes {
if let (
Some(Node::Text(last, last_style, last_change)),
Node::Text(text, style, change),
) = (out.last_mut(), &node)
&& *last_style == *style
&& *last_change == *change
{
last.push_str(text);
continue;
}
out.push(node);
}
}
out
}
fn equation(latex: &str) -> Vec<Node> {
let mut parser = Parser::new(latex);
let mut lines: Vec<Vec<Node>> = vec![Vec::new()];
loop {
let nodes = parser.list(Stops {
rows: true,
..Stops::default()
});
if let Some(line) = lines.last_mut() {
line.extend(nodes);
}
match parser.peek() {
Some(token) if is_command(token, "\\") => {
parser.at += 1;
lines.push(Vec::new());
}
Some(_) => {
parser.at += 1;
if let Some(line) = lines.last_mut() {
line.push(Node::Text("\\end".into(), Style::Math, None));
}
}
None => break,
}
}
let mut lines: Vec<Vec<Node>> = lines.into_iter().map(|line| trimmed(merge(line))).collect();
match lines.len() {
1 => lines.pop().unwrap_or_default(),
_ => vec![Node::Array(lines)],
}
}
struct Writer<'m> {
xml: String,
change: Option<Kind>,
mark: &'m mut dyn FnMut(Kind) -> String,
}
impl Writer<'_> {
fn marked(&mut self, inner: &str, change: Option<Kind>) {
match change {
Some(kind) => {
let tag = (self.mark)(kind);
let name = tag.split_whitespace().next().unwrap_or("w:ins").to_string();
let _ = write!(self.xml, "<{tag}>{inner}</{name}>");
}
None => self.xml.push_str(inner),
}
}
fn control(&mut self) {
self.xml.push_str("<m:ctrlPr>");
self.marked(FONT, self.change);
self.xml.push_str("</m:ctrlPr>");
}
fn properties(&mut self, name: &str, properties: &str) {
let _ = write!(self.xml, "<m:{name}>{properties}");
self.control();
let _ = write!(self.xml, "</m:{name}>");
}
fn run(&mut self, text: &str, style: Style, change: Option<Kind>) {
let properties = match style {
Style::Math => String::new(),
Style::Upright => "<m:rPr><m:sty m:val=\"p\"/></m:rPr>".into(),
Style::Bold => "<m:rPr><m:sty m:val=\"b\"/></m:rPr>".into(),
Style::BoldItalic => "<m:rPr><m:sty m:val=\"bi\"/></m:rPr>".into(),
Style::Text => "<m:rPr><m:nor/></m:rPr>".into(),
Style::Script(script) => format!("<m:rPr><m:scr m:val=\"{script}\"/></m:rPr>"),
};
let space = if text.starts_with(char::is_whitespace) || text.ends_with(char::is_whitespace)
{
" xml:space=\"preserve\""
} else {
""
};
self.xml.push_str("<m:r>");
self.marked(
&format!("{properties}{FONT}<m:t{space}>{}</m:t>", escape(text)),
change.or(self.change),
);
self.xml.push_str("</m:r>");
}
fn argument(&mut self, name: &str, nodes: &[Node]) {
let _ = write!(self.xml, "<m:{name}>");
self.nodes(nodes);
let _ = write!(self.xml, "</m:{name}>");
}
fn nodes(&mut self, nodes: &[Node]) {
for node in nodes {
self.node(node);
}
}
fn node(&mut self, node: &Node) {
match node {
Node::Text(text, style, change) => self.run(text, *style, *change),
Node::Group(nodes) => self.nodes(nodes),
Node::Fraction { num, den, bar } => {
self.xml.push_str("<m:f>");
let kind = if *bar {
""
} else {
"<m:type m:val=\"noBar\"/>"
};
self.properties("fPr", kind);
self.argument("num", num);
self.argument("den", den);
self.xml.push_str("</m:f>");
}
Node::Radical { degree, body } => {
self.xml.push_str("<m:rad>");
let hide = if degree.is_empty() {
"<m:degHide m:val=\"1\"/>"
} else {
""
};
self.properties("radPr", hide);
self.argument("deg", degree);
self.argument("e", body);
self.xml.push_str("</m:rad>");
}
Node::Scripts { base, sub, sup } => {
let name = match (sub, sup) {
(Some(_), Some(_)) => "sSubSup",
(Some(_), None) => "sSub",
_ => "sSup",
};
let _ = write!(self.xml, "<m:{name}>");
self.properties(&format!("{name}Pr"), "");
self.argument("e", base);
if let Some(sub) = sub {
self.argument("sub", sub);
}
if let Some(sup) = sup {
self.argument("sup", sup);
} else if sub.is_none() {
self.argument("sup", &[]);
}
let _ = write!(self.xml, "</m:{name}>");
}
Node::Pre { sub, sup, body } => {
self.xml.push_str("<m:sPre>");
self.properties("sPrePr", "");
self.argument("sub", sub);
self.argument("sup", sup);
self.argument("e", body);
self.xml.push_str("</m:sPre>");
}
Node::Delimiter {
open,
close,
separator,
items,
} => {
self.xml.push_str("<m:d>");
let mut properties = String::new();
if open != "(" {
let _ = write!(properties, "<m:begChr m:val=\"{}\"/>", escape(open));
}
if separator != "|" {
let _ = write!(properties, "<m:sepChr m:val=\"{}\"/>", escape(separator));
}
if close != ")" {
let _ = write!(properties, "<m:endChr m:val=\"{}\"/>", escape(close));
}
self.properties("dPr", &properties);
for item in items {
self.argument("e", item);
}
self.xml.push_str("</m:d>");
}
Node::Function { name, body } => {
self.xml.push_str("<m:func>");
self.properties("funcPr", "");
self.argument("fName", name);
self.argument("e", body);
self.xml.push_str("</m:func>");
}
Node::Nary {
chr,
sub,
sup,
body,
} => {
self.xml.push_str("<m:nary>");
let integral = matches!(chr, '\u{222B}'..='\u{2233}' | '\u{2A0B}'..='\u{2A1C}');
let mut properties = format!(
"<m:chr m:val=\"{chr}\"/><m:limLoc m:val=\"{}\"/>",
if integral { "subSup" } else { "undOvr" }
);
if sub.is_empty() {
properties.push_str("<m:subHide m:val=\"1\"/>");
}
if sup.is_empty() {
properties.push_str("<m:supHide m:val=\"1\"/>");
}
self.properties("naryPr", &properties);
self.argument("sub", sub);
self.argument("sup", sup);
self.argument("e", body);
self.xml.push_str("</m:nary>");
}
Node::Accent { chr, body } => {
self.xml.push_str("<m:acc>");
self.properties("accPr", &format!("<m:chr m:val=\"{chr}\"/>"));
self.argument("e", body);
self.xml.push_str("</m:acc>");
}
Node::Bar { top, body } => {
self.xml.push_str("<m:bar>");
let pos = if *top { "top" } else { "bot" };
self.properties("barPr", &format!("<m:pos m:val=\"{pos}\"/>"));
self.argument("e", body);
self.xml.push_str("</m:bar>");
}
Node::Limit { upper, base, limit } => {
let name = if *upper { "limUpp" } else { "limLow" };
let _ = write!(self.xml, "<m:{name}>");
self.properties(&format!("{name}Pr"), "");
self.argument("e", base);
self.argument("lim", limit);
let _ = write!(self.xml, "</m:{name}>");
}
Node::Brace { top, body } => {
self.xml.push_str("<m:groupChr>");
let properties = if *top {
"<m:chr m:val=\"ā\"/><m:pos m:val=\"top\"/><m:vertJc m:val=\"bot\"/>"
} else {
"<m:chr m:val=\"ā\"/><m:pos m:val=\"bot\"/><m:vertJc m:val=\"top\"/>"
};
self.properties("groupChrPr", properties);
self.argument("e", body);
self.xml.push_str("</m:groupChr>");
}
Node::Matrix(rows) => {
let columns = rows.iter().map(Vec::len).max().unwrap_or(1).max(1);
self.xml.push_str("<m:m>");
self.properties(
"mPr",
&format!(
"<m:mcs><m:mc><m:mcPr><m:count m:val=\"{columns}\"/>\
<m:mcJc m:val=\"center\"/></m:mcPr></m:mc></m:mcs>"
),
);
for row in rows {
self.xml.push_str("<m:mr>");
for at in 0..columns {
self.argument("e", row.get(at).map_or(&[][..], Vec::as_slice));
}
self.xml.push_str("</m:mr>");
}
self.xml.push_str("</m:m>");
}
Node::Boxed { operator, body } => {
self.xml.push_str("<m:box>");
let emulate = if *operator {
"<m:opEmu m:val=\"1\"/>"
} else {
""
};
self.properties("boxPr", emulate);
self.argument("e", body);
self.xml.push_str("</m:box>");
}
Node::Array(lines) => {
self.xml.push_str("<m:eqArr>");
self.properties("eqArrPr", "");
for line in lines {
self.argument("e", line);
}
self.xml.push_str("</m:eqArr>");
}
}
}
}
pub(super) fn omml(
latex: &str,
display: bool,
change: Option<Kind>,
mark: &mut dyn FnMut(Kind) -> String,
) -> String {
let mut writer = Writer {
xml: String::new(),
change,
mark,
};
let namespace = format!(" xmlns:m=\"{M_NS}\"");
if display {
let _ = write!(writer.xml, "<m:oMathPara{namespace}>");
}
let _ = write!(
writer.xml,
"<m:oMath{}>",
if display { "" } else { namespace.as_str() }
);
writer.nodes(&equation(latex));
writer.xml.push_str("</m:oMath>");
if display {
writer.xml.push_str("</m:oMathPara>");
}
writer.xml
}
#[cfg(test)]
mod tests {
use super::*;
fn shape(latex: &str) -> String {
let xml = omml(latex, false, None, &mut |_| String::new());
let mut out = String::new();
let mut rest = xml.as_str();
let mut skip = 0usize;
while let Some(open) = rest.find('<') {
let text = &rest[..open];
if skip == 0 && !text.is_empty() {
out.push_str(text);
}
let close = rest[open..].find('>').map_or(rest.len(), |i| open + i);
let tag = &rest[open + 1..close];
rest = &rest[close + 1..];
let name = tag
.trim_start_matches('/')
.split([' ', '/'])
.next()
.unwrap_or("");
let hidden = name.ends_with("Pr") || name.starts_with("w:");
let closing = tag.starts_with('/');
let empty = tag.ends_with('/');
if hidden {
if !empty {
if closing {
skip = skip.saturating_sub(1);
} else {
skip += 1;
}
}
continue;
}
if skip > 0 || name == "m:t" || name == "m:oMath" {
continue;
}
let short = name.trim_start_matches("m:");
match (closing, empty) {
(true, _) => out.push(')'),
(false, true) => {
let _ = write!(out, "{short}()");
}
(false, false) => {
let _ = write!(out, "{short}(");
}
}
}
out
}
#[test]
fn ams_and_unicode_math_names_are_symbols() {
assert_eq!(shape(r"a\leqslant b"), "r(a⩽b)");
assert_eq!(shape(r"A\lhd B\twoheadrightarrow C"), "r(Aā²Bā C)");
assert_eq!(shape(r"\Box\blacksquare\lozenge"), "r(ā”ā ā)");
assert_eq!(shape(r"a\thinspace b"), "r(a\u{2009}b)");
assert_eq!(shape(r"1+\dotsb+n"), "r(1+āÆ+n)");
}
#[test]
fn more_nary_operators_and_their_limits() {
assert_eq!(shape(r"\iiiint_a^b f"), "nary(sub(r(a))sup(r(b))e(r(f)))");
for (latex, chr, limits) in [
(r"\iiiint_a^b f", 'āØ', "subSup"),
(r"\fint_a^b f", 'āØ', "subSup"),
(r"\ointctrclockwise_C f", 'ā³', "subSup"),
(r"\bigsqcap_i A", 'āØ
', "undOvr"),
(r"\bigtimes_i A", 'āØ', "undOvr"),
] {
let xml = omml(latex, false, None, &mut |_| String::new());
assert!(
xml.contains(&format!("<m:chr m:val=\"{chr}\"/>")),
"{latex}: {xml}"
);
assert!(
xml.contains(&format!("<m:limLoc m:val=\"{limits}\"/>")),
"{latex}: {xml}"
);
}
assert_eq!(nary_command('āØ'), Some("iiiint"));
}
#[test]
fn no_command_is_defined_twice() {
let mut names: Vec<&str> = SYMBOLS.iter().map(|(n, _)| *n).collect();
names.extend(NARY.iter().map(|(n, _)| *n));
names.extend(ACCENTS.iter().map(|(n, _)| *n));
names.extend(FUNCTIONS);
names.sort_unstable();
let twice: Vec<_> = names.windows(2).filter(|w| w[0] == w[1]).collect();
assert!(twice.is_empty(), "{twice:?}");
}
#[test]
fn scripts_take_the_last_character_as_base() {
assert_eq!(shape("A=Ļr^2"), "r(A=Ļ)sSup(e(r(r))sup(r(2)))");
assert_eq!(shape("x_i^2"), "sSubSup(e(r(x))sub(r(i))sup(r(2)))");
assert_eq!(shape("{(x+a)}^n"), "sSup(e(d(e(r(x+a))))sup(r(n)))");
}
#[test]
fn balanced_brackets_build_a_delimiter_and_strays_stay_text() {
assert_eq!(shape("f(x)"), "r(f)d(e(r(x)))");
assert_eq!(shape("(a]"), "r((a])");
assert_eq!(shape(r"\{a\}"), "d(e(r(a)))");
assert_eq!(shape(r"\lparen a\rparen"), "r((a))");
}
#[test]
fn structures_come_back_as_word_writes_them() {
assert_eq!(
shape(r"\frac{-b±\sqrt{b^2-4ac}}{2a}"),
"f(num(r(-b±)rad(deg()e(sSup(e(r(b))sup(r(2)))r(-4ac))))den(r(2a)))"
);
assert_eq!(
shape(r"\sum_{k=0}^n \binom{n}{k}"),
"nary(sub(r(k=0))sup(r(n))e(d(e(f(num(r(n))den(r(k)))))))"
);
assert_eq!(
shape(r"\lim_{nāā}{a}"),
"func(fName(limLow(e(r(lim))lim(r(nāā))))e(r(a)))"
);
assert_eq!(
shape(r"\sin^2{x}"),
"func(fName(sSup(e(r(sin))sup(r(2))))e(r(x)))"
);
assert_eq!(shape(r"\left[ a \middle| b \right)"), "d(e(r(a))e(r(b)))");
assert_eq!(
shape(r"\begin{matrix}1 & 0 \\ 0 & 1\end{matrix}"),
"m(mr(e(r(1))e(r(0)))mr(e(r(0))e(r(1))))"
);
assert_eq!(
shape(r"\underbrace{x+y}_{n}"),
"limLow(e(groupChr(e(r(x+y))))lim(r(n)))"
);
assert_eq!(
shape(r"\prescript{a}{b}{X}"),
"sPre(sub(r(a))sup(r(b))e(r(X)))"
);
}
#[test]
fn an_nary_takes_one_operand_with_its_scripts() {
assert_eq!(
shape(r"\sum_{i=1}^n x_i+1"),
"nary(sub(r(i=1))sup(r(n))e(sSub(e(r(x))sub(r(i)))))r(+1)"
);
}
#[test]
fn an_unknown_command_stays_as_text() {
assert_eq!(shape(r"\foo x"), r"r(\foo x)");
assert_eq!(shape(r"\alpha+\beta"), "r(α+β)");
assert_eq!(shape(r"\foo{x}"), r"r(\foo{x})");
}
#[test]
fn primes_are_a_superscript_as_word_builds_them() {
assert_eq!(shape("f'(x)"), "sSup(e(r(f))sup(r(ā²)))d(e(r(x)))");
assert_eq!(shape("f''"), "sSup(e(r(f))sup(r(ā³)))");
assert_eq!(shape("f'''"), "sSup(e(r(f))sup(r(ā“)))");
assert_eq!(shape("f'^2"), "sSup(e(r(f))sup(r(ā²2)))");
assert_eq!(shape("'a"), "r('a)");
}
#[test]
fn not_negates_the_next_relation() {
assert_eq!(shape(r"a \not\in B"), shape(r"a \notin B"));
assert_eq!(shape(r"a \not= b"), shape("a ā b"));
assert_eq!(shape(r"\not\subset"), "r(ā)");
assert_eq!(shape(r"\not\perp"), "r(ā„\u{338})");
}
#[test]
fn sized_delimiters_are_delimiters() {
assert_eq!(shape(r"\bigl( x \bigr)"), shape("( x )"));
assert_eq!(shape(r"\Bigl[ y \Bigr]"), shape("[ y ]"));
assert_eq!(shape(r"\biggl\{ z \biggr\}"), shape(r"\{ z \}"));
}
#[test]
fn nesting_past_the_limit_reads_as_text_instead_of_overflowing() {
let deep = format!("{}x{}", "{".repeat(50_000), "}".repeat(50_000));
let _ = shape(&deep);
let fracs = format!("{}x{}", r"\frac{".repeat(20_000), "}{y}".repeat(20_000));
let _ = shape(&fracs);
let scripts = format!("x{}y{}", "^{x".repeat(50_000), "}".repeat(50_000));
let _ = shape(&scripts);
let primes = format!("x{}y{}", "'^{x".repeat(50_000), "}".repeat(50_000));
let _ = shape(&primes);
}
#[test]
fn scripts_within_the_limit_still_nest() {
let nested = format!("x{}y{}", "^{x".repeat(8), "}".repeat(8));
assert!(!shape(&nested).contains("^{"), "{}", shape(&nested));
}
#[test]
fn an_array_is_a_matrix_without_its_column_spec() {
assert_eq!(
shape(r"\begin{array}{cc} a & b \\ c & d \end{array}"),
shape(r"\begin{matrix} a & b \\ c & d \end{matrix}")
);
assert_eq!(
shape(r"\begin{array} a & b \\ c & d\end{array}"),
shape(r"\begin{matrix} a & b \\ c & d \end{matrix}")
);
}
#[test]
fn modulo_and_spacing_commands() {
assert_eq!(shape(r"a \bmod n"), "r(a )r(mod)r( n)");
assert_eq!(shape(r"a \pmod{n}"), "r(a )d(e(r(mod)r( n)))");
assert_eq!(shape(r"x\hspace{1em}y"), "r(x y)");
}
#[test]
fn a_change_marks_every_run_and_control_inside() {
let mut next = 0;
let xml = omml("x^2", true, Some(Kind::Insert), &mut |_| {
next += 1;
format!("w:ins w:id=\"{next}\" w:author=\"A\"")
});
assert!(xml.starts_with("<m:oMathPara xmlns:m="), "{xml}");
assert_eq!(xml.matches("<w:ins ").count(), 3, "{xml}");
assert!(xml.contains("<m:ctrlPr><w:ins w:id=\"1\""), "{xml}");
assert!(!xml.contains("w:delText"), "{xml}");
}
#[test]
fn a_change_inside_marks_only_its_runs() {
let latex = format!("A={DELETE_START}Ļ{DELETE_END}{INSERT_START}2{INSERT_END}r^2");
let xml = omml(&latex, false, None, &mut |kind| match kind {
Kind::Insert => "w:ins w:id=\"1\"".into(),
Kind::Delete => "w:del w:id=\"2\"".into(),
});
assert!(xml.contains("<m:r><w:del w:id=\"2\">"), "{xml}");
assert!(xml.contains("<m:t>Ļ</m:t></w:del></m:r>"), "{xml}");
assert!(xml.contains("<m:t>2</m:t></w:ins></m:r>"), "{xml}");
assert_eq!(xml.matches("<w:ins ").count(), 1, "{xml}");
assert_eq!(xml.matches("<w:del ").count(), 1, "{xml}");
}
#[test]
fn display_lines_are_an_equation_array() {
assert_eq!(shape(r"a \\ b"), "eqArr(e(r(a))e(r(b)))");
let xml = omml(r"a \\ b", true, None, &mut |_| String::new());
assert_eq!(xml.matches("<m:oMath>").count(), 1, "{xml}");
}
#[test]
fn every_construct_and_its_malformed_forms_build_a_shape() {
assert_eq!(
shape("\\operatorname*{argmax}_x f"),
"func(fName(limLow(e(r(argmax))lim(r(x))))e(r(f)))"
);
assert_eq!(shape("\\sqrt[3]{x}"), "rad(deg(r(3))e(r(x)))");
assert_eq!(shape("\\sqrt{x}"), "rad(deg()e(r(x)))");
assert_eq!(shape("\\sqrt[3"), "rad(deg()e(r([)))r(3)");
assert_eq!(shape("a]"), "r(a])");
assert_eq!(shape("(a"), "r((a)");
assert_eq!(shape("{(}"), "r(()");
assert_eq!(shape("(a & b)"), "r((a & b))");
assert_eq!(shape("(a \\\\ b)"), "eqArr(e(r((a))e(r(b))))");
assert_eq!(shape("\\left( a \\right)"), "d(e(r(a)))");
assert_eq!(shape("\\left( a"), "d(e(r(a)))");
assert_eq!(shape("\\left"), "d(e())");
assert_eq!(shape("\\left( a \\middle/ b \\right)"), "d(e(r(a))e(r(b)))");
assert_eq!(shape("\\left( a \\middle| b \\right."), "d(e(r(a))e(r(b)))");
assert_eq!(shape("f'_i"), "sSubSup(e(r(f))sub(r(i))sup(r(ā²)))");
assert_eq!(shape("f''^2_j"), "sSubSup(e(r(f))sub(r(j))sup(r(ā³2)))");
assert_eq!(shape("\\frac{a}{b"), "f(num(r(a))den(r(b)))");
assert_eq!(shape("\\frac"), "f(num()den())");
assert_eq!(
shape("\\text{a {b} c \\alpha d \\{ x^y_z & \\\\}"),
"r(a {b} c αd { x^y_z & \\\\)"
);
assert_eq!(shape("\\text x"), "r(x)");
assert_eq!(shape("\\text{"), "");
assert_eq!(shape("x_1_2"), "sSub(e(sSub(e(r(x))sub(r(1))))sub(r(2)))");
assert_eq!(shape("x^1^2"), "sSup(e(sSup(e(r(x))sup(r(1))))sup(r(2)))");
assert_eq!(
shape("\\sum\\limits_0^n x"),
"nary(sub(r(0))sup(r(n))e(r(x)))"
);
assert_eq!(shape("\\sum^n_0 x"), "nary(sub(r(0))sup(r(n))e(r(x)))");
assert_eq!(shape("{\\sum}"), "nary(sub()sup()e())");
assert_eq!(shape("\\sum_0"), "nary(sub(r(0))sup()e())");
assert_eq!(
shape("\\int x^2"),
"nary(sub()sup()e(sSup(e(r(x))sup(r(2)))))"
);
assert_eq!(
shape("\\sum{a}_i"),
"nary(sub()sup()e(sSub(e(r(a))sub(r(i)))))"
);
assert_eq!(shape("\\sum{a}"), "nary(sub()sup()e(r(a)))");
assert_eq!(
shape("\\int \\alpha_1"),
"nary(sub()sup()e(sSub(e(r(α))sub(r(1)))))"
);
assert_eq!(shape("\\genfrac{}{}{0pt}{}{a}{b}"), "f(num(r(a))den(r(b)))");
assert_eq!(
shape("\\genfrac(]{1pt}{}{a}{b}"),
"d(e(f(num(r(a))den(r(b)))))"
);
assert_eq!(
shape("\\genfrac({}{}{}{a}{b}"),
"d(e(f(num(r(a))den(r(b)))))"
);
assert_eq!(
shape("\\genfrac{}]{}{}{a}{b}"),
"d(e(f(num(r(a))den(r(b)))))"
);
assert_eq!(shape("\\{ a \\}"), "d(e(r( a )))");
assert_eq!(shape("\\{ a"), "r({ a)");
assert_eq!(
shape("\\overbrace{x}^{n}"),
"limUpp(e(groupChr(e(r(x))))lim(r(n)))"
);
assert_eq!(shape("\\overbrace{x}"), "groupChr(e(r(x)))");
assert_eq!(
shape("\\underbrace{x}_{n}"),
"limLow(e(groupChr(e(r(x))))lim(r(n)))"
);
assert_eq!(shape("\\underbrace{x}"), "groupChr(e(r(x)))");
assert_eq!(shape("\\not="), "r(ā )");
assert_eq!(shape("\\not\\in"), "r(ā)");
assert_eq!(shape("\\not \\in"), "r(ā)");
assert_eq!(shape("\\not\\foo"), "r(\\not\\foo)");
assert_eq!(shape("\\not"), "r(\\not)");
assert_eq!(shape("\\not\\|"), "r(\\notā)");
assert_eq!(shape("\\not\\{"), "r(\\not{)");
assert_eq!(shape("\\not\\le x"), "r(ā°x)");
assert_eq!(
shape("\\begin{matrix} a & b \\\\ c \\end{matrix}"),
"m(mr(e(r(a))e(r(b)))mr(e(r(c))e()))"
);
assert_eq!(shape("\\begin{pmatrix}a"), "d(e(m(mr(e(r(a))))))");
assert_eq!(
shape("\\begin{array}{cc} a&b \\end{array}"),
"m(mr(e(r(a))e(r(b))))"
);
assert_eq!(
shape("\\begin{array} a&b \\end{array}"),
"m(mr(e(r(a))e(r(b))))"
);
assert_eq!(
shape("\\begin{cases} a & x \\\\ b \\end{cases}"),
"d(e(eqArr(e(r(a & x))e(r(b)))))"
);
assert_eq!(
shape("\\begin{aligned} a &= b \\\\ c &= d \\end{aligned}"),
"eqArr(e(r(a &= b))e(r(c &= d)))"
);
assert_eq!(
shape("\\begin{Vmatrix}a\\end{Vmatrix}"),
"d(e(m(mr(e(r(a))))))"
);
assert_eq!(
shape("\\begin{vmatrix}a\\end{vmatrix}"),
"d(e(m(mr(e(r(a))))))"
);
assert_eq!(
shape("\\begin{Bmatrix}a\\end{Bmatrix}"),
"d(e(m(mr(e(r(a))))))"
);
assert_eq!(
shape("\\begin{bmatrix}a\\end{bmatrix}"),
"d(e(m(mr(e(r(a))))))"
);
assert_eq!(shape("a \\\\ b \\\\"), "eqArr(e(r(a))e(r(b))e())");
assert_eq!(shape("\\\\ a"), "eqArr(e()e(r(a)))");
assert_eq!(shape("a \\end b"), "r(a \\end b)");
assert_eq!(
shape("\\overline{x}\\underline{y}"),
"bar(e(r(x)))bar(e(r(y)))"
);
assert_eq!(shape("\\mathbf{x}\\mathsf{y}\\mathtt{z}"), "r(x)r(y)r(z)");
assert_eq!(shape("\\bigl( a \\bigr)"), "d(e(r( a )))");
assert_eq!(shape("\\displaystyle x"), "r(x)");
assert_eq!(shape("\\bmod"), "r(mod)");
assert_eq!(shape("\\pmod{n}"), "d(e(r(mod)r( n)))");
assert_eq!(
shape("\\prescript{a}{b}{X}"),
"sPre(sub(r(a))sup(r(b))e(r(X)))"
);
assert_eq!(shape("\\hat x \\vec{y}"), "acc(e(r(x)))r( )acc(e(r(y)))");
assert_eq!(
shape("\\lim_{n\\to\\infty} a_n"),
"func(fName(limLow(e(r(lim))lim(r(nāā))))e(sSub(e(r(a))sub(r(n)))))"
);
assert_eq!(shape("\\sin x"), "func(fName(r(sin))e(r(x)))");
assert_eq!(shape("\\boxed{x}"), "r(\\boxed{x})");
assert_eq!(shape("\\mathop{x}"), "box(e(r(x)))");
assert_eq!(shape("_a"), "sSub(e()sub(r(a)))");
assert_eq!(shape("^b"), "sSup(e()sup(r(b)))");
assert_eq!(shape("'a"), "r('a)");
assert_eq!(shape("x'"), "sSup(e(r(x))sup(r(ā²)))");
assert_eq!(shape("\\\\"), "eqArr(e()e())");
assert_eq!(shape("\\"), "r(\\)");
assert_eq!(shape("\\*"), "r(\\*)");
assert_eq!(shape("\\hspace{1em}x"), "r(x)");
assert_eq!(shape("\\stackrel{a}{=}"), "limUpp(e(r(=))lim(r(a)))");
assert_eq!(shape("\\underset{a}{b}"), "limLow(e(r(b))lim(r(a)))");
assert_eq!(shape("\\overset{a}{b}"), "limUpp(e(r(b))lim(r(a)))");
assert_eq!(shape("\\binom{n}{k}"), "d(e(f(num(r(n))den(r(k)))))");
assert_eq!(shape("\\tbinom"), "d(e(f(num()den())))");
assert_eq!(shape("\\cfrac{1}{2}"), "f(num(r(1))den(r(2)))");
}
#[test]
fn nesting_past_the_limit_reads_as_text() {
let deep = format!("{}x{}", "{".repeat(70), "}".repeat(70));
let shape_deep = shape(&deep);
assert!(shape_deep.starts_with("r({{"), "{shape_deep}");
assert!(shape_deep.ends_with("}})"), "{shape_deep}");
let open = shape(&format!("{}x", "{".repeat(70)));
assert!(open.starts_with("r({") && open.ends_with("x)"), "{open}");
}
#[test]
fn changes_inside_arguments_keep_their_text() {
let mut mark = |kind: Kind| {
format!(
"w:{} w:id=\"9\"",
if kind == Kind::Insert { "ins" } else { "del" }
)
};
for latex in [
format!(r"\frac{{{INSERT_START}a{INSERT_END}}}{{b}}"),
format!(r"x^{INSERT_START}{{2}}{INSERT_END}"),
] {
let xml = omml(&latex, false, None, &mut mark);
assert!(xml.contains("<w:ins w:id=\"9\">"), "{latex}: {xml}");
}
let xml = omml(
&format!(r"\text{{a{DELETE_START}b{DELETE_END}c}}"),
false,
None,
&mut mark,
);
let text: String = xml
.split("<m:t>")
.skip(1)
.filter_map(|piece| piece.split_once("</m:t>").map(|(t, _)| t))
.collect();
assert_eq!(text, "abc", "{xml}");
}
}