Skip to main content

convert_math

Function convert_math 

Source
pub fn convert_math(html: &str) -> String
Expand description

Convert LaTeX math spans inside an HTML fragment to MathML.

Two delimiter styles are recognised, in this order:

  • $$...$$<math display="block">…</math>
  • $...$<math>…</math> (inline)

The matchers are deliberately conservative: a $ immediately followed by a digit is not treated as math (so $5 and $2.50 in prose stay literal), and an inline match must have non-space content. Unbalanced $ is left as-is.

Fast-path: returns immediately when the input contains no $ character.

§Examples

Block math:

use html_generator::math::convert_math;

let out = convert_math("<p>$$x + y$$</p>");
assert!(out.contains(r#"display="block""#));
assert!(out.contains("<math"));

Inline math:

use html_generator::math::convert_math;

let out = convert_math(r"<p>Pythagoras: $a^2 + b^2 = c^2$.</p>");
assert!(out.contains("<math"));
// Inline form is the default; no `display="block"`:
assert!(!out.contains(r#"display="block""#));

Plain prose with $ is not touched:

use html_generator::math::convert_math;

let out = convert_math("<p>That cost $5.</p>");
assert_eq!(out, "<p>That cost $5.</p>");