cellml-text-editor 0.1.5 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -145,7 +145,7 @@ class x {
|
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
147
|
const m = "http://www.cellml.org/cellml/2.0#", h = "http://www.w3.org/1998/Math/MathML";
|
|
148
|
-
class
|
|
148
|
+
class $ {
|
|
149
149
|
scanner;
|
|
150
150
|
doc;
|
|
151
151
|
sourceLineAttr;
|
|
@@ -469,7 +469,7 @@ class g {
|
|
|
469
469
|
}), p += `${i}</${t}>`, p;
|
|
470
470
|
}
|
|
471
471
|
}
|
|
472
|
-
const
|
|
472
|
+
const g = "http://www.cellml.org/cellml/2.0#";
|
|
473
473
|
class N {
|
|
474
474
|
output = "";
|
|
475
475
|
indentLevel = 0;
|
|
@@ -561,7 +561,7 @@ class N {
|
|
|
561
561
|
if (s === "ci")
|
|
562
562
|
return e.textContent?.trim() || "";
|
|
563
563
|
if (s === "cn") {
|
|
564
|
-
const i = e.textContent?.trim() || "0", t = e.getAttributeNS(
|
|
564
|
+
const i = e.textContent?.trim() || "0", t = e.getAttributeNS(g, "units");
|
|
565
565
|
return t ? `${i} {units: ${t}}` : i;
|
|
566
566
|
} else {
|
|
567
567
|
if (s === "piecewise")
|
|
@@ -647,10 +647,13 @@ class C {
|
|
|
647
647
|
}
|
|
648
648
|
return this.parseNode(e);
|
|
649
649
|
}
|
|
650
|
+
ignoreTag(e) {
|
|
651
|
+
return ["bvar"].includes(e);
|
|
652
|
+
}
|
|
650
653
|
parseNode(e) {
|
|
651
654
|
if (!e) return "";
|
|
652
655
|
const s = e.localName;
|
|
653
|
-
return s === "apply" ? this.parseApply(e) : s === "ci" ? this.parseIdentifier(e.textContent || "") : s === "cn" ? e.textContent || "0" : s === "piecewise" ? this.parsePiecewise(e) : "";
|
|
656
|
+
return s === "apply" ? this.parseApply(e) : s === "ci" ? this.parseIdentifier(e.textContent || "") : s === "cn" ? e.textContent || "0" : s === "piecewise" ? this.parsePiecewise(e) : s === "pi" ? "\\pi" : (this.ignoreTag(s) || console.warn(`Unsupported MathML node: ${s}`), "");
|
|
654
657
|
}
|
|
655
658
|
escapeGreek(e) {
|
|
656
659
|
return [
|
|
@@ -776,5 +779,5 @@ class C {
|
|
|
776
779
|
export {
|
|
777
780
|
C as CellMLLatexGenerator,
|
|
778
781
|
N as CellMLTextGenerator,
|
|
779
|
-
|
|
782
|
+
$ as CellMLTextParser
|
|
780
783
|
};
|
|
@@ -8,4 +8,4 @@
|
|
|
8
8
|
${this.indent()}${s.join(`
|
|
9
9
|
${this.indent()}`)}
|
|
10
10
|
${this.indent()}endsel`}}class E{convert(e){if(!e)return"";if(e.localName==="math")return Array.from(e.children).map(s=>this.convert(s)).join(`
|
|
11
|
-
`);if(e.localName==="apply"&&e.firstElementChild?.localName==="eq"){const s=Array.from(e.children),i=this.parseNode(s[1]),t=this.parseNode(s[2]);return`${i} = ${t}`}return this.parseNode(e)}parseNode(e){if(!e)return"";const s=e.localName;return s==="apply"?this.parseApply(e):s==="ci"?this.parseIdentifier(e.textContent||""):s==="cn"?e.textContent||"0":s==="piecewise"?this.parsePiecewise(e):""}escapeGreek(e){return["alpha","beta","gamma","delta","epsilon","zeta","eta","theta","iota","kappa","lambda","mu","nu","xi","omicron","pi","rho","sigma","tau","upsilon","phi","chi","psi","omega"].includes(e.toLowerCase())?`\\${e}`:e}parseIdentifier(e){if(!e.includes("_"))return this.escapeGreek(e);const s=e.split("_"),i=this.escapeGreek(s[0]||""),t=[];s[1]&&t.push(s[1]),s.length>4&&t.push(...s.slice(4));let r="";s.length===3&&(s[2]||[]).length===1?t.push(this.escapeGreek(s[2]||"")):s[2]&&(r=this.escapeGreek(s[2]),s[3]&&(r+=`_{${this.escapeGreek(s[3])}}`)),t.forEach((u,l)=>{t[l]=this.escapeGreek(u)});const c=t.join(",");let h=i;return c&&(h+=`_{${c}}`),r&&(h+=`^{${r}}`),h}parseApply(e){const s=Array.from(e?.children||[]),i=s[0]?.localName,t=s.slice(1).map(r=>this.parseNode(r));switch(i){case"plus":return t.join(" + ");case"minus":return t.length===1?`-${t[0]}`:`${t[0]} - ${t[1]}`;case"times":return t.join(" \\cdot ");case"divide":return`\\frac{${t[0]}}{${t[1]}}`;case"eq":return`${t[0]} == ${t[1]}`;case"neq":return`${t[0]} \\neq ${t[1]}`;case"lt":return`${t[0]} < ${t[1]}`;case"leq":return`${t[0]} \\leq ${t[1]}`;case"gt":return`${t[0]} > ${t[1]}`;case"geq":return`${t[0]} \\geq ${t[1]}`;case"and":return t.join(" \\land ");case"or":return t.join(" \\lor ");case"power":const r=s[1],c=t[0]||"",h=t[1];return r?.localName==="ci"||r?.localName==="cn"&&!c.trim().startsWith("-")?`{${c}}^{${h}}`:`\\left({${c}}\\right)^{${h}}`;case"root":case"sqrt":return`\\sqrt{${t[0]}}`;case"diff":const l=s.find(x=>x.localName==="bvar"),p=s.find(x=>x.localName!=="diff"&&x.localName!=="bvar"),d=l?this.parseNode(l.firstElementChild):"x";return`\\frac{d${p?this.parseNode(p):"y"}}{d${d}}`;case"exp":return`e^{${t[0]}}`;case"abs":return`\\left|${t[0]}\\right|`;case"floor":return`\\lfloor ${t[0]} \\rfloor`;case"ceil":return`\\lceil ${t[0]} \\rceil`;case"cos":case"cosh":case"log10":case"log":case"ln":case"max":case"min":case"sin":case"sinh":case"tan":case"tanh":return`\\${i}\\left(${t[0]}\\right)`;default:return console.log(`Unsupported MathML operator: ${i}`),`\\text{${i}}(${t.join(", ")})`}}parsePiecewise(e){let s="";return Array.from(e.children).forEach(t=>{if(t.localName==="piece"){const r=this.parseNode(t.children[0]),c=this.parseNode(t.children[1]);s+=`${r} & \\text{if } ${c} \\\\ `}else if(t.localName==="otherwise"){const r=this.parseNode(t.children[0]);s+=`${r} & \\text{otherwise}`}}),`\\begin{cases} ${s} \\end{cases}`}}m.CellMLLatexGenerator=E,m.CellMLTextGenerator=C,m.CellMLTextParser=$,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})}));
|
|
11
|
+
`);if(e.localName==="apply"&&e.firstElementChild?.localName==="eq"){const s=Array.from(e.children),i=this.parseNode(s[1]),t=this.parseNode(s[2]);return`${i} = ${t}`}return this.parseNode(e)}ignoreTag(e){return["bvar"].includes(e)}parseNode(e){if(!e)return"";const s=e.localName;return s==="apply"?this.parseApply(e):s==="ci"?this.parseIdentifier(e.textContent||""):s==="cn"?e.textContent||"0":s==="piecewise"?this.parsePiecewise(e):s==="pi"?"\\pi":(this.ignoreTag(s)||console.warn(`Unsupported MathML node: ${s}`),"")}escapeGreek(e){return["alpha","beta","gamma","delta","epsilon","zeta","eta","theta","iota","kappa","lambda","mu","nu","xi","omicron","pi","rho","sigma","tau","upsilon","phi","chi","psi","omega"].includes(e.toLowerCase())?`\\${e}`:e}parseIdentifier(e){if(!e.includes("_"))return this.escapeGreek(e);const s=e.split("_"),i=this.escapeGreek(s[0]||""),t=[];s[1]&&t.push(s[1]),s.length>4&&t.push(...s.slice(4));let r="";s.length===3&&(s[2]||[]).length===1?t.push(this.escapeGreek(s[2]||"")):s[2]&&(r=this.escapeGreek(s[2]),s[3]&&(r+=`_{${this.escapeGreek(s[3])}}`)),t.forEach((u,l)=>{t[l]=this.escapeGreek(u)});const c=t.join(",");let h=i;return c&&(h+=`_{${c}}`),r&&(h+=`^{${r}}`),h}parseApply(e){const s=Array.from(e?.children||[]),i=s[0]?.localName,t=s.slice(1).map(r=>this.parseNode(r));switch(i){case"plus":return t.join(" + ");case"minus":return t.length===1?`-${t[0]}`:`${t[0]} - ${t[1]}`;case"times":return t.join(" \\cdot ");case"divide":return`\\frac{${t[0]}}{${t[1]}}`;case"eq":return`${t[0]} == ${t[1]}`;case"neq":return`${t[0]} \\neq ${t[1]}`;case"lt":return`${t[0]} < ${t[1]}`;case"leq":return`${t[0]} \\leq ${t[1]}`;case"gt":return`${t[0]} > ${t[1]}`;case"geq":return`${t[0]} \\geq ${t[1]}`;case"and":return t.join(" \\land ");case"or":return t.join(" \\lor ");case"power":const r=s[1],c=t[0]||"",h=t[1];return r?.localName==="ci"||r?.localName==="cn"&&!c.trim().startsWith("-")?`{${c}}^{${h}}`:`\\left({${c}}\\right)^{${h}}`;case"root":case"sqrt":return`\\sqrt{${t[0]}}`;case"diff":const l=s.find(x=>x.localName==="bvar"),p=s.find(x=>x.localName!=="diff"&&x.localName!=="bvar"),d=l?this.parseNode(l.firstElementChild):"x";return`\\frac{d${p?this.parseNode(p):"y"}}{d${d}}`;case"exp":return`e^{${t[0]}}`;case"abs":return`\\left|${t[0]}\\right|`;case"floor":return`\\lfloor ${t[0]} \\rfloor`;case"ceil":return`\\lceil ${t[0]} \\rceil`;case"cos":case"cosh":case"log10":case"log":case"ln":case"max":case"min":case"sin":case"sinh":case"tan":case"tanh":return`\\${i}\\left(${t[0]}\\right)`;default:return console.log(`Unsupported MathML operator: ${i}`),`\\text{${i}}(${t.join(", ")})`}}parsePiecewise(e){let s="";return Array.from(e.children).forEach(t=>{if(t.localName==="piece"){const r=this.parseNode(t.children[0]),c=this.parseNode(t.children[1]);s+=`${r} & \\text{if } ${c} \\\\ `}else if(t.localName==="otherwise"){const r=this.parseNode(t.children[0]);s+=`${r} & \\text{otherwise}`}}),`\\begin{cases} ${s} \\end{cases}`}}m.CellMLLatexGenerator=E,m.CellMLTextGenerator=C,m.CellMLTextParser=$,Object.defineProperty(m,Symbol.toStringTag,{value:"Module"})}));
|