flm_core/math_passthrough/mod.rs
1//! The package `math-passthrough` and its one feature, `math-passthrough`:
2//! mathematics carried to the output as the author wrote it, without being read.
3//!
4//! The [`math`](crate::stdlib::features::math) feature, one of the standard
5//! features ([`stdlib`](crate::stdlib)), introduces mathematics into the language (the
6//! display-math environments, their equation lines and numbers, the text
7//! commands, `\eqref`) and processes none of it. This feature is one of the
8//! features that *process* mathematics, of which a registry holds one: it
9//! accepts any formula whose macro calls and environments are syntactically
10//! valid, reads nothing in it, and carries the source of each formula and of
11//! each equation line to the output. Every name it registers carries
12//! `passthrough`, as a reminder that FLM has not parsed the content. Each
13//! backend writes that source in a form the reader of the output typesets: a
14//! LaTeX compiler, or the MathJax script in a web browser.
15//!
16//! # The package
17//!
18//! The package `math-passthrough` ([`PACKAGE`]) holds this one feature and uses
19//! no other package: the feature's declarations state what it needs (see *It
20//! requires `math`* below). A program written in Rust adds [`package`] after the
21//! package that holds the standard features; a program that reads its settings
22//! as data adds [`factory`] to its catalog of packages:
23//!
24//! ```
25//! use flm_core::core::plan::{Overrides, Plan};
26//! use flm_core::core::registry::{PackageEntry, RegistryBuilder, RegistryInputs};
27//! use flm_core::math_passthrough;
28//! use flm_core::stdlib::{self, StdlibConfig};
29//!
30//! let mut builder = RegistryBuilder::new();
31//! builder.use_package(stdlib::add_features(PackageEntry::new("std"), StdlibConfig::new()));
32//! builder.use_package(math_passthrough::package());
33//! let registry = builder.build(&RegistryInputs::new())?;
34//!
35//! // The plan of a standard workflow builds.
36//! let plan = Plan::for_workflow(®istry, stdlib::backends::html::WORKFLOW, &Overrides::none());
37//! assert!(plan.is_ok());
38//! # Ok::<(), flm_core::core::registry::RegistryBuildError>(())
39//! ```
40//!
41//! # It requires `math`
42//!
43//! The feature's rules lower `flm.math.environment` and its check stage
44//! provides `flm.math.content`, both declared by the `math` feature. A registry
45//! that holds this feature without `math` fails to build: a rule on an element
46//! that no feature declares, and a stage providing a capability that no
47//! feature declares, are each a [`BuildError::Undeclared`] naming the feature,
48//! the place and the name. Conversely, in a registry that holds `math` and no
49//! feature that processes mathematics, each formula is an Error (the
50//! [`math`](crate::stdlib::features::math) module documentation, *Processing
51//! mathematics*).
52//!
53//! The feature depends on nothing outside `flm-core`: an embedded program with
54//! no JavaScript and no extension renders mathematics with it. It has no
55//! configuration and no settings of its own; a program that reads its settings
56//! as data switches it off like any other feature.
57//!
58//! # Unknown commands in mathematics
59//!
60//! Mathematical notation uses an open-ended set of commands that FLM does not
61//! declare. This feature therefore registers a *fallback* ([`FallbackDef`]), a
62//! declaration of the element that the parse gives to a macro or an environment
63//! that no feature declares, for macros and for environments **in math mode
64//! only**. Such a command becomes a node of the element [`OPAQUE`]
65//! (`flm.math-passthrough.opaque`): it takes no argument, its source is kept
66//! character for character, and nothing is reported. A command of another
67//! feature written inside a formula (`\section`, `\href`) is such a command,
68//! since every other feature declares its commands for text mode only.
69//!
70//! Outside math mode the language layer's own fallbacks apply: an unknown macro
71//! becomes `flm.unknown.macro` and an unknown environment
72//! `flm.unknown.environment`, each with an Error diagnostic.
73//!
74//! # The check
75//!
76//! The check stage [`CHECK`] (`flm.math-passthrough.check`) provides the
77//! capability [`flm.math.content`](crate::stdlib::features::math::CONTENT),
78//! and the feature adds that capability to the collection
79//! [`flm.validated`](crate::stdlib::VALIDATED), which every standard workflow
80//! demands. It accepts every formula and reports every equation line that
81//! holds no mathematics ([`check`]).
82//!
83//! # The rules
84//!
85//! Each rule stages (builds into the lowered tree; see
86//! [`Lowerer`](crate::core::lowering::Lowerer)) presentation nodes. Except on the
87//! `latex-recomposed` backend, a formula becomes a
88//! [`flm.pres.math-passthrough`](crate::stdlib::backends::pres::MATH_PASSTHROUGH)
89//! node that holds its source; the rules write the source unchanged with one
90//! exception: **a text command becomes content**, lowered through its own rule
91//! into a [`flm.pres.math-text`](crate::stdlib::backends::pres::MATH_TEXT) node
92//! among the formula's source ([`source`]).
93//!
94//! | element | every backend | `latex-recomposed` |
95//! |---|---|---|
96//! | `flm.math` (`\(…\)`, `\[…\]`) | [`MATH_RULE`] | [`latex::MATH_LATEX_RULE`] |
97//! | `flm.math.environment` | [`ENVIRONMENT_RULE`] | [`latex::ENVIRONMENT_LATEX_RULE`] |
98//! | `flm.math-passthrough.opaque` | [`OPAQUE_RULE`] | [`OPAQUE_RULE`] |
99//!
100//! - [`MATH_RULE`] stages a `flm.pres.math-passthrough` node, displayed for
101//! `\[…\]` and inline for `\(…\)`, over the group's source.
102//! - [`ENVIRONMENT_RULE`] stages a displayed `flm.pres.math-passthrough` node
103//! that carries the environment's name, over the environment's lines. Each
104//! line gives, in order: an empty `flm.pres.math-text` node carrying the
105//! line's anchor, so that a link can lead to the line; the line's source;
106//! the line's tag, written `\tag*{…}` around a `flm.pres.math-text` node that
107//! holds the author's tag or the line's number; and, when another line
108//! follows, the `\\` that ends the line. A line's `\label` and `\nonumber` are
109//! not written: they are data of the line, which the `math` feature has
110//! already read.
111//! - The rules of [`latex`] write mathematics back as LaTeX source, with each
112//! line's labels in the [LaTeX hook](crate::stdlib::backends::latex::preamble)
113//! `\flmLabels` (a macro of the style file that a user may redefine), and
114//! leave the numbering to LaTeX.
115//! - [`OPAQUE_RULE`] stages a `flm.pres.math-passthrough` node over the opaque
116//! node's source. A parse produces opaque nodes only in math mode, and the
117//! rules above write a math group or an environment whole, so this rule runs
118//! only for math-mode content that is lowered node by node, for example, on
119//! the `latex-recomposed` backend, a math-mode argument of a user definition
120//! written as a call of that definition.
121//!
122//! The rules marked for every backend serve any backend that writes
123//! `flm.pres.math-passthrough` natively, which the four standard backends do:
124//! the `html` backend writes it as the [HTML
125//! contract](crate::guide::rendering#the-html-contract) states and loads the
126//! MathJax script at the first such formula of a page; the rendering guide
127//! describes the result under [mathematics in
128//! HTML](crate::guide::rendering#mathematics-in-html), [mathematics in the
129//! rendered LaTeX](crate::guide::rendering#mathematics-in-the-rendered-latex) and
130//! [mathematics in the recomposed
131//! LaTeX](crate::guide::rendering#mathematics-in-the-recomposed-latex).
132//!
133//! [`ENVIRONMENT_RULE`] reads two tables that other features own: the anchors
134//! table of the references feature
135//! ([`ANCHORS`]) and the counter table of the numbering feature
136//! ([`COUNTERS`]). It reads both
137//! optionally, under a peer declaration ([`PeerName`](crate::core::registry::PeerName))
138//! of each, so that a registry without those features still builds and renders:
139//! the formulas then carry no anchor and no number.
140//!
141//! # The rules under another feature's names
142//!
143//! Another feature that processes mathematics may write formulas as LaTeX
144//! with this feature's rules, registered under rule names of its own:
145//! [`math_rule_named`] and [`environment_rule_named`] build the rules that
146//! stage `flm.pres.math-passthrough` under a given name and default mark, and
147//! [`latex::math_rule_named`] and [`latex::environment_rule_named`] the rules
148//! of the `latex-recomposed` backend under a given name. Each rule carries
149//! this feature's body, declaration and description, and the documentation of
150//! each constructor lists the peer declarations the registering feature makes
151//! beside it. The registering feature owns the rule, so the plan's warning on
152//! its unchecked output (below) names that feature. [`OPAQUE_RULE`] has no
153//! such constructor: its element is this feature's fallback's.
154//!
155//! The `math-techxt` extension, which renders formulas as Unicode text on the
156//! `text` and `html` backends, registers the four on `latex-rendered` and
157//! `latex-recomposed`, where LaTeX typesets each formula from its LaTeX, and
158//! writes no LaTeX rule of its own. An extension reaches the constructors
159//! through the extension API's mirror of this module; in JavaScript, its
160//! modules are `flm:math_passthrough` and `flm:math_passthrough/latex`, which
161//! keep this module's name, with its underscore, while the feature and its
162//! package are named `math-passthrough`.
163//!
164//! # Output that nothing checks
165//!
166//! The feature does not read a formula, so nothing checks that the program
167//! reading the output accepts what it writes: a misspelled command reaches
168//! that program as written. Each rule declares so
169//! ([`LoweringDecl::unchecked_output`]) on the backends where a program reads
170//! the formulas after FLM:
171//!
172//! | backend | rules | what is written | what reads it |
173//! |---|---|---|---|
174//! | `html` | [`MATH_RULE`], [`ENVIRONMENT_RULE`], [`OPAQUE_RULE`] | each formula's LaTeX source | the formula renderer in the reader's browser |
175//! | `latex-rendered` | [`MATH_RULE`], [`ENVIRONMENT_RULE`], [`OPAQUE_RULE`] | each formula's LaTeX source | LuaLaTeX |
176//! | `latex-recomposed` | the rules of [`latex`], [`OPAQUE_RULE`] | each formula's recomposed LaTeX | LaTeX |
177//!
178//! A plan of one of these backends therefore keeps one
179//! [`PlanWarning::UncheckedOutput`](crate::core::plan::PlanWarning::UncheckedOutput)
180//! for this feature, or for the feature that registers the rules under its
181//! own names, which the run reports as the Warning
182//! `flm.run.unchecked-output`. The rules declare nothing on the `text`
183//! backend: the text output prints each formula's source as it is, and no
184//! program reads it afterwards.
185//!
186//! # Map of the module
187//!
188//! - [`PACKAGE`], [`package`], [`factory`] — the package.
189//! - [`MathPassthrough`] — the feature; [`feature`] returns it.
190//! - [`OPAQUE`] — the element of an unknown command in math mode.
191//! - [`CHECK`], [`check`] — the check stage and its one condition.
192//! - [`MATH_RULE`], [`ENVIRONMENT_RULE`], [`OPAQUE_RULE`], [`source`] — the
193//! rules marked for every backend and their bodies.
194//! - [`math_rule_named`], [`environment_rule_named`] — the same rules under
195//! another feature's names.
196//! - [`latex`] — the rules for the `latex-recomposed` backend.
197
198// An environment fallback stands beside the macro one because an alignment
199// environment nested inside displayed mathematics is the common case. There is
200// no fallback for special characters: an unrecognized trigger character is not
201// a special character, so such a fallback could never apply. The source of a
202// formula is kept as written because it goes to a formula engine or is printed
203// literally, where any normalization would corrupt it.
204
205pub mod check;
206pub mod latex;
207pub mod source;
208
209use std::sync::Arc;
210
211use techy::latexlike::Mode;
212
213use crate::core::lowering::{DefaultMark, LoweringDecl, LoweringRule};
214use crate::core::registry::{
215 BuildError, FeatureBuilder, FeatureInfo, FlmFeature, PackageEntry, PackageError, PackageFactory,
216};
217use crate::core::settings::PackageSettings;
218use crate::lang::mint::{FallbackDef, FallbackKind};
219use crate::lang::structural::MATH;
220use crate::model::element::ElementSpec;
221use crate::model::names::{FeatureName, RuleName};
222use crate::stdlib::VALIDATED;
223use crate::stdlib::backends::{html, latex_recomposed, latex_rendered, pres};
224use crate::stdlib::features::math::{CONTENT, ENVIRONMENT};
225use crate::stdlib::features::numbering::{COUNTERS, Counters};
226use crate::stdlib::features::refs::{self, ANCHORS};
227
228pub use check::{CHECK, EmptyLine};
229pub use source::{EnvironmentRule, MathRule, OpaqueRule};
230
231/// The name of the package, `math-passthrough`: the name by which a program
232/// looks the package up in its catalog of packages (with the `flm` program,
233/// the name a document lists under `use` in its front matter).
234pub const PACKAGE: &str = "math-passthrough";
235
236/// The feature's registered name, `math-passthrough`, the name of its package
237/// too.
238pub const FEATURE: &str = "math-passthrough";
239
240/// Returns the package `math-passthrough`, holding the [`MathPassthrough`]
241/// feature.
242///
243/// The returned [`PackageEntry`] uses no other package: a program that
244/// registers it registers the standard features
245/// ([`stdlib::add_features`](crate::stdlib::add_features)) too, before it,
246/// since the feature requires the `math` feature (see the [module
247/// documentation](self)).
248pub fn package() -> PackageEntry {
249 PackageEntry::new(PACKAGE).feature(feature())
250}
251
252/// Returns the package factory of the package `math-passthrough`: the form in
253/// which a program adds the package to its catalog of packages
254/// ([`PackageCatalog::add_compiled`](crate::core::registry::PackageCatalog::add_compiled)).
255///
256/// The [`PackageFactory`] declares that the package uses no other package. The
257/// feature takes no settings; its build function returns [`package`], without
258/// the feature when the settings switch it off.
259///
260/// # Errors
261///
262/// A [`PackageError`] when the build function fails for empty settings
263/// ([`PackageFactory::new`] calls it once). This build function never fails, so
264/// the error does not occur.
265pub fn factory() -> Result<PackageFactory, PackageError> {
266 PackageFactory::new(PACKAGE, |settings: &PackageSettings| {
267 let mut entry = package();
268 entry.features.retain(|feature| !settings.is_off(&feature.info().name));
269 Ok(entry)
270 })
271}
272
273/// The name of the element of a macro or an environment that no feature
274/// declares, written in math mode, `flm.math-passthrough.opaque`: the node
275/// keeps its source as written. See the module documentation, *Unknown
276/// commands in mathematics*.
277pub const OPAQUE: &str = "flm.math-passthrough.opaque";
278
279/// The name of the default lowering rule of [`OPAQUE`] on every backend,
280/// `flm.math-passthrough.opaque.lower.default`, whose body is [`OpaqueRule`].
281pub const OPAQUE_RULE: &str = "flm.math-passthrough.opaque.lower.default";
282
283/// The name of the rule of `flm.math` (inline and displayed mathematics
284/// written without an environment), `flm.math.lower.passthrough`, whose body is
285/// [`MathRule`]: the default on every backend. On the `latex-recomposed`
286/// backend, [`MATH_LATEX_RULE`](latex::MATH_LATEX_RULE) is chosen instead,
287/// because it is the default on that backend.
288pub const MATH_RULE: &str = "flm.math.lower.passthrough";
289
290/// The name of the rule of the display-math environments
291/// (`flm.math.environment`), `flm.math.environment.lower.passthrough`, whose
292/// body is [`EnvironmentRule`]: the default on every backend. On the
293/// `latex-recomposed` backend,
294/// [`ENVIRONMENT_LATEX_RULE`](latex::ENVIRONMENT_LATEX_RULE) is chosen
295/// instead, because it is the default on that backend.
296pub const ENVIRONMENT_RULE: &str = "flm.math.environment.lower.passthrough";
297
298/// Adds to `decl` the two backends on which a rule of this feature writes a
299/// formula's source and nothing checks it before the program that reads the
300/// output: `html`, whose formulas the formula renderer in the reader's browser
301/// typesets, and `latex-rendered`, whose formulas LuaLaTeX typesets (see the
302/// module documentation, *Output that nothing checks*).
303fn source_unchecked(decl: LoweringDecl) -> LoweringDecl {
304 decl.unchecked_output(
305 html::BACKEND,
306 "each formula's LaTeX source",
307 "the formula renderer in the reader's browser",
308 )
309 .unchecked_output(latex_rendered::BACKEND, "each formula's LaTeX source", "LuaLaTeX")
310}
311
312/// Adds to `decl` the `latex-recomposed` backend, on which a rule of this
313/// feature writes each formula as LaTeX recomposed from its nodes and nothing
314/// checks it before LaTeX reads it (see the module documentation, *Output that
315/// nothing checks*).
316fn recomposed_unchecked(decl: LoweringDecl) -> LoweringDecl {
317 decl.unchecked_output(
318 latex_recomposed::BACKEND,
319 "each formula's recomposed LaTeX",
320 "LaTeX",
321 )
322}
323
324/// The element value of every node of [`OPAQUE`].
325fn opaque_element() -> Arc<ElementSpec> {
326 Arc::new(ElementSpec::new(OPAQUE).docs(
327 "A macro or environment that no feature declares, written in math mode: the node \
328 keeps its source as written, because mathematical notation uses an open-ended set \
329 of commands.",
330 ))
331}
332
333/// The rule [`OPAQUE_RULE`], ready to be registered: it targets [`OPAQUE`],
334/// produces `flm.pres.math-passthrough`, is the default on every backend, and
335/// declares its output unchecked on `html`, `latex-rendered` and
336/// `latex-recomposed` (see the module documentation, *Output that nothing
337/// checks*).
338pub fn opaque_rule() -> LoweringRule {
339 LoweringRule::new(
340 OPAQUE_RULE,
341 OPAQUE,
342 recomposed_unchecked(source_unchecked(LoweringDecl::new().produces(pres::MATH_PASSTHROUGH))),
343 Arc::new(OpaqueRule),
344 DefaultMark::Everywhere,
345 )
346 .docs(
347 "an unknown command in math mode lowers to `flm.pres.math-passthrough` over its \
348 source, unchanged",
349 )
350}
351
352/// The rule [`MATH_RULE`], ready to be registered: [`math_rule_named`] under
353/// that name, marked [`DefaultMark::Everywhere`].
354pub fn math_rule() -> LoweringRule {
355 math_rule_named(MATH_RULE, DefaultMark::Everywhere)
356}
357
358/// The library's rule of `flm.math` that stages `flm.pres.math-passthrough`,
359/// under the rule name `name` and the default mark `mark`, ready to be
360/// registered by any feature: its body is [`MathRule`], it targets
361/// `flm.math`, produces `flm.pres.math-passthrough`, declares its output
362/// unchecked on `html` and `latex-rendered` (see the module documentation,
363/// *Output that nothing checks*), and carries the library's description.
364/// [`math_rule`] is this rule under [`MATH_RULE`].
365///
366/// The rule does not declare that it produces `flm.pres.math-text`: the
367/// `flm.pres.math-text` node of a text command is staged by that command's own
368/// rule ([`text::RULE`](crate::stdlib::features::math::text::RULE)).
369///
370/// The rule reads no table, so a feature that registers it declares nothing
371/// beside it. The feature that registers the rule is its owner: a plan that
372/// keeps one of its unchecked outputs names that feature in the
373/// [`PlanWarning::UncheckedOutput`](crate::core::plan::PlanWarning::UncheckedOutput),
374/// with the texts the library declares.
375pub fn math_rule_named(name: impl Into<RuleName>, mark: DefaultMark) -> LoweringRule {
376 LoweringRule::new(
377 name,
378 MATH,
379 source_unchecked(LoweringDecl::new().produces(pres::MATH_PASSTHROUGH)),
380 Arc::new(MathRule),
381 mark,
382 )
383 .docs(
384 "inline or displayed mathematics lowers to `flm.pres.math-passthrough` over its \
385 source, with the argument of every text command inside it lowered as document \
386 content",
387 )
388}
389
390/// The rule [`ENVIRONMENT_RULE`], ready to be registered:
391/// [`environment_rule_named`] under that name, marked
392/// [`DefaultMark::Everywhere`].
393pub fn environment_rule() -> LoweringRule {
394 environment_rule_named(ENVIRONMENT_RULE, DefaultMark::Everywhere)
395}
396
397/// The library's rule of `flm.math.environment` that stages
398/// `flm.pres.math-passthrough`, under the rule name `name` and the default
399/// mark `mark`, ready to be registered by any feature: its body is
400/// [`EnvironmentRule`], it targets `flm.math.environment`, reads the anchors
401/// table and the counter table optionally, produces
402/// `flm.pres.math-passthrough` and `flm.pres.math-text`, declares its output
403/// unchecked on `html` and `latex-rendered` (see the module documentation,
404/// *Output that nothing checks*), and carries the library's description.
405/// [`environment_rule`] is this rule under [`ENVIRONMENT_RULE`].
406///
407/// A feature that registers the rule declares beside it, as
408/// [`MathPassthrough`] does, a peer table
409/// ([`FeatureBuilder::peer_table`]) of each table it reads: the anchors table
410/// ([`ANCHORS`], of value type [`Anchors`](refs::Anchors)) and the counter
411/// table ([`COUNTERS`], of value type [`Counters`]). The feature that
412/// registers the rule is its owner: a plan that keeps one of its unchecked
413/// outputs names that feature in the
414/// [`PlanWarning::UncheckedOutput`](crate::core::plan::PlanWarning::UncheckedOutput),
415/// with the texts the library declares.
416pub fn environment_rule_named(name: impl Into<RuleName>, mark: DefaultMark) -> LoweringRule {
417 LoweringRule::new(
418 name,
419 ENVIRONMENT,
420 source_unchecked(
421 LoweringDecl::new()
422 .reads_opt(ANCHORS)
423 .reads_opt(COUNTERS)
424 .produces(pres::MATH_PASSTHROUGH)
425 .produces(pres::MATH_TEXT),
426 ),
427 Arc::new(EnvironmentRule),
428 mark,
429 )
430 .docs(
431 "a display-math environment lowers to a displayed `flm.pres.math-passthrough` that \
432 carries its name, over its equation lines; each line begins with an empty \
433 `flm.pres.math-text` carrying its anchor and ends with its tag (the author's tag or \
434 the line's number) and, unless it is the last line, its line break; labels are not \
435 written",
436 )
437}
438
439/// The `math-passthrough` feature: it registers the fallback of math mode,
440/// the check stage that provides `flm.math.content`, which it adds to the
441/// collection `flm.validated`, and the lowering rules of the formulas on every
442/// backend. See the module documentation.
443#[derive(Debug, Default, Clone, Copy)]
444pub struct MathPassthrough;
445
446impl FlmFeature for MathPassthrough {
447 fn info(&self) -> FeatureInfo {
448 FeatureInfo::new(FEATURE, "Math passthrough").docs(
449 "Mathematics carried to the output as written: unknown macros and environments in \
450 math mode kept as source (`flm.math-passthrough.opaque`), the check \
451 `flm.math.content`, which accepts every formula, and the lowering rules of the \
452 formulas on every backend. Requires the `math` feature.",
453 )
454 }
455
456 fn register(&self, b: &mut FeatureBuilder<'_>) -> Result<(), BuildError> {
457 let owner: FeatureName = b.feature().clone();
458 let opaque = opaque_element();
459 for kind in [FallbackKind::Macro, FallbackKind::Environment] {
460 b.fallback(FallbackDef {
461 owner: owner.clone(),
462 modes: vec![Mode::Math],
463 kind,
464 element: Arc::clone(&opaque),
465 })?;
466 }
467 b.element_type_text(
468 OPAQUE,
469 "A command of mathematical notation that no feature declares, kept as written and \
470 passed to the output, where the program that typesets the formula reads it.",
471 )?;
472 b.stage(Arc::new(check::PassthroughCheck))?;
473 // Every workflow that validates a document demands the check of its
474 // mathematics, since this feature processes it.
475 b.collection_member(VALIDATED, CONTENT)?;
476 // The anchors table belongs to the references feature and the counter
477 // table to the feature that numbers; a registry need hold neither, so
478 // the environment rule reads both optionally, under these peer
479 // declarations, and a formula then carries no anchor and no number.
480 b.peer_table::<refs::Anchors>(
481 ANCHORS.name(),
482 "the references feature's anchors table, which the display-math environment rule \
483 reads for the anchor of each equation line",
484 )?;
485 b.peer_table::<Counters>(
486 COUNTERS.name(),
487 "the numbering feature's counter table, which the display-math environment rule \
488 reads for the number of each equation line",
489 )?;
490 b.lowering_rule(opaque_rule())?;
491 b.lowering_rule(math_rule())?;
492 b.lowering_rule(environment_rule())?;
493 latex::register(b)?;
494 Ok(())
495 }
496}
497
498/// The [`MathPassthrough`] feature, as a shared value ready to be listed in a
499/// package entry.
500pub fn feature() -> Arc<dyn FlmFeature> {
501 Arc::new(MathPassthrough)
502}
503
504#[cfg(test)]
505mod tests {
506 use super::*;
507 use crate::model::names::ElementName;
508
509 #[test]
510 fn the_package_holds_the_feature_and_uses_no_package() {
511 let entry = package();
512 assert_eq!(entry.name, PACKAGE);
513 assert!(entry.uses.is_empty());
514 let names: Vec<String> = entry.features.iter().map(|feature| feature.info().name.to_string()).collect();
515 assert_eq!(names, [FEATURE]);
516 let factory = factory().expect("the factory");
517 assert_eq!(factory.name().as_str(), PACKAGE);
518 assert!(factory.used().is_empty());
519 let mut settings = PackageSettings::new();
520 settings.switch_off(FEATURE);
521 assert!(factory.build(&settings).expect("the package builds").features.is_empty());
522 }
523
524 #[test]
525 fn the_rules_for_every_backend_are_marked_everywhere() {
526 for rule in [opaque_rule(), math_rule(), environment_rule()] {
527 assert!(rule.default.is_general(), "{}", rule.name);
528 assert!(rule.decl.produces.contains(&ElementName::from(pres::MATH_PASSTHROUGH)));
529 }
530 assert_eq!(environment_rule().decl.reads_opt.len(), 2);
531 assert!(math_rule().decl.reads_opt.is_empty());
532 }
533
534 /// The backends each rule declares unchecked output on.
535 fn unchecked_backends(rule: &LoweringRule) -> Vec<&str> {
536 rule.decl.unchecked_output.iter().map(|decl| decl.backend.as_str()).collect()
537 }
538
539 #[test]
540 fn the_rules_declare_unchecked_output_on_every_backend_but_text() {
541 assert_eq!(unchecked_backends(&math_rule()), ["html", "latex-rendered"]);
542 assert_eq!(unchecked_backends(&environment_rule()), ["html", "latex-rendered"]);
543 assert_eq!(unchecked_backends(&opaque_rule()), ["html", "latex-rendered", "latex-recomposed"]);
544 assert_eq!(unchecked_backends(&latex::math_rule()), ["latex-recomposed"]);
545 assert_eq!(unchecked_backends(&latex::environment_rule()), ["latex-recomposed"]);
546 }
547}