Skip to main content

flm_core/stdlib/features/math/
html.rs

1//! The mark of a formula on the HTML backend: what tells a stylesheet or a
2//! script of the page that an element is a formula, whichever processing
3//! feature wrote it.
4//!
5//! Each processing feature (the module documentation of [`math`](super),
6//! *Processing mathematics*) writes a formula into the page its own way: the
7//! TeX that a renderer in the reader's browser typesets, MathML, text, a
8//! drawing. Whatever element it writes as a formula's **outermost element**,
9//! that element carries the mark, and nothing is written around it for the
10//! mark's sake:
11//!
12//! - the class of the role [`FORMULA_ROLE`], `flm-math-formula`, after the
13//!   feature's own classes;
14//! - `data-flm-display`, `inline` or `display`;
15//! - for a display-math environment, `data-flm-env`, the environment's name.
16//!
17//! A stylesheet selects a formula as `.flm-math-formula` and a displayed one as
18//! `.flm-math-formula[data-flm-display="display"]`; a script finds the formula
19//! around an element with `closest('.flm-math-formula')`. The mark says what
20//! an element is and nothing of how it is written: whether a script of the
21//! page still has to transform the formula is that script's business, under the
22//! conventions for a page updated in place ([a page updated in
23//! place](crate::guide::rendering#a-page-updated-in-place)).
24//!
25//! A feature that writes the outermost element as HTML tags
26//! ([`flm.pres.html.tag`](crate::stdlib::backends::html::TAG)) gives it the
27//! attributes [`formula_attributes`] answers. The formulas of
28//! [`math-passthrough`](crate::math_passthrough) are
29//! `flm.pres.math-passthrough` nodes whose payload carries the role, and the
30//! HTML contract writes the role's class and the payload's `display` and `env`
31//! as these same attributes.
32
33use crate::stdlib::backends::html::{class_of_role, data_attribute};
34use crate::stdlib::backends::pres::MathDisplay;
35
36/// The role of a formula's outermost element, `flm.math.formula`, whose class
37/// `flm-math-formula` marks a formula in an HTML page.
38pub const FORMULA_ROLE: &str = "flm.math.formula";
39
40/// The attributes that mark the outermost element of a formula written as
41/// HTML tags: `class`, holding `classes` (the writer's own classes, separated
42/// by spaces; empty for none) followed by the class of [`FORMULA_ROLE`];
43/// `data-flm-display`, the name of `display`; and, when `env` names the
44/// display-math environment the formula is written as, `data-flm-env`.
45///
46/// ```
47/// use flm_core::stdlib::backends::pres::MathDisplay;
48/// use flm_core::stdlib::features::math::html::formula_attributes;
49///
50/// assert_eq!(
51///     formula_attributes("my-formula", MathDisplay::Display, Some("align")),
52///     [
53///         (String::from("class"), String::from("my-formula flm-math-formula")),
54///         (String::from("data-flm-display"), String::from("display")),
55///         (String::from("data-flm-env"), String::from("align")),
56///     ],
57/// );
58/// ```
59pub fn formula_attributes(classes: &str, display: MathDisplay, env: Option<&str>) -> Vec<(String, String)> {
60    let role = class_of_role(FORMULA_ROLE);
61    let class = if classes.trim().is_empty() { role } else { format!("{} {role}", classes.trim()) };
62    let mut attributes = vec![
63        (String::from("class"), class),
64        (data_attribute("display"), String::from(display.as_str())),
65    ];
66    if let Some(env) = env {
67        attributes.push((data_attribute("env"), String::from(env)));
68    }
69    attributes
70}