Skip to content

Commit

Permalink
reverting commits
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin committed Feb 26, 2024
1 parent 2bdd115 commit 47c606d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 52 deletions.
33 changes: 1 addition & 32 deletions skema/skema-rs/mathml/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl fmt::Display for MathExpression {
MathExpression::Msub(base, subscript) => {
write!(f, "{base}_{{{subscript}}}")
}
MathExpression::Msubsup(base, subscript, superscript) => {
MathExpression::Msubsup(base, subscript,superscript)=>{
write!(f, "{base}_{{{subscript}}}^{{{superscript}}}")
}
MathExpression::Mo(op) => {
Expand All @@ -222,37 +222,6 @@ impl fmt::Display for MathExpression {
write!(f, "{:?}", base)?;
write!(f, "{superscript:?}")
}
MathExpression::Mtext(text) => write!(f, "{}", text),
MathExpression::SummationMath(SummationMath { op, func }) => {
write!(f, "{op}")?;
write!(f, "{func}")
}
MathExpression::HatComp(HatComp { op, comp }) => {
write!(f, "{op}")?;
write!(f, "{comp}")
}
MathExpression::Integral(Integral {
op,
integrand,
integration_variable,
}) => {
write!(f, "{op}")?;
write!(f, "{integrand}")?;
write!(f, "{integration_variable}")
}
MathExpression::LaplacianComp(LaplacianComp { op, comp }) => {
write!(f, "{op}(")?;
write!(f, "{comp})")
}
MathExpression::SurfaceIntegral(row) => {
write!(f, "{row})")
}
MathExpression::DownArrow(DownArrow { sub, sup, comp }) => match (sub, sup) {
(Some(low), Some(up)) => write!(f, "{comp}↓_{{{low}}}^{{{up}}}"),
(Some(low), None) => write!(f, "{comp}↓_{{{low}}}"),
(None, Some(up)) => write!(f, "{comp}↓^{{{up}}}"),
(None, None) => write!(f, "{comp}↓"),
},
expression => write!(f, "{expression:?}"),
}
}
Expand Down
3 changes: 2 additions & 1 deletion skema/skema-rs/mathml/src/parsers/generic_mathml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ pub fn grad(input: Span) -> IResult<Operator> {
let (s, op) = value(Operator::Grad, alt((ws(tag("∇")), ws(tag("&#x2207;")))))(input)?;
Ok((s, op))
}

pub fn dot(input: Span) -> IResult<Operator> {
let (s, op) = value(Operator::Dot, alt((ws(tag("⋅")), ws(tag("&#x22c5;")))))(input)?;
Ok((s, op))
Expand Down Expand Up @@ -365,7 +366,7 @@ pub fn msubsup(input: Span) -> IResult<MathExpression> {
}

//Text
pub fn mtext(input: Span) -> IResult<MathExpression> {
fn mtext(input: Span) -> IResult<MathExpression> {
let (s, element) = elem0!("mtext")(input)?;
Ok((s, Mtext(element.trim().to_string())))
}
Expand Down
42 changes: 27 additions & 15 deletions skema/skema-rs/mathml/src/parsers/interpreted_mathml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use crate::{
SummationMath, Type,
},
parsers::generic_mathml::{
add, attribute, cross, divide, dot, elem_many0, equals, etag, lparen, mean, mi, mn, msub,
msubsup, mtext, multiply, rparen, stag, subtract, tag_parser, vector, ws, xml_declaration,
IResult, ParseError, Span,
add, attribute, dot, elem_many0, equals, etag, grad, lparen, mean, mi, mn, msqrt, msub,
msubsup, multiply, rparen, stag, subtract, tag_parser, ws, xml_declaration, IResult,
ParseError, Span,
},
};

Expand Down Expand Up @@ -1725,18 +1725,30 @@ pub fn math_expression(input: Span) -> IResult<MathExpression> {
func_of: None,
})
}),
alt((
absolute,
sqrt,
map(operator, MathExpression::Mo),
map(gradient, MathExpression::Mo),
mn,
msub,
superscript,
mfrac,
mtext,
over_term,
)),
map(
grad_func,
|(
op,
Ci {
r#type,
content,
func_of,
},
)| {
MathExpression::Differential(Differential {
diff: Box::new(MathExpression::Mo(op)),
func: Box::new(MathExpression::Ci(Ci {
r#type,
content,
func_of,
})),
})
},
),
absolute,
map(operator, MathExpression::Mo),
map(gradient, MathExpression::Mo),
alt((mn, msub, superscript, msqrt, mfrac, over_term)),
map(mrow, MathExpression::Mrow),
msubsup,
)))(input)
Expand Down
58 changes: 54 additions & 4 deletions skema/skema-rs/mathml/src/parsers/math_expression_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,14 @@ pub fn preprocess_mathml_for_to_latex(input: &str) -> String {
.replace_all(&no_newlines, "><")
.to_string();

no_spaces.to_string()
let new_no_spaces = no_spaces.replace(" ", "");

// Replace <mi>∇</mi> with <mo>∇</mo>
let replaced_str = new_no_spaces
.replace(r#"<mi>∇</mi>"#, "<mo>∇</mo>")
.to_string();

replaced_str
}

#[test]
Expand Down Expand Up @@ -2391,18 +2398,61 @@ fn new_test_halfar_whitespace() {
";
let exp = input.parse::<MathExpressionTree>().unwrap();
let s_exp = exp.to_string();
println!("s_exp={:?}", s_exp);
assert_eq!(
s_exp,
"(= t_{0} (* (* (/ 1 (* 18 Γ)) (^ (/ 7 4) 3)) (/ R_{0}^{4} H_{0}^{7})))"
);
}

#[test]
fn test_equation_with_mtext() {
let input = "<math><msub><mrow><mi mathvariant=\"script\">L</mi></mrow><mrow><mtext>reg</mtext></mrow></msub><mo>=</mo><msub><mrow><mi mathvariant=\"script\">L</mi></mrow><mrow><mi>d</mi><mn>1</mn></mrow></msub><mo>+</mo><msub><mrow><mi mathvariant=\"script\">L</mi></mrow><mrow><mi>d</mi><mn>2</mn></mrow></msub></math>";
fn new_test_halfar_whitespace() {
let input = "
<math>
<msub>
<mi>t</mi>
<mn>0</mn>
</msub>
<mo>=</mo>
<mfrac>
<mn>1</mn>
<mrow>
<mn>18</mn>
<mi>&#x0393;</mi>
</mrow>
</mfrac>
<msup>
<mrow>
<mo>(</mo>
<mfrac>
<mn>7</mn>
<mn>4</mn>
</mfrac>
<mo>)</mo>
</mrow>
<mn>3</mn>
</msup>
<mfrac>
<msubsup>
<mi>R</mi>
<mn>0</mn>
<mn>4</mn>
</msubsup>
<msubsup>
<mi>H</mi>
<mn>0</mn>
<mn>7</mn>
</msubsup>
</mfrac>
</math>
";
let exp = input.parse::<MathExpressionTree>().unwrap();
let s_exp = exp.to_string();
assert_eq!(s_exp, "(= L_{reg} (+ L_{d1} L_{d2}))");
println!("s_exp={:?}", s_exp);
assert_eq!(
s_exp,
"(= t_{0} (* (* (/ 1 (* 18 Γ)) (^ (/ 7 4) 3)) (/ R_{0}^{4} H_{0}^{7})))"
);
}

#[test]
Expand Down

0 comments on commit 47c606d

Please sign in to comment.