use crate::build_mathml;
use crate::context::KatexContext;
use crate::define_function::{FunctionDefSpec, FunctionPropSpec};
use crate::mathml_tree::{MathDomNode, MathNode, MathNodeType};
use crate::options::Options;
use crate::parser::parse_node::{AnyParseNode, NodeType};
use crate::types::{ParseError, ParseErrorKind};
pub fn define_tag(ctx: &mut KatexContext) {
ctx.define_function(FunctionDefSpec {
node_type: Some(NodeType::Tag),
names: &["\\tag"],
props: FunctionPropSpec::default(),
handler: None,
html_builder: None, mathml_builder: Some(mathml_builder),
});
}
fn mathml_builder(
node: &AnyParseNode,
options: &Options,
ctx: &KatexContext,
) -> Result<MathDomNode, ParseError> {
let AnyParseNode::Tag(group) = node else {
return Err(ParseError::new(ParseErrorKind::ExpectedNode {
node: NodeType::Tag,
}));
};
let mut pad = MathNode::builder().node_type(MathNodeType::Mtd).build();
pad.attributes.insert("width".to_owned(), "50%".to_owned());
let body = build_mathml::build_expression_row(ctx, &group.body, options, None)?;
let tag = build_mathml::build_expression_row(ctx, &group.tag, options, None)?;
let body_cell = MathNode::builder()
.node_type(MathNodeType::Mtd)
.children(vec![body])
.build();
let tag_cell = MathNode::builder()
.node_type(MathNodeType::Mtd)
.children(vec![tag])
.build();
let row = MathNode::builder()
.node_type(MathNodeType::Mtr)
.children(vec![
MathDomNode::Math(pad.clone()),
MathDomNode::Math(body_cell),
MathDomNode::Math(pad),
MathDomNode::Math(tag_cell),
])
.build();
let mut table = MathNode::builder()
.node_type(MathNodeType::Mtable)
.children(vec![MathDomNode::Math(row)])
.build();
table
.attributes
.insert("width".to_owned(), "100%".to_owned());
Ok(MathDomNode::Math(table))
}