mathml_core/blocks/
display.rs

1use super::*;
2use crate::{
3    traits::{write_tag_close, write_tag_start},
4    MathOperator, MathSpace,
5};
6
7impl Display for MathRoot {
8    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
9        write_tag_start(f, self)?;
10        for child in &self.children {
11            write!(f, "{}", child)?;
12        }
13        write_tag_close(f, self)
14    }
15}
16
17impl Display for MathPhantom {
18    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
19        write!(f, "<mphantom>")?;
20        write!(f, "{}", self.inner)?;
21        write!(f, "</mphantom>")
22    }
23}
24impl Display for MathStyle {
25    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
26        write_tag_start(f, self)?;
27        write!(f, "{}", self.base)?;
28        write_tag_close(f, self)
29    }
30}
31impl Display for MathRow {
32    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
33        f.write_str("<mrow>")?;
34        for child in &self.children {
35            write!(f, "{}", child)?;
36        }
37        f.write_str("</mrow>")
38    }
39}
40
41impl Display for MathFunction {
42    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
43        let identifier = MathIdentifier::normal(&self.name);
44        if self.body.is_empty() {
45            write!(f, "{}", identifier)
46        }
47        else {
48            let mut row = MathRow::new(vec![
49                identifier.into(),
50                MathOperator::new("⁡".to_string()).into(),
51                MathSpace::new(1.0 / 6.0).into(),
52            ]);
53            row.mut_items().extend(self.body.iter().cloned());
54            write!(f, "{}", row)
55        }
56    }
57}
58
59impl Display for MathTable {
60    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
61        write_tag_start(f, self)?;
62        f.write_str("<mtr><mtd>")?;
63        for (i, node) in self.stream.iter().enumerate() {
64            match node {
65                MathML::NewLine => {
66                    f.write_str("</mtd></mtr>")?;
67                    if i < self.stream.len() {
68                        f.write_str("<mtr><mtd>")?;
69                    }
70                }
71                MathML::Ampersand => {
72                    f.write_str("</mtd>")?;
73                    if i < self.stream.len() {
74                        f.write_str("<mtd>")?;
75                    }
76                }
77                _ => {
78                    write!(f, "{}", node)?;
79                }
80            }
81        }
82        f.write_str("</mtd></mtr>")?;
83        write_tag_close(f, self)
84    }
85}