mathml_core/helpers/
mod.rs1#![allow(non_snake_case)]
2#![allow(unused_variables)]
3#![doc = include_str!("readme.md")]
4mod matrix;
5pub use self::matrix::*;
6use crate::{
7 blocks::MathStyle, LineThickness, MathElement, MathFenced, MathFraction, MathML, MathMultiScript, MathRow, MathTable,
8};
9
10#[doc = include_str!("isotope.xml")]
20pub fn frac<N, D>(numerator: N, denominator: D) -> MathML
21where
22 N: Into<MathML>,
23 D: Into<MathML>,
24{
25 MathML::fraction(numerator, denominator)
26}
27
28#[doc = include_str!("isotope.xml")]
38pub fn dfrac<N, D>(numerator: N, denominator: D) -> MathML
39where
40 N: Into<MathML>,
41 D: Into<MathML>,
42{
43 MathStyle::display(frac(numerator, denominator)).into()
44}
45
46#[doc = include_str!("isotope.xml")]
56pub fn cfrac(numerator: MathML, denominator: MathML) -> MathML {
57 todo!()
58}
59
60#[doc = include_str!("isotope.xml")]
71pub fn binom<N, D>(numerator: N, denominator: D) -> MathML
72where
73 N: Into<MathML>,
74 D: Into<MathML>,
75{
76 MathFraction::new(numerator.into(), denominator.into()).with_thickness(LineThickness::Length(0)).into()
77}
78
79#[doc = include_str!("isotope.xml")]
90pub fn cbinom<N, D>(numerator: N, denominator: D) -> MathML
91where
92 N: Into<MathML>,
93 D: Into<MathML>,
94{
95 MathMultiScript::sub_super_script(MathML::identifier("C"), numerator.into(), denominator.into()).into()
96}
97
98#[doc = include_str!("isotope.xml")]
109pub fn legendre_symbols<N, D>(numerator: N, denominator: D) -> MathFenced
110where
111 N: Into<MathML>,
112 D: Into<MathML>,
113{
114 MathFenced::new(vec![MathFraction::new(numerator.into(), denominator.into()).into()], '(', ')')
115}
116
117#[doc = include_str!("isotope.xml")]
132pub fn isotope(symbol: &str, mass_number: usize, atomic_number: Option<usize>) -> MathMultiScript {
133 let ld = match atomic_number {
134 Some(s) => vec![s.into()],
135 None => vec![],
136 };
137 MathMultiScript::new(MathML::identifier(symbol), vec![MathML::number(mass_number)], ld, vec![], vec![])
138}
139
140#[inline(always)]
142pub fn safe_html_char<W>(writer: &mut W, c: char) -> std::fmt::Result
143where
144 W: std::fmt::Write,
145{
146 match c {
147 '<' => writer.write_str("<"),
148 '>' => writer.write_str(">"),
149 '&' => writer.write_str("&"),
150 '"' => writer.write_str("""),
151 '\'' => writer.write_str("'"),
152 _ => writer.write_char(c),
153 }
154}
155
156#[inline(always)]
158pub fn safe_html_str<W>(writer: &mut W, s: &str) -> std::fmt::Result
159where
160 W: std::fmt::Write,
161{
162 for c in s.chars() {
163 safe_html_char(writer, c)?;
164 }
165 Ok(())
166}
167
168#[track_caller]
170pub fn assert_no_ws(source: &MathML, target: &str) {
171 let text = source.to_string();
172 assert_eq!(remove_ws(&text), remove_ws(target));
173}
174
175#[inline]
177fn remove_ws(string: &str) -> String {
178 let mut refined = String::with_capacity(string.len());
179 for char in string.chars() {
180 if char.is_whitespace() {
181 continue;
182 }
183 refined.push(char);
184 }
185 refined
186}