mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-12 10:03:51 +00:00
* Add craft-math: the equation tree with OMML, MathML, LaTeX and UnicodeMath
The math tree, LaTeX, linear-format, symbol and tree-walk modules come
from craft-wordmodel (storytold/craft-libs#963, WordCraft's crates/doc at
07f97737) unchanged, with the run colour (Rgb) and the symbol-font tables
they use. MathML is WordCraft's crates/formats/src/mathml.rs and OMML is
WordCraft's crates/docx/src/read/math.rs and write/math.rs (06c2ae6f /
07f97737), moved onto craft-xml. The HTML-specific MathML tests stay in
WordCraft; the rest moved with the code.
mathml::parse and omml::parse/read_str/write_str are new: string entry
points for callers that have no tree of their own.
* craft-math: read MathLive's and other spellings of LaTeX math commands
\abs{...} (an absolute value), \lvert \rvert \lVert \rVert, \nparallel,
\backprime, \infinity, \cross, \differentialD, \exponentialE and
\imaginaryI (MathLive's d, upright e and i), \emph, \textsf, \hbox,
\mathnormal and \underleftarrow / \underrightarrow /
\underleftrightarrow were unknown and read as literal text. They are read
as equaSHion's typed-input parser reads them (storytold/craft-libs#29).
* craft-math: equaSHion's equation corpus as test data
corpus/equations.tsv, corpus/latex.tsv and the OMML samples of
storytold/craft-libs#29 (7a6ef6b9), unmodified.
* craft-math: corpus round trips, GridCraft's OMML cases, entry points and limits tested; README
The corpus equations go through OMML, MathML and LaTeX and back; the OMML
samples are written back as kept and from the tree. GridCraft's OMML to
LaTeX test cases (crates/xlsx/src/omml.rs) give LaTeX that reads back the
same through the tree. omml::parse and read_str are tested against other
prefixes, the strict namespace, kept OMML, junk, the element cap and deep
nesting.
---------
Co-authored-by: shuff57 <62350898+shuff57@users.noreply.github.com>
56 lines
2.3 KiB
Rust
56 lines
2.3 KiB
Rust
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
|
|
// The code came from an app whose lints allow this.
|
|
#![allow(clippy::type_complexity)]
|
|
//! Equations: one tree and the syntaxes it is read from and written to.
|
|
//!
|
|
//! - [`math`]: the tree ([`MNode`], [`MRun`], an argument [`Arg`] is a `Vec<MNode>`), modelled on
|
|
//! the Office Math object model (ECMA-376 Part 1 §22.1), and [`Math`], an equation with its kept
|
|
//! source OMML.
|
|
//! - [`math_linear`]: the linear format (UnicodeMath, the plain text Word shows for an equation),
|
|
//! read and written ([`math::parse_linear`], [`math::to_linear`]).
|
|
//! - [`math_latex`]: LaTeX math, read and written ([`math_latex::parse_latex`],
|
|
//! [`math_latex::to_latex`]), with the named symbols of [`math_symbols`].
|
|
//! - [`omml`]: Office Math Markup, read ([`omml::read_omath`], [`omml::read_str`]) and written
|
|
//! ([`omml::write_equation`], [`omml::write_str`]).
|
|
//! - [`mathml`]: presentation MathML, written ([`mathml::to_mathml`]) and read
|
|
//! ([`mathml::parse`], [`mathml::read`], [`mathml::to_tree`]).
|
|
//! - [`math_tree`]: the arguments of a node and a walk over every run.
|
|
//! - [`symbol_fonts`]: text in symbol fonts (Symbol, Wingdings…) as Unicode, as OMML runs and
|
|
//! Word text need it.
|
|
//! - [`Rgb`]: the colour of a run.
|
|
//!
|
|
//! Every reader is bounded (nesting [`math::MAX_DEPTH`], node budgets) and never panics on any
|
|
//! input.
|
|
//!
|
|
//! ```
|
|
//! use craft_math::{math, math_latex, mathml, omml};
|
|
//!
|
|
//! let tree = math::parse_linear("x=(-b±√(b^2-4ac))/2a");
|
|
//! assert_eq!(math::to_linear(&tree), "x=(-b±√(b^2-4ac))/2a");
|
|
//! assert!(math_latex::to_latex(&tree).contains("\\frac"));
|
|
//!
|
|
//! let eq = math::Math { nodes: tree.clone(), ..Default::default() };
|
|
//! let xml = omml::write_str("", false, &eq, None);
|
|
//! assert_eq!(omml::read_str(&xml).map(|m| m.nodes), Some(tree.clone()));
|
|
//!
|
|
//! let ml = mathml::to_mathml(&tree, false, "");
|
|
//! assert_eq!(mathml::parse(&ml).as_ref().map(mathml::to_tree), Some(tree));
|
|
//! ```
|
|
|
|
#[cfg(doctest)]
|
|
#[doc = include_str!("../README.md")]
|
|
struct ReadmeExamples;
|
|
|
|
pub mod math;
|
|
pub mod math_latex;
|
|
pub mod math_linear;
|
|
pub mod math_symbols;
|
|
pub mod math_tree;
|
|
pub mod mathml;
|
|
pub mod omml;
|
|
mod rgb;
|
|
pub mod symbol_fonts;
|
|
|
|
pub use math::{Arg, MNode, MRun, Math, MathProps};
|
|
pub use rgb::Rgb;
|