Merge pull request #347 from gagern/splitEnvironments

Split up environments list into calls to defineEnvironment
This commit is contained in:
Kevin Barabash 2015-09-14 22:51:04 -06:00
commit d7d1367558

View File

@ -38,32 +38,54 @@ function parseArray(parser, pos, mode, result) {
} }
/* /*
* An environment definition is very similar to a function definition. * An environment definition is very similar to a function definition:
* Each element of the following array may contain * it is declared with a name or a list of names, a set of properties
* - names: The names associated with a function. This can be used to * and a handler containing the actual implementation.
* share one implementation between several similar environments. *
* The properties include:
* - numArgs: The number of arguments after the \begin{name} function. * - numArgs: The number of arguments after the \begin{name} function.
* - argTypes: (optional) Just like for a function * - argTypes: (optional) Just like for a function
* - allowedInText: (optional) Whether or not the environment is allowed inside * - allowedInText: (optional) Whether or not the environment is allowed inside
* text mode (default false) (not enforced yet) * text mode (default false) (not enforced yet)
* - numOptionalArgs: (optional) Just like for a function * - numOptionalArgs: (optional) Just like for a function
* - handler: The function that is called to handle this environment. * A bare number instead of that object indicates the numArgs value.
* It will receive the following arguments: *
* The handler function will receive the following arguments:
* - pos: the current position of the parser. * - pos: the current position of the parser.
* - mode: the current parsing mode. * - mode: the current parsing mode.
* - envName: the name of the environment, one of the listed names. * - envName: the name of the environment, one of the listed names.
* - [args]: the arguments passed to \begin. * - [args]: the arguments passed to \begin.
* - positions: the positions associated with these arguments. * - positions: the positions associated with these arguments.
* The handler is called with `this` referring to the parser.
* It must return a ParseResult.
*/ */
var environmentDefinitions = [ function defineEnvironment(names, props, handler) {
if (typeof names === "string") {
names = [names];
}
if (typeof props === "number") {
props = { numArgs: props };
}
// Set default values of environments
var data = {
numArgs: props.numArgs || 0,
argTypes: props.argTypes,
greediness: 1,
allowedInText: !!props.allowedInText,
numOptionalArgs: props.numOptionalArgs || 0,
handler: handler
};
for (var i = 0; i < names.length; ++i) {
module.exports[names[i]] = data;
}
}
// Arrays are part of LaTeX, defined in lttab.dtx so its documentation // Arrays are part of LaTeX, defined in lttab.dtx so its documentation
// is part of the source2e.pdf file of LaTeX2e source documentation. // is part of the source2e.pdf file of LaTeX2e source documentation.
{ defineEnvironment("array", {
names: ["array"], numArgs: 1
numArgs: 1, }, function(pos, mode, envName, colalign, positions) {
handler: function(pos, mode, envName, colalign, positions) {
var parser = this; var parser = this;
colalign = colalign.value.map ? colalign.value : [colalign]; colalign = colalign.value.map ? colalign.value : [colalign];
var cols = colalign.map(function(node) { var cols = colalign.map(function(node) {
@ -90,21 +112,19 @@ var environmentDefinitions = [
}; };
res = parseArray(parser, pos, mode, res); res = parseArray(parser, pos, mode, res);
return res; return res;
} });
},
// The matrix environments of amsmath builds on the array environment // The matrix environments of amsmath builds on the array environment
// of LaTeX, which is discussed above. // of LaTeX, which is discussed above.
{ defineEnvironment([
names: [
"matrix", "matrix",
"pmatrix", "pmatrix",
"bmatrix", "bmatrix",
"Bmatrix", "Bmatrix",
"vmatrix", "vmatrix",
"Vmatrix" "Vmatrix"
], ], {
handler: function(pos, mode, envName) { }, function(pos, mode, envName) {
var delimiters = { var delimiters = {
"matrix": null, "matrix": null,
"pmatrix": ["(", ")"], "pmatrix": ["(", ")"],
@ -126,15 +146,13 @@ var environmentDefinitions = [
}, mode); }, mode);
} }
return res; return res;
} });
},
// A cases environment (in amsmath.sty) is almost equivalent to // A cases environment (in amsmath.sty) is almost equivalent to
// \def\arraystretch{1.2}% // \def\arraystretch{1.2}%
// \left\{\begin{array}{@{}l@{\quad}l@{}} … \end{array}\right. // \left\{\begin{array}{@{}l@{\quad}l@{}} … \end{array}\right.
{ defineEnvironment("cases", {
names: ["cases"], }, function(pos, mode, envName) {
handler: function(pos, mode, envName) {
var res = { var res = {
type: "array", type: "array",
arraystretch: 1.2, arraystretch: 1.2,
@ -157,22 +175,4 @@ var environmentDefinitions = [
right: "." right: "."
}, mode); }, mode);
return res; return res;
} });
}
];
module.exports = (function() {
// nested function so we don't leak i and j into the module scope
var exports = {};
for (var i = 0; i < environmentDefinitions.length; ++i) {
var def = environmentDefinitions[i];
def.greediness = 1;
def.allowedInText = !!def.allowedInText;
def.numArgs = def.numArgs || 0;
def.numOptionalArgs = def.numOptionalArgs || 0;
for (var j = 0; j < def.names.length; ++j) {
exports[def.names[j]] = def;
}
}
return exports;
})();