Skip to main content

ratex_parser/functions/
htmlmathml.rs

1use std::collections::HashMap;
2
3use crate::error::{ParseError, ParseResult};
4use crate::functions::{define_function_full, ArgType, FunctionContext, FunctionSpec};
5use crate::parse_node::ParseNode;
6
7pub fn register(map: &mut HashMap<&'static str, FunctionSpec>) {
8    define_function_full(
9        map,
10        &["\\html@mathml"],
11        "htmlmathml",
12        2, 0, None,
13        false, true, true, false, false,
14        handle_htmlmathml,
15    );
16
17    define_function_full(
18        map,
19        &["\\htmlStyle"],
20        "html",
21        2, 0,
22        Some(vec![ArgType::Raw, ArgType::Original]),
23        false, true, true, false, false,
24        handle_htmlstyle,
25    );
26}
27
28fn handle_htmlmathml(
29    ctx: &mut FunctionContext,
30    args: Vec<ParseNode>,
31    _opt_args: Vec<Option<ParseNode>>,
32) -> ParseResult<ParseNode> {
33    let html = ParseNode::ord_argument(args[0].clone());
34    let mathml = ParseNode::ord_argument(args[1].clone());
35
36    Ok(ParseNode::HtmlMathMl {
37        mode: ctx.parser.mode,
38        html,
39        mathml,
40        loc: None,
41    })
42}
43
44fn handle_htmlstyle(
45    ctx: &mut FunctionContext,
46    args: Vec<ParseNode>,
47    _opt_args: Vec<Option<ParseNode>>,
48) -> ParseResult<ParseNode> {
49    let mut args = args.into_iter();
50    let style = match args.next() {
51        Some(ParseNode::Raw { string, .. }) => string,
52        _ => return Err(ParseError::msg("Expected raw style for \\htmlStyle")),
53    };
54    let body = ParseNode::ord_argument(args.next().unwrap());
55    let mut attributes = HashMap::new();
56    attributes.insert("style".to_string(), style);
57
58    Ok(ParseNode::Html {
59        mode: ctx.parser.mode,
60        attributes,
61        body,
62        loc: None,
63    })
64}