Skip to content

Commit

Permalink
Simplified MASTON output for mixed zones (text/math)
Browse files Browse the repository at this point in the history
  • Loading branch information
arnog committed Apr 8, 2019
1 parent 10616a5 commit d7d0b62
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dist/mathlive.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/mathlive.mjs

Large diffs are not rendered by default.

42 changes: 36 additions & 6 deletions dist/src/addons/maston.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const CANONICAL_NAMES = {
'\\approx': 'approx',
'\\cong': 'congruent',
'\\sim': 'similar',
'\\equiv': 'equiv',
'\\pm': 'plusminus', // PLUS-MINUS SIGN

'\\land': 'and',
Expand Down Expand Up @@ -137,6 +138,7 @@ const OP_NAME = {
'approx': 'approx',
'congruent': 'congruent',
'similar': 'similar',
'equiv': 'equiv',
'<': 'lt',
'>': 'gt',
'<=': 'le',
Expand All @@ -159,6 +161,7 @@ const FUNCTION_TEMPLATE = {
'approx': '%0 \\approx %1',
'congruent': '%0 \\cong %1',
'similar': '%0 \\sim %1',
'equiv': '%0 \\equiv %1',
'assign': '%0 := %1',
'lt': '%0 < %1',
'gt': '%0 > %1',
Expand Down Expand Up @@ -326,6 +329,7 @@ const OP_PRECEDENCE = {

// Relational
'congruent': 265,
'equiv': 260, // MathML: "identical to"
'=': 260,
'!=': 255,
'?=': 255,
Expand Down Expand Up @@ -1685,9 +1689,34 @@ MathAtom.MathAtom.prototype.toAST = function(options) {
case 'array':
if (this.env.name === 'cardinality') {
result = wrapFn('card', parse(this.array, options));
} else if (/matrix|pmatrix|bmatrix/.test(this.env.name)) {

result = wrapFn('array', parse(this.array, options));

} else if (/array|matrix|pmatrix|bmatrix/.test(this.env.name)) {
result = { fn: 'array', args: [] };
for (const row of this.array) {
result.args.push(row.map(cell => parse(cell, options)));
}

} else if (this.env.name === 'cases') {
result = { fn: 'cases', args: [] };
for (const row of this.array) {
if (row[0]) {
const statement = [];
statement.push(parse(row[0], options));
let condition = parse(row[1], options);
if (condition) {
if (condition.fn === 'text' && condition.arg) {
if (/^(if|when|for)$/i.test(condition.arg[0].trim() )) {
condition = condition.arg.filter(
x => typeof x !== 'string')
;
}
}
}

statement.push(condition || {});
result.args.push(statement);
}
}
}
break;

Expand Down Expand Up @@ -1751,7 +1780,8 @@ function filterPresentationAtoms(atoms) {
atoms.numer = filterPresentationAtoms(atoms.numer);
}
if (atoms.array && Array.isArray(atoms.array)) {
atoms.array = filterPresentationAtoms(atoms.array);
atoms.array = atoms.array.map(row => row.map(cell =>
filterPresentationAtoms(cell)));
}
result = [atoms];
}
Expand Down Expand Up @@ -1780,7 +1810,7 @@ function parseSentence(expr, options) {
text += expr.atoms[expr.index].body;
expr.index += 1;
}
zones.push(wrapFn('text', text));
zones.push(text);
} else {
const z = parseExpression(expr, options).ast;
// Something went wrong in parsing the expression...
Expand All @@ -1790,7 +1820,7 @@ function parseSentence(expr, options) {
}

if (zones.length > 1) {
return wrapFn('list0', zones);
return wrapFn('text', ...zones);
}

return zones[0] || undefined;
Expand Down
4 changes: 2 additions & 2 deletions dist/src/editor/editor-editableMathlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -1565,7 +1565,7 @@ EditableMathlist.prototype.up = function(options) {
// In an array
let colRow = arrayColRow(this.parent().array, relation);
colRow = arrayAdjustRow(this.parent().array, colRow, -1);
if (colRow) {
if (colRow && arrayCell(colRow)) {
this.path[this.path.length - 1].relation =
'cell' + arrayIndex(this.parent().array, colRow);
this.setSelection(this.anchorOffset());
Expand Down Expand Up @@ -1602,7 +1602,7 @@ EditableMathlist.prototype.down = function(options) {
// In an array
let colRow = arrayColRow(this.parent().array, relation);
colRow = arrayAdjustRow(this.parent().array, colRow, +1);
if (colRow) {
if (colRow && arrayCell(colRow)) {
this.path[this.path.length - 1].relation =
'cell' + arrayIndex(this.parent().array, colRow);
this.setSelection(this.anchorOffset());
Expand Down
1 change: 1 addition & 0 deletions dist/src/editor/editor-shortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ function validateShortcut(siblings, shortcut) {
array = sibling.array;
openfence = sibling.type === 'mopen';
closefence = sibling.type === 'mclose' || sibling.type === 'leftright';
space = sibling.type === 'space';
}

if (typeof shortcut === 'object') {
Expand Down
42 changes: 36 additions & 6 deletions src/addons/maston.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const CANONICAL_NAMES = {
'\\approx': 'approx',
'\\cong': 'congruent',
'\\sim': 'similar',
'\\equiv': 'equiv',
'\\pm': 'plusminus', // PLUS-MINUS SIGN

'\\land': 'and',
Expand Down Expand Up @@ -137,6 +138,7 @@ const OP_NAME = {
'approx': 'approx',
'congruent': 'congruent',
'similar': 'similar',
'equiv': 'equiv',
'<': 'lt',
'>': 'gt',
'<=': 'le',
Expand All @@ -159,6 +161,7 @@ const FUNCTION_TEMPLATE = {
'approx': '%0 \\approx %1',
'congruent': '%0 \\cong %1',
'similar': '%0 \\sim %1',
'equiv': '%0 \\equiv %1',
'assign': '%0 := %1',
'lt': '%0 < %1',
'gt': '%0 > %1',
Expand Down Expand Up @@ -326,6 +329,7 @@ const OP_PRECEDENCE = {

// Relational
'congruent': 265,
'equiv': 260, // MathML: "identical to"
'=': 260,
'!=': 255,
'?=': 255,
Expand Down Expand Up @@ -1685,9 +1689,34 @@ MathAtom.MathAtom.prototype.toAST = function(options) {
case 'array':
if (this.env.name === 'cardinality') {
result = wrapFn('card', parse(this.array, options));
} else if (/matrix|pmatrix|bmatrix/.test(this.env.name)) {

result = wrapFn('array', parse(this.array, options));

} else if (/array|matrix|pmatrix|bmatrix/.test(this.env.name)) {
result = { fn: 'array', args: [] };
for (const row of this.array) {
result.args.push(row.map(cell => parse(cell, options)));
}

} else if (this.env.name === 'cases') {
result = { fn: 'cases', args: [] };
for (const row of this.array) {
if (row[0]) {
const statement = [];
statement.push(parse(row[0], options));
let condition = parse(row[1], options);
if (condition) {
if (condition.fn === 'text' && condition.arg) {
if (/^(if|when|for)$/i.test(condition.arg[0].trim() )) {
condition = condition.arg.filter(
x => typeof x !== 'string')
;
}
}
}

statement.push(condition || {});
result.args.push(statement);
}
}
}
break;

Expand Down Expand Up @@ -1751,7 +1780,8 @@ function filterPresentationAtoms(atoms) {
atoms.numer = filterPresentationAtoms(atoms.numer);
}
if (atoms.array && Array.isArray(atoms.array)) {
atoms.array = filterPresentationAtoms(atoms.array);
atoms.array = atoms.array.map(row => row.map(cell =>
filterPresentationAtoms(cell)));
}
result = [atoms];
}
Expand Down Expand Up @@ -1780,7 +1810,7 @@ function parseSentence(expr, options) {
text += expr.atoms[expr.index].body;
expr.index += 1;
}
zones.push(wrapFn('text', text));
zones.push(text);
} else {
const z = parseExpression(expr, options).ast;
// Something went wrong in parsing the expression...
Expand All @@ -1790,7 +1820,7 @@ function parseSentence(expr, options) {
}

if (zones.length > 1) {
return wrapFn('list0', zones);
return wrapFn('text', ...zones);
}

return zones[0] || undefined;
Expand Down
4 changes: 2 additions & 2 deletions src/editor/editor-editableMathlist.js
Original file line number Diff line number Diff line change
Expand Up @@ -1565,7 +1565,7 @@ EditableMathlist.prototype.up = function(options) {
// In an array
let colRow = arrayColRow(this.parent().array, relation);
colRow = arrayAdjustRow(this.parent().array, colRow, -1);
if (colRow) {
if (colRow && arrayCell(colRow)) {
this.path[this.path.length - 1].relation =
'cell' + arrayIndex(this.parent().array, colRow);
this.setSelection(this.anchorOffset());
Expand Down Expand Up @@ -1602,7 +1602,7 @@ EditableMathlist.prototype.down = function(options) {
// In an array
let colRow = arrayColRow(this.parent().array, relation);
colRow = arrayAdjustRow(this.parent().array, colRow, +1);
if (colRow) {
if (colRow && arrayCell(colRow)) {
this.path[this.path.length - 1].relation =
'cell' + arrayIndex(this.parent().array, colRow);
this.setSelection(this.anchorOffset());
Expand Down
1 change: 1 addition & 0 deletions src/editor/editor-shortcuts.js
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ function validateShortcut(siblings, shortcut) {
array = sibling.array;
openfence = sibling.type === 'mopen';
closefence = sibling.type === 'mclose' || sibling.type === 'leftright';
space = sibling.type === 'space';
}

if (typeof shortcut === 'object') {
Expand Down

0 comments on commit d7d0b62

Please sign in to comment.