Skip to content

Commit

Permalink
Remove old attempt to embed inputs inside displayed math (#256)
Browse files Browse the repository at this point in the history
* convert displayed math tests to vitest
  • Loading branch information
dqnykamp authored Nov 12, 2024
1 parent 7678ab1 commit d6c323b
Show file tree
Hide file tree
Showing 13 changed files with 1,396 additions and 2,223 deletions.
18 changes: 1 addition & 17 deletions packages/doenetml-worker/src/components/Function.js
Original file line number Diff line number Diff line change
Expand Up @@ -2694,6 +2694,7 @@ export default class Function extends InlineComponent {

stateVariableDefinitions.latex = {
public: true,
forRenderer: true,
shadowingInstructions: {
createComponentOfType: "latex",
},
Expand Down Expand Up @@ -2756,23 +2757,6 @@ export default class Function extends InlineComponent {
},
};

stateVariableDefinitions.latexWithInputChildren = {
forRenderer: true,
returnDependencies: () => ({
latex: {
dependencyType: "stateVariable",
variableName: "latex",
},
}),
definition: function ({ dependencyValues }) {
return {
setValue: {
latexWithInputChildren: [dependencyValues.latex],
},
};
},
};

stateVariableDefinitions.text = {
public: true,
shadowingInstructions: {
Expand Down
103 changes: 8 additions & 95 deletions packages/doenetml-worker/src/components/MMeMen.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
returnAnchorStateVariableDefinition,
} from "../utils/graphical";
import { latexToAst, superSubscriptsToUnicode } from "../utils/math";
import { createInputStringFromChildren } from "../utils/parseMath";

export class M extends InlineComponent {
constructor(args) {
Expand Down Expand Up @@ -93,7 +94,6 @@ export class M extends InlineComponent {
},
}),
definition: function ({ dependencyValues }) {
let warnings = [];
if (dependencyValues.inlineChildren.length === 0) {
return {
useEssentialOrDefaultValue: {
Expand All @@ -102,33 +102,14 @@ export class M extends InlineComponent {
};
}

let pieces = [];
for (let child of dependencyValues.inlineChildren) {
if (typeof child !== "object") {
let childtrim = String(child).trim();
if (childtrim) {
pieces.push(childtrim);
}
} else if (typeof child.stateValues.latex === "string") {
let latex = child.stateValues.latex.trim();
if (latex) {
pieces.push(latex);
}
} else if (typeof child.stateValues.text === "string") {
let text = child.stateValues.text.trim();
if (text) {
pieces.push(text);
}
} else {
warnings.push({
message: `Child <${child.componentType}> of <${componentClass.componentType}> ignored as it does not have a string "text" or "latex" state variable.`,
level: 1,
});
}
}
let latex = pieces.join(" ");
let latex = createInputStringFromChildren({
children: dependencyValues.inlineChildren,
codePre: "",
format: "latex",
createDisplayedMathString: true,
}).string;

return { setValue: { latex }, sendWarnings: warnings };
return { setValue: { latex } };
},
inverseDefinition({
desiredStateVariableValues,
Expand Down Expand Up @@ -198,74 +179,6 @@ export class M extends InlineComponent {
},
};

stateVariableDefinitions.latexWithInputChildren = {
forRenderer: true,
returnDependencies: () => ({
inlineChildren: {
dependencyType: "child",
childGroups: ["inline"],
variableNames: ["latex", "text"],
variablesOptional: true,
},
latex: {
dependencyType: "stateVariable",
variableName: "latex",
},
}),
definition: function ({ dependencyValues, componentInfoObjects }) {
if (dependencyValues.inlineChildren.length === 0) {
return {
setValue: {
latexWithInputChildren: [dependencyValues.latex],
},
};
}

let latexWithInputChildren = [];
let lastLatexPieces = [];
let inputInd = 0;
for (let child of dependencyValues.inlineChildren) {
if (typeof child !== "object") {
let childtrim = String(child).trim();
if (childtrim) {
lastLatexPieces.push(childtrim);
}
} else if (
componentInfoObjects.isInheritedComponentType({
inheritedComponentType: child.componentType,
baseComponentType: "input",
})
) {
if (lastLatexPieces.length > 0) {
latexWithInputChildren.push(
lastLatexPieces.join(" "),
);
lastLatexPieces = [];
}
latexWithInputChildren.push(inputInd);
inputInd++;
} else {
if (typeof child.stateValues.latex === "string") {
let latex = child.stateValues.latex.trim();
if (latex) {
lastLatexPieces.push(latex);
}
} else if (typeof child.stateValues.text === "string") {
let text = child.stateValues.text.trim();
if (text) {
lastLatexPieces.push(text);
}
}
}
}
if (lastLatexPieces.length > 0) {
latexWithInputChildren.push(lastLatexPieces.join(" "));
}

return { setValue: { latexWithInputChildren } };
},
};

stateVariableDefinitions.renderMode = {
forRenderer: true,
returnDependencies: () => ({}),
Expand Down
17 changes: 0 additions & 17 deletions packages/doenetml-worker/src/components/Math.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,23 +836,6 @@ export default class MathComponent extends InlineComponent {
},
};

stateVariableDefinitions.latexWithInputChildren = {
forRenderer: true,
returnDependencies: () => ({
latex: {
dependencyType: "stateVariable",
variableName: "latex",
},
}),
definition: function ({ dependencyValues }) {
return {
setValue: {
latexWithInputChildren: [dependencyValues.latex],
},
};
},
};

stateVariableDefinitions.text = {
public: true,
shadowingInstructions: {
Expand Down
82 changes: 14 additions & 68 deletions packages/doenetml-worker/src/components/MdMdnMrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ export class Md extends InlineComponent {

stateVariableDefinitions.latex = {
public: true,
forRenderer: true,
shadowingInstructions: {
createComponentOfType: "latex",
},
forRenderer: true,
returnDependencies: () => ({
mrowChildren: {
dependencyType: "child",
Expand Down Expand Up @@ -128,70 +128,6 @@ export class Md extends InlineComponent {
},
};

stateVariableDefinitions.latexWithInputChildren = {
forRenderer: true,
returnDependencies: () => ({
mrowChildren: {
dependencyType: "child",
childGroups: ["mrows"],
variableNames: [
"latexWithInputChildren",
"hide",
"equationTag",
"numbered",
],
},
latex: {
dependencyType: "stateVariable",
variableName: "latex",
},
}),
definition: function ({ dependencyValues }) {
if (dependencyValues.mrowChildren.length > 0) {
let latexWithInputChildren = [];
let inputInd = 0;
let lastLatex = "";

for (let mrow of dependencyValues.mrowChildren) {
if (mrow.stateValues.hide) {
continue;
}
if (lastLatex.length > 0) {
lastLatex += "\\\\";
}
if (mrow.stateValues.numbered) {
lastLatex += `\\tag{${mrow.stateValues.equationTag}}`;
} else {
lastLatex += "\\notag ";
}
for (let latexOrChildInd of mrow.stateValues
.latexWithInputChildren) {
if (typeof latexOrChildInd === "number") {
if (lastLatex.length > 0) {
latexWithInputChildren.push(lastLatex);
lastLatex = "";
}
latexWithInputChildren.push(inputInd);
inputInd++;
} else {
lastLatex += latexOrChildInd;
}
}
}
if (lastLatex.length > 0) {
latexWithInputChildren.push(lastLatex);
}
return { setValue: { latexWithInputChildren } };
} else {
return {
setValue: {
latexWithInputChildren: [dependencyValues.latex],
},
};
}
},
};

stateVariableDefinitions.text = {
public: true,
shadowingInstructions: {
Expand All @@ -210,9 +146,19 @@ export class Md extends InlineComponent {
.replaceAll("\\notag", "")
.replaceAll("\\amp", "")
.split("\\\\")
.map((x) =>
me.fromAst(latexToAst.convert(x)).toString(),
)
.map((x) => {
let result = x.match(/\\tag\{(\w+)\}(.*)/);
if (result) {
x = result[2];
}
let text = me
.fromAst(latexToAst.convert(x))
.toString();
if (result) {
text += ` (${result[1]})`;
}
return text;
})
.join("\\\\\n");
} catch (e) {
// just return latex if can't parse with math-expressions
Expand Down
5 changes: 5 additions & 0 deletions packages/doenetml-worker/src/components/Ref.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,11 @@ export default class Ref extends InlineComponent {
},
};

stateVariableDefinitions.text = {
isAlias: true,
targetVariableName: "linkText",
};

return stateVariableDefinitions;
}

Expand Down
9 changes: 2 additions & 7 deletions packages/doenetml-worker/src/components/chemistry/Atom.js
Original file line number Diff line number Diff line change
Expand Up @@ -809,15 +809,10 @@ export default class Atom extends InlineComponent {

stateVariableDefinitions.latex = {
public: true,
forRenderer: true,
shadowingInstructions: {
createComponentOfType: "latex",
},
additionalStateVariablesDefined: [
{
variableName: "latexWithInputChildren",
forRenderer: true,
},
],
returnDependencies: () => ({
symbol: {
dependencyType: "stateVariable",
Expand All @@ -832,7 +827,7 @@ export default class Atom extends InlineComponent {
latex = "[\\text{Invalid Chemical Symbol}]";
}
return {
setValue: { latex, latexWithInputChildren: [latex] },
setValue: { latex },
};
},
};
Expand Down
9 changes: 2 additions & 7 deletions packages/doenetml-worker/src/components/chemistry/Ion.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,15 +649,10 @@ export default class Ion extends InlineComponent {

stateVariableDefinitions.latex = {
public: true,
forRenderer: true,
shadowingInstructions: {
createComponentOfType: "latex",
},
additionalStateVariablesDefined: [
{
variableName: "latexWithInputChildren",
forRenderer: true,
},
],
returnDependencies: () => ({
symbol: {
dependencyType: "stateVariable",
Expand Down Expand Up @@ -687,7 +682,7 @@ export default class Ion extends InlineComponent {
latex = "[\\text{Invalid Chemical Symbol}]";
}
return {
setValue: { latex, latexWithInputChildren: [latex] },
setValue: { latex },
};
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,10 @@ export default class IonicCompound extends InlineComponent {

stateVariableDefinitions.latex = {
public: true,
forRenderer: true,
shadowingInstructions: {
createComponentOfType: "latex",
},
additionalStateVariablesDefined: [
{
variableName: "latexWithInputChildren",
forRenderer: true,
},
],
returnDependencies: () => ({
ionicCompound: {
dependencyType: "stateVariable",
Expand All @@ -193,7 +188,7 @@ export default class IonicCompound extends InlineComponent {
latex = "[\\text{Invalid Ionic Compound}]";
}
return {
setValue: { latex, latexWithInputChildren: [latex] },
setValue: { latex },
};
},
};
Expand Down
Loading

0 comments on commit d6c323b

Please sign in to comment.