craft-math: a multi-letter script base survives linear text (port of wordcraft#6230)

The parser attaches a script to the last character before it (mc^2 is m, c²), so a base of
several letters from a Word or LibreOffice file, written bare, read back split. lin_base now writes
such a base in invisible brackets, 〖mc〗^2, as Word linearizes it; a number (10^3, as Word writes
it) and an upright function name stay bare.

Ported from storytold/wordcraft#6230 (issue wordcraft#6228), which is stacked on wordcraft#6084:
its own change to math.rs and its tests/linear_multichar_base.rs. The ODT test that pinned
LibreOffice's E=mc^2 (<msup><mi>mc</mi>…) moves with it: in craft-odf it now expects E=〖mc〗^2,
as in the WordCraft PR. It runs only with craft-odf's `text` feature.
This commit is contained in:
kiwi4boy
2026-11-12 21:59:46 -05:00
parent 8d2481a748
commit cc63fb5074
3 changed files with 49 additions and 3 deletions
+8 -2
View File
@@ -551,13 +551,19 @@ fn lin_operand(a: &[MNode], s: &mut String, depth: usize) {
/// `(a^b)^c` would gain a visible delimiter (#6075). A base that is itself one invisible delimiter
/// (LaTeX `\left.a\right.`) gets an outer pair too, so the inner one reads back as that delimiter.
/// So does an operator (`⊗`, `×`, `<`…): a script after one reads as a pre-script, so `⊗_i` would
/// come back as `⊗` and a pre-script, where `〖⊗〗_i` keeps it the base.
/// come back as `⊗` and a pre-script, where `〖⊗〗_i` keeps it the base. And so does a run of several
/// letters: the parser attaches a script to the last character before it (`mc^2` is `m`, `c²`), so
/// `mc` is written `〖mc〗^2`, as Word linearizes it (#6228). A number (`10^3`, as Word writes it) and
/// an upright function name (`sin^2`, read whole as a name) stay bare.
fn lin_base(a: &[MNode], s: &mut String, depth: usize) {
let mut inner = String::new();
lin_arg(a, &mut inner, depth);
let invisible = matches!(a, [MNode::Delim { beg: None, end: None, elems, .. }] if elems.len() == 1);
let operator = matches!(a, [MNode::Run(r)] if r.text.chars().all(crate::math_linear::is_boundary));
if simple_operand(a) && !inner.is_empty() && !invisible && !operator {
let number = |t: &str| t.chars().all(|c| c.is_ascii_digit() || c == '.');
let name = |r: &MRun| r.sty == Some(MSty::Plain) && crate::math_linear::FUNCTION_NAMES.contains(&r.text.as_str());
let letters = matches!(a, [MNode::Run(r)] if r.text.chars().nth(1).is_some() && !number(&r.text) && !name(r));
if simple_operand(a) && !inner.is_empty() && !invisible && !operator && !letters {
s.push_str(&inner);
} else {
s.push('〖');
@@ -0,0 +1,39 @@
//! A script base of several letters survives linear text (#6228). The parser attaches a script to the
//! last character before it (`ab^2` is `a`, `b²`), so `to_linear` writes such a base in invisible
//! brackets, `〖mc〗^2`, as Word linearizes it. Single characters, numbers and upright function
//! names stay bare.
use craft_math::math::{MNode, MRun, ScriptKind, parse_linear, to_linear};
fn a(t: &str) -> Vec<MNode> {
vec![MNode::Run(MRun::new(t))]
}
fn sup(base: Vec<MNode>, s: Vec<MNode>) -> MNode {
MNode::Script { kind: ScriptKind::Sup, base, sub: Vec::new(), sup: s }
}
#[test]
fn a_base_of_several_letters_prints_back_whole() {
for base in ["ab", "mc", "x1", "αβ"] {
let n = vec![sup(a(base), a("2"))];
assert_eq!(to_linear(&n), format!("〖{base}〗^2"));
assert_eq!(parse_linear(&to_linear(&n)), n, "{base}");
}
// After other text, and as the base of a base.
let e = vec![MNode::Run(MRun::new("E=")), sup(a("mc"), a("2"))];
assert_eq!(to_linear(&e), "E=〖mc〗^2");
assert_eq!(parse_linear(&to_linear(&e)), e);
let nested = vec![sup(vec![sup(a("ab"), a("2"))], a("3"))];
assert_eq!(parse_linear(&to_linear(&nested)), nested, "{}", to_linear(&nested));
}
#[test]
fn single_characters_numbers_and_function_names_stay_bare() {
assert_eq!(to_linear(&[sup(a("x"), a("2"))]), "x^2");
// Word writes a number base bare too (`10^3`).
assert_eq!(to_linear(&[sup(a("10"), a("3"))]), "10^3");
assert_eq!(to_linear(&[sup(a("2.5"), a("2"))]), "2.5^2");
let sin = vec![sup(vec![MNode::Run(MRun::plain("sin"))], a("2"))];
assert_eq!(to_linear(&sin), "sin^2");
}
+2 -1
View File
@@ -167,7 +167,8 @@ fn embedded_formula_object_reads_its_mathml() {
.flat_map(|p| &p.inlines)
.filter_map(|i| if let Inline::Equation { linear, display } = i { Some((linear.clone(), *display)) } else { None })
.collect();
assert_eq!(eqs, vec![("E=mc^2".to_string(), true)]);
// The base is `mc` (`<msup><mi>mc</mi>…`), so its linear form groups it, as Word writes it (#6228).
assert_eq!(eqs, vec![("E=〖mc〗^2".to_string(), true)]);
}
#[test]