Skip to content

Instantly share code, notes, and snippets.

@herabit
Created April 29, 2024 22:28
Show Gist options
  • Save herabit/61ca317fd4730507112539a477dbe250 to your computer and use it in GitHub Desktop.
Save herabit/61ca317fd4730507112539a477dbe250 to your computer and use it in GitHub Desktop.
There's probably a better way of doing this, though, I am stubborn and refuse to find out, this is good enough. Probably some bugs I have yet to encounter due to my arguably, nonexistent testing.
macro_rules! mathml_impl {
() => {
""
};
($ident:ident $($tt:tt)*) => {
::core::concat!(
"<mi>",
::core::stringify!($ident),
"</mi>",
$crate::macros::mathml_impl!($($tt)*)
)
};
// This is a workaround for issues relating to parsing unary negations.
(~ $term:tt $($tt:tt)*) => {
::core::concat!(
"<mrow>",
"<mo>-</mo>",
$crate::macros::mathml_impl!($term),
"</mrow>",
$crate::macros::mathml_impl!($($tt)*)
)
};
($num:literal $($tt:tt)*) => {
::core::concat!(
"<mn>",
::core::stringify!($num),
"</mn>",
$crate::macros::mathml_impl!($($tt)*)
)
};
(* $($tt:tt)*) => {
::core::concat!(
"<mo>&times;</mo>",
$crate::macros::mathml_impl!($($tt)*)
)
};
($term:tt _^ $sub:tt $sup:tt $($tt:tt)*) => {
::core::concat!(
"<msubsup>",
$crate::macros::mathml_impl!($term),
$crate::macros::mathml_impl!($sub),
$crate::macros::mathml_impl!($sup),
"</msubsup>",
$crate::macros::mathml_impl!($($tt)*)
)
};
($term:tt ^ $sup:tt $($tt:tt)*) => {
::core::concat!(
"<msup>",
$crate::macros::mathml_impl!($term),
$crate::macros::mathml_impl!($sup),
"</msup>",
$crate::macros::mathml_impl!($($tt)*)
)
};
($term:tt _ $sub:tt $($tt:tt)*) => {
::core::concat!(
"<msub>",
$crate::macros::mathml_impl!($term),
$crate::macros::mathml_impl!($sub),
"</msub>",
$crate::macros::mathml_impl!($($tt)*)
)
};
(( $($inner:tt)* ) $($tt:tt)*) => {
::core::concat!(
"<mrow>",
"<mo>", "(", "</mo>",
$crate::macros::mathml_impl!($($inner)*),
"<mo>", ")", "</mo>",
"</mrow>",
$crate::macros::mathml_impl!($($tt)*)
)
};
([ $($inner:tt)* ] $($tt:tt)*) => {
::core::concat!(
"<mrow>",
$crate::macros::mathml_impl!($($inner)*),
"</mrow>",
$crate::macros::mathml_impl!($($tt)*)
)
};
($op:tt $($tt:tt)*) => {
::core::concat!(
"<mo>",
::core::stringify!($op),
"</mo>",
$crate::macros::mathml_impl!($($tt)*)
)
};
}
macro_rules! mathml {
($($tt:tt)*) => {
::core::concat!(
r#"<math xmlns="http://www.w3.org/1998/Math/MathML">"#,
$crate::macros::mathml_impl!($($tt)*),
"</math>"
)
};
}
pub(crate) use mathml;
pub(crate) use mathml_impl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment