Make Node.toTex a wrapper like Node.compile

Node.prototype.toTex is now a wrapper that calls the node's _toTex
This commit is contained in:
Max Bruckner 2015-03-13 21:23:14 +01:00
parent 37c1363c2d
commit 35ce7f7fb4
14 changed files with 32 additions and 17 deletions

View File

@ -99,7 +99,7 @@ ArrayNode.prototype.toString = function() {
* @param {String} type
* @return {String} str
*/
ArrayNode.prototype.toTex = function(customFunctions, type) {
ArrayNode.prototype._toTex = function(customFunctions, type) {
type = type || 'bmatrix';
var s = '\\begin{' + type + '}';

View File

@ -92,7 +92,7 @@ AssignmentNode.prototype.toString = function() {
* @param {Object|function} callback(s)
* @return {String}
*/
AssignmentNode.prototype.toTex = function(customFunctions) {
AssignmentNode.prototype._toTex = function(customFunctions) {
var precedence = operators.getPrecedence(this);
var exprPrecedence = operators.getPrecedence(this.expr);

View File

@ -125,7 +125,7 @@ BlockNode.prototype.toString = function() {
* @param {Object|function} callback(s)
* @return {String} str
*/
BlockNode.prototype.toTex = function(customFunctions) {
BlockNode.prototype._toTex = function(customFunctions) {
return this.blocks.map(function (param) {
return param.node.toTex(customFunctions) + (param.visible ? '' : ';');
}).join('\n');

View File

@ -154,7 +154,7 @@ ConditionalNode.prototype.toString = function() {
* @param {Object|function} callback(s)
* @return {String} str
*/
ConditionalNode.prototype.toTex = function(customFunctions) {
ConditionalNode.prototype._toTex = function(customFunctions) {
var s = (
latex.addBraces(this.trueExpr.toTex(customFunctions)) +
', &\\quad' +

View File

@ -159,7 +159,7 @@ ConstantNode.prototype.toString = function() {
* @param {Object|function} callback(s)
* @return {String} str
*/
ConstantNode.prototype.toTex = function(customFunctions) {
ConstantNode.prototype._toTex = function(customFunctions) {
var value = this.value,
index;
switch (this.valueType) {

View File

@ -113,7 +113,7 @@ FunctionAssignmentNode.prototype.toString = function() {
* @param {Object|function} callback(s)
* @return {String} str
*/
FunctionAssignmentNode.prototype.toTex = function(customFunctions) {
FunctionAssignmentNode.prototype._toTex = function(customFunctions) {
var precedence = operators.getPrecedence(this);
var exprPrecedence = operators.getPrecedence(this.expr);

View File

@ -116,7 +116,7 @@ FunctionNode.prototype.toString = function() {
* @param {Object|function} callback(s)
* @return {String} str
*/
FunctionNode.prototype.toTex = function(customFunctions) {
FunctionNode.prototype._toTex = function(customFunctions) {
return latex.toArgs(this, customFunctions);
};

View File

@ -212,7 +212,7 @@ IndexNode.prototype.toString = function() {
* @param {Object|function} callback(s)
* @return {String} str
*/
IndexNode.prototype.toTex = function(customFunctions) {
IndexNode.prototype._toTex = function(customFunctions) {
return this.object.toTex(customFunctions) + '[' + this.ranges.join(', ') + ']';
};

View File

@ -222,10 +222,23 @@ Node.prototype.toString = function() {
/**
* Get LaTeX representation
* @param {Object|function} callback(s)
* @return {String}
*/
Node.prototype.toTex = function() {
return '';
Node.prototype.toTex = function(customFunctions) {
return this._toTex(customFunctions);
};
/**
* Internal function to generate the LaTeX output.
* This has to be implemented by every Node
*
* @param {Object}|function}
* @throws {Error}
*/
Node.prototype._toTex = function () {
//must be implemented by each of the Node implementations
throw new Error('_toTex not implemented for this Node');
};
/**

View File

@ -239,7 +239,7 @@ OperatorNode.prototype.toString = function() {
* @param {Object|function} callback(s)
* @return {String} str
*/
OperatorNode.prototype.toTex = function(customFunctions) {
OperatorNode.prototype._toTex = function(customFunctions) {
var args = this.args;
var parens = calculateNecessaryParentheses(this, args);
var op = latex.toOperator(this.op); //operator

View File

@ -124,7 +124,7 @@ RangeNode.prototype.toString = function() {
* @params {Object|function} callback(s)
* @return {String} str
*/
RangeNode.prototype.toTex = function(customFunctions) {
RangeNode.prototype._toTex = function(customFunctions) {
var str = this.start.toTex(customFunctions);
if (this.step) {
str += ':' + this.step.toTex(customFunctions);

View File

@ -103,7 +103,7 @@ SymbolNode.prototype.toString = function() {
* @return {String} str
* @override
*/
SymbolNode.prototype.toTex = function(customFunctions) {
SymbolNode.prototype._toTex = function(customFunctions) {
return latex.toSymbol(this.name, customFunctions);
};

View File

@ -87,7 +87,7 @@ UpdateNode.prototype.toString = function() {
* @param {Object|function} callback(s)
* @return {String}
*/
UpdateNode.prototype.toTex = function(customFunctions) {
UpdateNode.prototype._toTex = function(customFunctions) {
return this.index.toTex(customFunctions) + ' = ' + this.expr.toTex(customFunctions);
};

View File

@ -74,9 +74,11 @@ describe('Node', function() {
assert.equal(node.toString(), '');
});
it ('should LaTeX a Node', function () {
var node = new Node();
assert.equal(node.toTex(), '');
it ('should throw an error when calling toTex', function () {
assert.throws(function () {
var node = new Node();
node.toTex();
}, /_toTex not implemented for this Node/);
});
it ('should throw an error in case of wrong arguments for compile', function () {